Debian Codegraph @

v2/generic.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 "strings"
11 "time"
12 )
13
14 type generic struct {
15 p interface{}
16 }
17
18 // Flag is shorthand for CommandLine.Flag.
19 func Flag(v interface{}, short rune, helpvalue ...string) Option {
20 return CommandLine.long(v, "", short, helpvalue...)
21 }
22
23 // FlagLong is shorthand for CommandLine.LongFlag.
24 func FlagLong(v interface{}, long string, short rune, helpvalue ...string) Option {
25 return CommandLine.long(v, long, short, helpvalue...)
26 }
27
28 // Flag calls FlagLong with only a short flag name.
29 func (s *Set) Flag(v interface{}, short rune, helpvalue ...string) Option {
30 return s.long(v, "", short, helpvalue...)
31 }
32
33 // FlagLong returns an Option in Set s for setting v. If long is not "" then
34 // the option has a long name, and if short is not 0, the option has a short
35 // name. v must either be of type getopt.Value or a pointer to one of the
36 // supported builtin types:
37 //
38 // bool, string, []string
39 // int, int8, int16, int32, int64
40 // uint, uint8, uint16, uint32, uint64
41 // float32, float64
42 // time.Duration
43 //
44 // FlagLong will panic if v is not a getopt.Value or one of the supported
45 // builtin types.
46 //
47 // The default value of the flag is the value of v at the time FlagLong is
48 // called.
49 func (s *Set) FlagLong(v interface{}, long string, short rune, helpvalue ...string) Option {
50 return s.long(v, long, short, helpvalue...)
51 }
52
53 func (s *Set) long(v interface{}, long string, short rune, helpvalue ...string) (opt Option) {
54 // Fix up our location when we return.
55 if where := calledFrom(); where != "" {
56 defer func() {
57 if opt, ok := opt.(*option); ok {
58 opt.where = where
59 }
60 }()
61 }
62 switch p := v.(type) {
63 case Value:
64 return s.addFlag(p, long, short, helpvalue...)
65 case *bool:
66 return s.addFlag(&generic{v}, long, short, helpvalue...).SetFlag()
67 case *string, *[]string:
68 return s.addFlag(&generic{v}, long, short, helpvalue...)
69 case *int, *int8, *int16, *int32, *int64:
70 return s.addFlag(&generic{v}, long, short, helpvalue...)
71 case *uint, *uint8, *uint16, *uint32, *uint64:
72 return s.addFlag(&generic{v}, long, short, helpvalue...)
73 case *float32, *float64:
74 return s.addFlag(&generic{v}, long, short, helpvalue...)
75 case *time.Duration:
76 return s.addFlag(&generic{v}, long, short, helpvalue...)
77 default:
78 panic(fmt.Sprintf("unsupported flag type: %T", v))
79 }
80 }
81
82 // genericValue returns the object underlying the generic Value v, or nil if v
83 // is not a generic Value.
84 func genericValue(v Value) interface{} {
85 if g, ok := v.(*generic); ok {
86 return g.p
87 }
88 return nil
89 }
90
91 func (g *generic) Set(value string, opt Option) error {
92 strconvErr := func(err error) error {
93 if e, ok := err.(*strconv.NumError); ok {
94 switch e.Err {
95 case strconv.ErrRange:
96 err = fmt.Errorf("value out of range: %s", value)
97 case strconv.ErrSyntax:
98 err = fmt.Errorf("not a valid number: %s", value)
99 }
100 }
101 return err
102 }
103 switch p := g.p.(type) {
104 case *bool:
105 switch strings.ToLower(value) {
106 case "", "1", "true", "on", "t":
107 *p = true
108 case "0", "false", "off", "f":
109 *p = false
110 default:
111 return fmt.Errorf("invalid value for bool %s: %q", opt.Name(), value)
112 }
113 return nil
114 case *string:
115 *p = value
116 return nil
117 case *[]string:
118 a := strings.Split(value, ",")
119 // If this is the first time we are seen then nil out the
120 // default value.
121 if opt.Count() <= 1 {
122 *p = nil
123 }
124 *p = append(*p, a...)
125 return nil
126 case *int:
127 i64, err := strconv.ParseInt(value, 0, strconv.IntSize)
128 if err == nil {
129 *p = int(i64)
130 }
131 return strconvErr(err)
132 case *int8:
133 i64, err := strconv.ParseInt(value, 0, 8)
134 if err == nil {
135 *p = int8(i64)
136 }
137 return strconvErr(err)
138 case *int16:
139 i64, err := strconv.ParseInt(value, 0, 16)
140 if err == nil {
141 *p = int16(i64)
142 }
143 return strconvErr(err)
144 case *int32:
145 i64, err := strconv.ParseInt(value, 0, 32)
146 if err == nil {
147 *p = int32(i64)
148 }
149 return strconvErr(err)
150 case *int64:
151 i64, err := strconv.ParseInt(value, 0, 64)
152 if err == nil {
153 *p = i64
154 }
155 return strconvErr(err)
156 case *uint:
157 u64, err := strconv.ParseUint(value, 0, strconv.IntSize)
158 if err == nil {
159 *p = uint(u64)
160 }
161 return strconvErr(err)
162 case *uint8:
163 u64, err := strconv.ParseUint(value, 0, 8)
164 if err == nil {
165 *p = uint8(u64)
166 }
167 return strconvErr(err)
168 case *uint16:
169 u64, err := strconv.ParseUint(value, 0, 16)
170 if err == nil {
171 *p = uint16(u64)
172 }
173 return strconvErr(err)
174 case *uint32:
175 u64, err := strconv.ParseUint(value, 0, 32)
176 if err == nil {
177 *p = uint32(u64)
178 }
179 return strconvErr(err)
180 case *uint64:
181 u64, err := strconv.ParseUint(value, 0, 64)
182 if err == nil {
183 *p = u64
184 }
185 return strconvErr(err)
186 case *float32:
187 f64, err := strconv.ParseFloat(value, 32)
188 if err == nil {
189 *p = float32(f64)
190 }
191 return strconvErr(err)
192 case *float64:
193 f64, err := strconv.ParseFloat(value, 64)
194 if err == nil {
195 *p = f64
196 }
197 return strconvErr(err)
198 case *time.Duration:
199 v, err := time.ParseDuration(value)
200 if err == nil {
201 *p = v
202 }
203 return err
204 }
205 panic("internal error")
206 }
207
208 func (g *generic) String() string {
209 switch p := g.p.(type) {
210 case *bool:
211 if *p {
212 return "true"
213 }
214 return "false"
215 case *string:
216 return *p
217 case *[]string:
218 return strings.Join([]string(*p), ",")
219 case *int:
220 return strconv.FormatInt(int64(*p), 10)
221 case *int8:
222 return strconv.FormatInt(int64(*p), 10)
223 case *int16:
224 return strconv.FormatInt(int64(*p), 10)
225 case *int32:
226 return strconv.FormatInt(int64(*p), 10)
227 case *int64:
228 return strconv.FormatInt(*p, 10)
229 case *uint:
230 return strconv.FormatUint(uint64(*p), 10)
231 case *uint8:
232 return strconv.FormatUint(uint64(*p), 10)
233 case *uint16:
234 return strconv.FormatUint(uint64(*p), 10)
235 case *uint32:
236 return strconv.FormatUint(uint64(*p), 10)
237 case *uint64:
238 return strconv.FormatUint(*p, 10)
239 case *float32:
240 return strconv.FormatFloat(float64(*p), 'g', -1, 32)
241 case *float64:
242 return strconv.FormatFloat(*p, 'g', -1, 64)
243 case *time.Duration:
244 return p.String()
245 }
246 panic("internal error")
247 }