Debian Codegraph @

v2/signed.go

raw symbols SCIP
1 // Copyright 2017 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
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 // Base for conversion as per strconv.ParseInt
17 Bits int // Number of bits as per strconv.ParseInt
18 Min int64 // Minimum allowed value if both Min and Max are not 0
19 Max int64 // Maximum allowed value if both Min and Max are not 0
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 // Signed creates an option that is stored in an int64 and is constrained
69 // by the limits pointed to by l. The Max and Min values are only used if
70 // at least one of the values are not 0. If Base is 0, the base is implied by
71 // the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.
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 }