| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
"sync" |
| 11 |
) |
| 12 |
|
| 13 |
type signed int64 |
| 14 |
|
| 15 |
type SignedLimit struct { |
| 16 |
Base int |
| 17 |
Bits int |
| 18 |
Min int64 |
| 19 |
Max int64 |
| 20 |
} |
| 21 |
|
| 22 |
var ( |
| 23 |
signedLimitsMu sync.Mutex |
| 24 |
signedLimits = make(map[*signed]*SignedLimit) |
| 25 |
) |
| 26 |
|
| 27 |
func (n *signed) Set(value string, opt Option) error { |
| 28 |
signedLimitsMu.Lock() |
| 29 |
l := signedLimits[n] |
| 30 |
signedLimitsMu.Unlock() |
| 31 |
if l == nil { |
| 32 |
return fmt.Errorf("no limits defined for %s", opt.Name()) |
| 33 |
} |
| 34 |
v, err := strconv.ParseInt(value, l.Base, l.Bits) |
| 35 |
if err != nil { |
| 36 |
if e, ok := err.(*strconv.NumError); ok { |
| 37 |
switch e.Err { |
| 38 |
case strconv.ErrRange: |
| 39 |
err = fmt.Errorf("value out of range: %s", value) |
| 40 |
case strconv.ErrSyntax: |
| 41 |
err = fmt.Errorf("not a valid number: %s", value) |
| 42 |
} |
| 43 |
} |
| 44 |
return err |
| 45 |
} |
| 46 |
if l.Min != 0 || l.Max != 0 { |
| 47 |
if v < l.Min { |
| 48 |
return fmt.Errorf("value out of range (<%v): %s", l.Min, value) |
| 49 |
} |
| 50 |
if v > l.Max { |
| 51 |
return fmt.Errorf("value out of range (>%v): %s", l.Max, value) |
| 52 |
} |
| 53 |
} |
| 54 |
*n = signed(v) |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
func (n *signed) String() string { |
| 59 |
signedLimitsMu.Lock() |
| 60 |
l := signedLimits[n] |
| 61 |
signedLimitsMu.Unlock() |
| 62 |
if l != nil && l.Base != 0 { |
| 63 |
return strconv.FormatInt(int64(*n), l.Base) |
| 64 |
} |
| 65 |
return strconv.FormatInt(int64(*n), 10) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func Signed(name rune, value int64, l *SignedLimit, helpvalue ...string) *int64 { |
| 73 |
CommandLine.signedOption(&value, "", name, l, helpvalue...) |
| 74 |
return &value |
| 75 |
} |
| 76 |
|
| 77 |
func (s *Set) Signed(name rune, value int64, l *SignedLimit, helpvalue ...string) *int64 { |
| 78 |
s.signedOption(&value, "", name, l, helpvalue...) |
| 79 |
return &value |
| 80 |
} |
| 81 |
|
| 82 |
func SignedLong(name string, short rune, value int64, l *SignedLimit, helpvalue ...string) *int64 { |
| 83 |
CommandLine.signedOption(&value, name, short, l, helpvalue...) |
| 84 |
return &value |
| 85 |
} |
| 86 |
|
| 87 |
func (s *Set) SignedLong(name string, short rune, value int64, l *SignedLimit, helpvalue ...string) *int64 { |
| 88 |
s.signedOption(&value, name, short, l, helpvalue...) |
| 89 |
return &value |
| 90 |
} |
| 91 |
|
| 92 |
func (s *Set) signedOption(p *int64, name string, short rune, l *SignedLimit, helpvalue ...string) { |
| 93 |
opt := s.FlagLong((*signed)(p), name, short, helpvalue...) |
| 94 |
if l.Base > 36 || l.Base == 1 || l.Base < 0 { |
| 95 |
fmt.Fprintf(stderr, "invalid base for %s: %d\n", opt.Name(), l.Base) |
| 96 |
exit(1) |
| 97 |
} |
| 98 |
if l.Bits < 0 || l.Bits > 64 { |
| 99 |
fmt.Fprintf(stderr, "invalid bit size for %s: %d\n", opt.Name(), l.Bits) |
| 100 |
exit(1) |
| 101 |
} |
| 102 |
if l.Min > l.Max { |
| 103 |
fmt.Fprintf(stderr, "min greater than max for %s\n", opt.Name()) |
| 104 |
exit(1) |
| 105 |
} |
| 106 |
lim := *l |
| 107 |
signedLimitsMu.Lock() |
| 108 |
signedLimits[(*signed)(p)] = &lim |
| 109 |
signedLimitsMu.Unlock() |
| 110 |
} |