Debian Codegraph @

v2/option.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 "strings"
10 )
11
12 // An Option can be either a Flag or a Value
13 type Option interface {
14 // Name returns the name of the option. If the option has been seen
15 // then the last way it was referenced (short or long) is returned
16 // otherwise if there is a short name then this will be the short name
17 // as a string, else it will be the long name.
18 Name() string
19
20 // ShortName always returns the short name of the option, or "" if there
21 // is no short name. The name does not include the "-".
22 ShortName() string
23
24 // LongName always returns the long name of the option, or "" if there
25 // is no long name. The name does not include the "--".
26 LongName() string
27
28 // Help always returns a description of the option, or "" if there
29 // is no description.
30 Help() string
31
32 // IsFlag returns true if Option is a flag.
33 IsFlag() bool
34
35 // Seen returns true if the flag was seen.
36 Seen() bool
37
38 // Count returns the number of times the flag was seen.
39 Count() int
40
41 // String returns the last value the option was set to.
42 String() string
43
44 // Value returns the Value of the option.
45 Value() Value
46
47 // SetOptional makes the value optional. The option and value are
48 // always a single argument. Either --option or --option=value. In
49 // the former case the value of the option does not change but the Set()
50 // will return true and the value returned by Count() is incremented.
51 // The short form is either -o or -ovalue. SetOptional returns
52 // the Option
53 SetOptional() Option
54
55 // SetFlag makes the value a flag. Flags are boolean values and
56 // normally do not taken a value. They are set to true when seen.
57 // If a value is passed in the long form then it must be on, case
58 // insensitivinsensitive, one of "true", "false", "t", "f", "on", "off", "1", "0".
59 // SetFlag returns the Option
60 SetFlag() Option
61
62 // Reset resets the state of the option so it appears it has not
63 // yet been seen, including resetting the value of the option
64 // to its original default state.
65 Reset()
66
67 // Mandataory sets the mandatory flag of the option. Parse will
68 // fail if a mandatory option is missing.
69 Mandatory() Option
70
71 // SetGroup sets the option as part of a radio group. Parse will
72 // fail if two options in the same group are seen.
73 SetGroup(string) Option
74 }
75
76 type option struct {
77 short rune // 0 means no short name
78 long string // "" means no long name
79 isLong bool // True if they used the long name
80 flag bool // true if a boolean flag
81 defval string // default value
82 optional bool // true if we take an optional value
83 help string // help message
84 where string // file where the option was defined
85 value Value // current value of option
86 count int // number of times we have seen this option
87 name string // name of the value (for usage)
88 uname string // name of the option (for usage)
89 mandatory bool // this option must be specified
90 group string // mutual exclusion group
91 }
92
93 // usageName returns the name of the option for printing usage lines in one
94 // of the following forms:
95 //
96 // -f
97 // --flag
98 // -f, --flag
99 // -s value
100 // --set=value
101 // -s, --set=value
102 func (o *option) usageName() string {
103 // Don't print help messages if we have none and there is only one
104 // way to specify the option.
105 if o.help == "" && (o.short == 0 || o.long == "") {
106 return ""
107 }
108 n := ""
109
110 switch {
111 case o.short != 0 && o.long == "":
112 n = "-" + string(o.short)
113 case o.short == 0 && o.long != "":
114 n = " --" + o.long
115 case o.short != 0 && o.long != "":
116 n = "-" + string(o.short) + ", --" + o.long
117 }
118
119 switch {
120 case o.flag:
121 return n
122 case o.optional:
123 return n + "[=" + o.name + "]"
124 case o.long != "":
125 return n + "=" + o.name
126 }
127 return n + " " + o.name
128 }
129
130 // sortName returns the name to sort the option on.
131 func (o *option) sortName() string {
132 if o.short != 0 {
133 return string(o.short) + o.long
134 }
135 return o.long[:1] + o.long
136 }
137
138 func (o *option) Seen() bool { return o.count > 0 }
139 func (o *option) Count() int { return o.count }
140 func (o *option) IsFlag() bool { return o.flag }
141 func (o *option) String() string { return o.value.String() }
142 func (o *option) SetOptional() Option { o.optional = true; return o }
143 func (o *option) SetFlag() Option { o.flag = true; return o }
144 func (o *option) Mandatory() Option { o.mandatory = true; return o }
145 func (o *option) SetGroup(g string) Option { o.group = g; return o }
146
147 func (o *option) Value() Value {
148 if o == nil {
149 return nil
150 }
151 return o.value
152 }
153
154 func (o *option) Name() string {
155 if !o.isLong && o.short != 0 {
156 return "-" + string(o.short)
157 }
158 return "--" + o.long
159 }
160
161 func (o *option) ShortName() string {
162 if o.short != 0 {
163 return string(o.short)
164 }
165 return ""
166 }
167
168 func (o *option) LongName() string {
169 return o.long
170 }
171
172 func (o *option) Help() string {
173 return o.help
174 }
175
176 // Reset rests an option so that it appears it has not yet been seen.
177 func (o *option) Reset() {
178 o.isLong = false
179 o.count = 0
180 o.value.Set(o.defval, o)
181 }
182
183 type optionList []*option
184
185 func (ol optionList) Len() int { return len(ol) }
186 func (ol optionList) Swap(i, j int) { ol[i], ol[j] = ol[j], ol[i] }
187 func (ol optionList) Less(i, j int) bool {
188 // first check the short names (or the first letter of the long name)
189 // If they are not equal (case insensitive) then we have our answer
190 n1 := ol[i].sortName()
191 n2 := ol[j].sortName()
192 l1 := strings.ToLower(n1)
193 l2 := strings.ToLower(n2)
194 if l1 < l2 {
195 return true
196 }
197 if l2 < l1 {
198 return false
199 }
200 return n1 < n2
201 }
202
203 // AddOption add the option o to set CommandLine if o is not already in set
204 // CommandLine.
205 func AddOption(o Option) {
206 CommandLine.AddOption(o)
207 }
208
209 // AddOption add the option o to set s if o is not already in set s.
210 func (s *Set) AddOption(o Option) {
211 opt := o.(*option)
212 for _, eopt := range s.options {
213 if opt == eopt {
214 return
215 }
216 }
217 if opt.short != 0 {
218 if oo, ok := s.shortOptions[opt.short]; ok {
219 fmt.Fprintf(stderr, "%s: -%c already declared at %s\n", opt.where, opt.short, oo.where)
220 exit(1)
221 }
222 s.shortOptions[opt.short] = opt
223 }
224 if opt.long != "" {
225 if oo, ok := s.longOptions[opt.long]; ok {
226 fmt.Fprintf(stderr, "%s: --%s already declared at %s\n", opt.where, opt.long, oo.where)
227 exit(1)
228 }
229 s.longOptions[opt.long] = opt
230 }
231 s.options = append(s.options, opt)
232 }