Debian Codegraph @

signed.go

raw symbols SCIP
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
5 package getopt
6
7 import (
8 "fmt"
9 "strconv"
10 )
11
12 type signed int64
13
14 type SignedLimit struct {
15 Base int // Base for conversion as per strconv.ParseInt
16 Bits int // Number of bits as per strconv.ParseInt
17 Min int64 // Minimum allowed value if both Min and Max are not 0
18 Max int64 // Maximum allowed value if both Min and Max are not 0
19 }
20
21 var signedLimits = make(map[*signed]*SignedLimit)
22
23 func (n *signed) Set(value string, opt Option) error {
24 l := signedLimits[n]
25 if l == nil {
26 return fmt.Errorf("no limits defined for %s", opt.Name())
27 }
28 v, err := strconv.ParseInt(value, l.Base, l.Bits)
29 if err != nil {
30 if e, ok := err.(*strconv.NumError); ok {
31 switch e.Err {
32 case strconv.ErrRange:
33 err = fmt.Errorf("value out of range: %s", value)
34 case strconv.ErrSyntax:
35 err = fmt.Errorf("not a valid number: %s", value)
36 }
37 }
38 return err
39 }
40 if l.Min != 0 || l.Max != 0 {
41 if v < l.Min {
42 return fmt.Errorf("value out of range (<%v): %s", l.Min, value)
43 }
44 if v > l.Max {
45 return fmt.Errorf("value out of range (>%v): %s", l.Max, value)
46 }
47 }
48 *n = signed(v)
49 return nil
50 }
51
52 func (n *signed) String() string {
53 l := signedLimits[n]
54 if l != nil && l.Base != 0 {
55 return strconv.FormatInt(int64(*n), l.Base)
56 }
57 return strconv.FormatInt(int64(*n), 10)
58 }
59
60 // Signed creates an option that is stored in an int64 and is constrained
61 // by the limits pointed to by l. The Max and Min values are only used if
62 // at least one of the values are not 0. If Base is 0, the base is implied by
63 // the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.
64 func Signed(name rune, value int64, l *SignedLimit, helpvalue ...string) *int64 {
65 return CommandLine.Signed(name, value, l, helpvalue...)
66 }
67
68 func (s *Set) Signed(name rune, value int64, l *SignedLimit, helpvalue ...string) *int64 {
69 return s.SignedLong("", name, value, l, helpvalue...)
70 }
71
72 func SignedLong(name string, short rune, value int64, l *SignedLimit, helpvalue ...string) *int64 {
73 return CommandLine.SignedLong(name, short, value, l, helpvalue...)
74 }
75
76 func (s *Set) SignedLong(name string, short rune, value int64, l *SignedLimit, helpvalue ...string) *int64 {
77 s.SignedVarLong(&value, name, short, l, helpvalue...)
78 return &value
79 }
80
81 func SignedVar(p *int64, name rune, l *SignedLimit, helpvalue ...string) Option {
82 return CommandLine.SignedVar(p, name, l, helpvalue...)
83 }
84
85 func (s *Set) SignedVar(p *int64, name rune, l *SignedLimit, helpvalue ...string) Option {
86 return s.SignedVarLong(p, "", name, l, helpvalue...)
87 }
88
89 func SignedVarLong(p *int64, name string, short rune, l *SignedLimit, helpvalue ...string) Option {
90 return CommandLine.SignedVarLong(p, name, short, l, helpvalue...)
91 }
92
93 func (s *Set) SignedVarLong(p *int64, name string, short rune, l *SignedLimit, helpvalue ...string) Option {
94 opt := s.VarLong((*signed)(p), name, short, helpvalue...)
95 if l.Base > 36 || l.Base == 1 || l.Base < 0 {
96 fmt.Fprintf(stderr, "invalid base for %s: %d\n", opt.Name(), l.Base)
97 exit(1)
98 }
99 if l.Bits < 0 || l.Bits > 64 {
100 fmt.Fprintf(stderr, "invalid bit size for %s: %d\n", opt.Name(), l.Bits)
101 exit(1)
102 }
103 if l.Min > l.Max {
104 fmt.Fprintf(stderr, "min greater than max for %s\n", opt.Name())
105 exit(1)
106 }
107 lim := *l
108 signedLimits[(*signed)(p)] = &lim
109 return opt
110 }