1// Copyright 2013 Google Inc.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package getopt
6
7import (
8	"fmt"
9	"strconv"
10)
11
12type uintValue uint
13
14func (i *uintValue) Set(value string, opt Option) error {
15	v, err := strconv.ParseUint(value, 0, strconv.IntSize)
16	if err != nil {
17		if e, ok := err.(*strconv.NumError); ok {
18			switch e.Err {
19			case strconv.ErrRange:
20				err = fmt.Errorf("value out of range: %s", value)
21			case strconv.ErrSyntax:
22				err = fmt.Errorf("not a valid number: %s", value)
23			}
24		}
25		return err
26	}
27	*i = uintValue(v)
28	return nil
29}
30
31func (i *uintValue) String() string {
32	return strconv.FormatUint(uint64(*i), 10)
33}
34
35// Uint creates an option that parses its value as an unsigned integer.
36func Uint(name rune, value uint, helpvalue ...string) *uint {
37	return CommandLine.Uint(name, value, helpvalue...)
38}
39
40func (s *Set) Uint(name rune, value uint, helpvalue ...string) *uint {
41	return s.UintLong("", name, value, helpvalue...)
42}
43
44func UintLong(name string, short rune, value uint, helpvalue ...string) *uint {
45	return CommandLine.UintLong(name, short, value, helpvalue...)
46}
47
48func (s *Set) UintLong(name string, short rune, value uint, helpvalue ...string) *uint {
49	s.UintVarLong(&value, name, short, helpvalue...)
50	return &value
51}
52
53func UintVar(p *uint, name rune, helpvalue ...string) Option {
54	return CommandLine.UintVar(p, name, helpvalue...)
55}
56
57func (s *Set) UintVar(p *uint, name rune, helpvalue ...string) Option {
58	return s.UintVarLong(p, "", name, helpvalue...)
59}
60
61func UintVarLong(p *uint, name string, short rune, helpvalue ...string) Option {
62	return CommandLine.UintVarLong(p, name, short, helpvalue...)
63}
64
65func (s *Set) UintVarLong(p *uint, name string, short rune, helpvalue ...string) Option {
66	return s.VarLong((*uintValue)(p), name, short, helpvalue...)
67}
68