• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..02-Oct-2018-

v2/H02-Oct-2018-3,8402,956

AUTHORSH A D02-Oct-201830 21

CONTRIBUTING.mdH A D02-Oct-2018312 116

LICENSEH A D02-Oct-20181.4 KiB2824

README.mdH A D02-Oct-20186.5 KiB227172

bool.goH A D02-Oct-20181.9 KiB7551

bool_test.goH A D02-Oct-20181.7 KiB10798

breakup_test.goH A D02-Oct-2018965 3527

counter.goH A D02-Oct-20182.1 KiB8257

counter_test.goH A D02-Oct-20181.4 KiB9282

duration.goH A D02-Oct-20181.8 KiB5739

duration_test.goH A D02-Oct-20181.3 KiB7465

enum.goH A D02-Oct-20182.3 KiB7451

enum_test.goH A D02-Oct-20181 KiB6758

error.goH A D02-Oct-20182.4 KiB9468

getopt.goH A D02-Oct-201814.5 KiB537246

int.goH A D02-Oct-20181.8 KiB6850

int16.goH A D02-Oct-20181.8 KiB6850

int16_test.goH A D02-Oct-20181.3 KiB8576

int32.goH A D02-Oct-20181.8 KiB6850

int32_test.goH A D02-Oct-20181.3 KiB8576

int64.goH A D02-Oct-20181.8 KiB6850

int64_test.goH A D02-Oct-20181.3 KiB8576

int_test.goH A D02-Oct-20181.3 KiB8576

list.goH A D02-Oct-20182.1 KiB7043

list_test.goH A D02-Oct-20181.8 KiB10087

option.goH A D02-Oct-20185.2 KiB194124

set.goH A D02-Oct-20187.8 KiB269150

signed.goH A D02-Oct-20183.4 KiB11188

signed_test.goH A D02-Oct-20181.5 KiB9889

string.goH A D02-Oct-20181.6 KiB5436

string_test.goH A D02-Oct-20181.3 KiB7869

uint.goH A D02-Oct-20181.8 KiB6850

uint16.goH A D02-Oct-20181.9 KiB6850

uint16_test.goH A D02-Oct-20181.3 KiB8576

uint32.goH A D02-Oct-20181.9 KiB6850

uint32_test.goH A D02-Oct-20181.3 KiB8576

uint64.goH A D02-Oct-20181.9 KiB6850

uint64_test.goH A D02-Oct-20181.3 KiB8576

uint_test.goH A D02-Oct-20181.3 KiB8576

unsigned.goH A D02-Oct-20183.4 KiB11288

unsigned_test.goH A D02-Oct-20181.5 KiB9889

util_test.goH A D02-Oct-20181.7 KiB8872

var.goH A D02-Oct-20181.7 KiB6445

README.md

1# getopt
2
3Package getopt provides traditional getopt processing for implementing
4commands that use traditional command lines.  The standard Go flag package
5cannot be used to write a program that parses flags the way ls or ssh does,
6for example.  There are two versions, v1 and v2, both named getopt, that
7use the following import paths:
8
9```
10	"github.com/pborman/getopt"     // version 1
11	"github.com/pborman/getopt/v2"  // version 2
12```
13
14This README describes version 2 of the package, which has a simplified API.
15
16## Usage
17
18Getopt supports functionality found in both the standard BSD getopt as well
19as (one of the many versions of) the GNU getopt_long.  Being a Go package,
20this package makes common usage easy, but still enables more controlled usage
21if needed.
22
23Typical 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
50If 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
62Support is provided for both short (-f) and long (--flag) options.  A single
63option may have both a short and a long name.  Each option may be a flag or a
64value.  A value takes an argument.
65
66Declaring no long names causes this package to process arguments like the
67traditional BSD getopt.
68
69Short flags may be combined into a single parameter.  For example, "-a -b -c"
70may also be expressed "-abc".  Long flags must stand on their own "--alpha
71--beta"
72
73Values require an argument.  For short options the argument may either be
74immediately following the short name or as the next argument.  Only one short
75value may be combined with short flags in a single argument; the short value
76must be after all short flags.  For example, if f is a flag and v is a value,
77then:
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
87For 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
95Values with an optional value only set the value if the value is part of the
96same argument.  In any event, the option count is increased and the option is
97marked 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
106There is no convience function defined for making the value optional.  The
107SetOptional 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
117Parsing continues until the first non-option or "--" is encountered.
118
119The short name "-" can be used, but it either is specified as "-" or as part
120of a group of options, for example "-f-".  If there are no long options
121specified then "--f" could also be used.  If "-" is not declared as an option
122then the single "-" will also terminate the option processing but unlike
123"--", the "-" will be part of the remaining arguments.
124
125## Advanced Usage
126
127Normally the parsing is performed by calling the Parse function.  If it is
128important to see the order of the options then the Getopt function should be
129used.  The standard Parse function does the equivalent of:
130
131```
132func 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
141When calling Getopt it is the responsibility of the caller to print any
142errors.
143
144Normally the default option set, CommandLine, is used.  Other option sets may
145be created with New.
146
147After parsing, the sets Args will contain the non-option arguments.  If an
148error is encountered then Args will begin with argument that caused the
149error.
150
151It is valid to call a set's Parse a second time to amend the current set of
152flags 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
168If called with set to { "prog", "-a", "cmd", "-b", "arg" } then both and and
169b would be set, cmd would be set to "cmd", and opts.Args() would return {
170"arg" }.
171
172Unless an option type explicitly prohibits it, an option may appear more than
173once in the arguments.  The last value provided to the option is the value.
174
175## Builtin Types
176
177The Flag and FlagLong functions support most standard Go types.  For the
178list, see the description of FlagLong below for a list of supported types.
179
180There are also helper routines to allow single line flag declarations.  These
181types are: Bool, Counter, Duration, Enum, Int16, Int32, Int64, Int, List,
182Signed, String, Uint16, Uint32, Uint64, Uint, and Unsigned.
183
184Each comes in a short and long flavor, e.g., Bool and BoolLong and include
185functions to set the flags on the standard command line or for a specific Set
186of flags.
187
188Except for the Counter, Enum, Signed and Unsigned types, all of these types
189can be declared using Flag and FlagLong by passing in a pointer to the
190appropriate type.
191
192## Declaring New Flag Types
193
194A pointer to any type that implements the Value interface may be passed to
195Flag or FlagLong.
196
197## VALUEHELP
198
199All non-flag options are created with a "valuehelp" as the last parameter.
200Valuehelp should be 0, 1, or 2 strings.  The first string, if provided, is
201the usage message for the option.  If the second string, if provided, is the
202name to use for the value when displaying the usage.  If not provided the
203term "value" is assumed.
204
205The usage message for the option created with
206
207```
208	StringLong("option", 'o', "defval", "a string of letters")
209```
210
211is
212
213```
214	-o, -option=value
215```
216while the usage message for the option created with
217
218```
219	StringLong("option", 'o', "defval", "a string of letters", "string")
220```
221
222is
223
224```
225	-o, -option=string
226```
227