Debian Codegraph @

README.md

raw symbols SCIP
1 # getopt ![build status](https://travis-ci.org/pborman/getopt.svg?branch=master)
2
3 Package getopt provides traditional getopt processing for implementing
4 commands that use traditional command lines. The standard Go flag package
5 cannot be used to write a program that parses flags the way ls or ssh does,
6 for example. There are two versions, v1 and v2, both named getopt, that
7 use the following import paths:
8
9 ```
10 "github.com/pborman/getopt" // version 1
11 "github.com/pborman/getopt/v2" // version 2
12 ```
13
14 This README describes version 2 of the package, which has a simplified API.
15
16 ## Usage
17
18 Getopt supports functionality found in both the standard BSD getopt as well
19 as (one of the many versions of) the GNU getopt_long. Being a Go package,
20 this package makes common usage easy, but still enables more controlled usage
21 if needed.
22
23 Typical usage:
24
25 ```
26 Declare flags and have getopt return pointers to the values.
27 helpFlag := getopt.Bool('?', "display help")
28 cmdFlag := getopt.StringLong("command", 'c', "default", "the command")
29
30 Declare flags against existing variables.
31 var (
32 fileName = "/the/default/path"
33 timeout = time.Second * 5
34 verbose bool
35 )
36 func init() {
37 getopt.Flag(&verbose, 'v', "be verbose")
38 getopt.FlagLong(&fileName, "path", 0, "the path")
39 getopt.FlagLong(&timeout, "timeout", 't', "some timeout")
40 }
41
42 func main() {
43 Parse the program arguments
44 getopt.Parse()
45 Get the remaining positional parameters
46 args := getopt.Args()
47 ...
48 ```
49
50 If you don't want the program to exit on error, use getopt.Getopt:
51
52 ```
53 err := getopt.Getopt(nil)
54 if err != nil {
55 code to handle error
56 fmt.Fprintln(os.Stderr, err)
57 }
58 ```
59
60 ## Flag Syntax
61
62 Support is provided for both short (-f) and long (--flag) options. A single
63 option may have both a short and a long name. Each option may be a flag or a
64 value. A value takes an argument.
65
66 Declaring no long names causes this package to process arguments like the
67 traditional BSD getopt.
68
69 Short flags may be combined into a single parameter. For example, "-a -b -c"
70 may also be expressed "-abc". Long flags must stand on their own "--alpha
71 --beta"
72
73 Values require an argument. For short options the argument may either be
74 immediately following the short name or as the next argument. Only one short
75 value may be combined with short flags in a single argument; the short value
76 must be after all short flags. For example, if f is a flag and v is a value,
77 then:
78
79 ```
80 -vvalue (sets v to "value")
81 -v value (sets v to "value")
82 -fvvalue (sets f, and sets v to "value")
83 -fv value (sets f, and sets v to "value")
84 -vf value (set v to "f" and value is the first parameter)
85 ```
86
87 For the long value option val:
88
89 ```
90 --val value (sets val to "value")
91 --val=value (sets val to "value")
92 --valvalue (invalid option "valvalue")
93 ```
94
95 Values with an optional value only set the value if the value is part of the
96 same argument. In any event, the option count is increased and the option is
97 marked as seen.
98
99 ```
100 -v -f (sets v and f as being seen)
101 -vvalue -f (sets v to "value" and sets f)
102 --val -f (sets v and f as being seen)
103 --val=value -f (sets v to "value" and sets f)
104 ```
105
106 There is no convience function defined for making the value optional. The
107 SetOptional method must be called on the actual Option.
108
109 ```
110 v := String("val", 'v', "", "the optional v")
111 Lookup("v").SetOptional()
112
113 var s string
114 FlagLong(&s, "val", 'v', "the optional v).SetOptional()
115 ```
116
117 Parsing continues until the first non-option or "--" is encountered.
118
119 The short name "-" can be used, but it either is specified as "-" or as part
120 of a group of options, for example "-f-". If there are no long options
121 specified then "--f" could also be used. If "-" is not declared as an option
122 then the single "-" will also terminate the option processing but unlike
123 "--", the "-" will be part of the remaining arguments.
124
125 ## Advanced Usage
126
127 Normally the parsing is performed by calling the Parse function. If it is
128 important to see the order of the options then the Getopt function should be
129 used. The standard Parse function does the equivalent of:
130
131 ```
132 func Parse() {
133 if err := getopt.Getopt(os.Args, nil); err != nil {
134 fmt.Fprintln(os.Stderr, err)
135 s.usage()
136 os.Exit(1)
137 }
138 }
139 ```
140
141 When calling Getopt it is the responsibility of the caller to print any
142 errors.
143
144 Normally the default option set, CommandLine, is used. Other option sets may
145 be created with New.
146
147 After parsing, the sets Args will contain the non-option arguments. If an
148 error is encountered then Args will begin with argument that caused the
149 error.
150
151 It is valid to call a set's Parse a second time to amend the current set of
152 flags or values. As an example:
153
154 ```
155 var a = getopt.Bool('a', "", "The a flag")
156 var b = getopt.Bool('b', "", "The a flag")
157 var cmd = ""
158
159 var opts = getopt.CommandLine
160
161 opts.Parse(os.Args)
162 if opts.NArgs() > 0 {
163 cmd = opts.Arg(0)
164 opts.Parse(opts.Args())
165 }
166 ```
167
168 If called with set to { "prog", "-a", "cmd", "-b", "arg" } then both a and
169 b would be set, cmd would be set to "cmd", and opts.Args() would return {
170 "arg" }.
171
172 Unless an option type explicitly prohibits it, an option may appear more than
173 once in the arguments. The last value provided to the option is the value.
174
175 ## Builtin Types
176
177 The Flag and FlagLong functions support most standard Go types. For the
178 list, see the description of FlagLong below for a list of supported types.
179
180 There are also helper routines to allow single line flag declarations. These
181 types are: Bool, Counter, Duration, Enum, Int16, Int32, Int64, Int, List,
182 Signed, String, Uint16, Uint32, Uint64, Uint, and Unsigned.
183
184 Each comes in a short and long flavor, e.g., Bool and BoolLong and include
185 functions to set the flags on the standard command line or for a specific Set
186 of flags.
187
188 Except for the Counter, Enum, Signed and Unsigned types, all of these types
189 can be declared using Flag and FlagLong by passing in a pointer to the
190 appropriate type.
191
192 ## Declaring New Flag Types
193
194 A pointer to any type that implements the Value interface may be passed to
195 Flag or FlagLong.
196
197 ## VALUEHELP
198
199 All non-flag options are created with a "valuehelp" as the last parameter.
200 Valuehelp should be 0, 1, or 2 strings. The first string, if provided, is
201 the usage message for the option. If the second string, if provided, is the
202 name to use for the value when displaying the usage. If not provided the
203 term "value" is assumed.
204
205 The usage message for the option created with
206
207 ```
208 StringLong("option", 'o', "defval", "a string of letters")
209 ```
210
211 is
212
213 ```
214 -o, -option=value
215 ```
216 while the usage message for the option created with
217
218 ```
219 StringLong("option", 'o', "defval", "a string of letters", "string")
220 ```
221
222 is
223
224 ```
225 -o, -option=string
226 ```