|
2 | 2 | // Use of this source code is governed by a BSD-style |
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// Package pflag is a drop-in replacement for Go's flag package, implementing |
6 | | -// POSIX/GNU-style --flags. |
7 | | -// |
8 | | -// pflag is compatible with the GNU extensions to the POSIX recommendations |
9 | | -// for command-line options. See |
10 | | -// http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html |
11 | | -// |
12 | | -// Usage: |
13 | | -// |
14 | | -// pflag is a drop-in replacement of Go's native flag package. If you import |
15 | | -// pflag under the name "flag" then all code should continue to function |
16 | | -// with no changes. |
17 | | -// |
18 | | -// import flag "github.com/spf13/pflag" |
19 | | -// |
20 | | -// There is one exception to this: if you directly instantiate the Flag struct |
21 | | -// there is one more field "Shorthand" that you will need to set. |
22 | | -// Most code never instantiates this struct directly, and instead uses |
23 | | -// functions such as String(), BoolVar(), and Var(), and is therefore |
24 | | -// unaffected. |
25 | | -// |
26 | | -// Define flags using flag.String(), Bool(), Int(), etc. |
27 | | -// |
28 | | -// This declares an integer flag, -flagname, stored in the pointer ip, with type *int. |
29 | | -// |
30 | | -// var ip = flag.Int("flagname", 1234, "help message for flagname") |
31 | | -// |
32 | | -// If you like, you can bind the flag to a variable using the Var() functions. |
33 | | -// |
34 | | -// var flagvar int |
35 | | -// func init() { |
36 | | -// flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") |
37 | | -// } |
38 | | -// |
39 | | -// Or you can create custom flags that satisfy the Value interface (with |
40 | | -// pointer receivers) and couple them to flag parsing by |
41 | | -// |
42 | | -// flag.Var(&flagVal, "name", "help message for flagname") |
43 | | -// |
44 | | -// For such flags, the default value is just the initial value of the variable. |
45 | | -// |
46 | | -// After all flags are defined, call |
47 | | -// |
48 | | -// flag.Parse() |
49 | | -// |
50 | | -// to parse the command line into the defined flags. |
51 | | -// |
52 | | -// Flags may then be used directly. If you're using the flags themselves, |
53 | | -// they are all pointers; if you bind to variables, they're values. |
54 | | -// |
55 | | -// fmt.Println("ip has value ", *ip) |
56 | | -// fmt.Println("flagvar has value ", flagvar) |
57 | | -// |
58 | | -// After parsing, the arguments after the flag are available as the |
59 | | -// slice flag.Args() or individually as flag.Arg(i). |
60 | | -// The arguments are indexed from 0 through flag.NArg()-1. |
61 | | -// |
62 | | -// The pflag package also defines some new functions that are not in flag, |
63 | | -// that give one-letter shorthands for flags. You can use these by appending |
64 | | -// 'P' to the name of any function that defines a flag. |
65 | | -// |
66 | | -// var ip = flag.IntP("flagname", "f", 1234, "help message") |
67 | | -// var flagvar bool |
68 | | -// func init() { |
69 | | -// flag.BoolVarP(&flagvar, "boolname", "b", true, "help message") |
70 | | -// } |
71 | | -// flag.VarP(&flagval, "varname", "v", "help message") |
72 | | -// |
73 | | -// Shorthand letters can be used with single dashes on the command line. |
74 | | -// Boolean shorthand flags can be combined with other shorthand flags. |
75 | | -// |
76 | | -// Command line flag syntax: |
77 | | -// |
78 | | -// --flag // boolean flags only |
79 | | -// --flag=x |
80 | | -// |
81 | | -// Unlike the flag package, a single dash before an option means something |
82 | | -// different than a double dash. Single dashes signify a series of shorthand |
83 | | -// letters for flags. All but the last shorthand letter must be boolean flags. |
84 | | -// |
85 | | -// // boolean flags |
86 | | -// -f |
87 | | -// -abc |
88 | | -// // non-boolean flags |
89 | | -// -n 1234 |
90 | | -// -Ifile |
91 | | -// // mixed |
92 | | -// -abcs "hello" |
93 | | -// -abcn1234 |
94 | | -// |
95 | | -// Flag parsing stops after the terminator "--". Unlike the flag package, |
96 | | -// flags can be interspersed with arguments anywhere on the command line |
97 | | -// before this terminator. |
98 | | -// |
99 | | -// Integer flags accept 1234, 0664, 0x1234 and may be negative. |
100 | | -// Boolean flags (in their long form) accept 1, 0, t, f, true, false, |
101 | | -// TRUE, FALSE, True, False. |
102 | | -// Duration flags accept any input valid for time.ParseDuration. |
103 | | -// |
104 | | -// The default set of command-line flags is controlled by |
105 | | -// top-level functions. The FlagSet type allows one to define |
106 | | -// independent sets of flags, such as to implement subcommands |
107 | | -// in a command-line interface. The methods of FlagSet are |
108 | | -// analogous to the top-level functions for the command-line |
109 | | -// flag set. |
| 5 | +/* |
| 6 | +Package pflag is a drop-in replacement for Go's flag package, implementing |
| 7 | +POSIX/GNU-style --flags. |
| 8 | +
|
| 9 | +pflag is compatible with the GNU extensions to the POSIX recommendations |
| 10 | +for command-line options. See |
| 11 | +http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html |
| 12 | +
|
| 13 | +Usage: |
| 14 | +
|
| 15 | +pflag is a drop-in replacement of Go's native flag package. If you import |
| 16 | +pflag under the name "flag" then all code should continue to function |
| 17 | +with no changes. |
| 18 | +
|
| 19 | + import flag "github.com/spf13/pflag" |
| 20 | +
|
| 21 | +There is one exception to this: if you directly instantiate the Flag struct |
| 22 | +there is one more field "Shorthand" that you will need to set. |
| 23 | +Most code never instantiates this struct directly, and instead uses |
| 24 | +functions such as String(), BoolVar(), and Var(), and is therefore |
| 25 | +unaffected. |
| 26 | +
|
| 27 | +Define flags using flag.String(), Bool(), Int(), etc. |
| 28 | +
|
| 29 | +This declares an integer flag, -flagname, stored in the pointer ip, with type *int. |
| 30 | +
|
| 31 | + var ip = flag.Int("flagname", 1234, "help message for flagname") |
| 32 | +
|
| 33 | +If you like, you can bind the flag to a variable using the Var() functions. |
| 34 | +
|
| 35 | + var flagvar int |
| 36 | + func init() { |
| 37 | + flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") |
| 38 | + } |
| 39 | +
|
| 40 | +Or you can create custom flags that satisfy the Value interface (with |
| 41 | +pointer receivers) and couple them to flag parsing by |
| 42 | +
|
| 43 | + flag.Var(&flagVal, "name", "help message for flagname") |
| 44 | +
|
| 45 | +For such flags, the default value is just the initial value of the variable. |
| 46 | +
|
| 47 | +After all flags are defined, call |
| 48 | +
|
| 49 | + flag.Parse() |
| 50 | +
|
| 51 | +to parse the command line into the defined flags. |
| 52 | +
|
| 53 | +Flags may then be used directly. If you're using the flags themselves, |
| 54 | +they are all pointers; if you bind to variables, they're values. |
| 55 | +
|
| 56 | + fmt.Println("ip has value ", *ip) |
| 57 | + fmt.Println("flagvar has value ", flagvar) |
| 58 | +
|
| 59 | +After parsing, the arguments after the flag are available as the |
| 60 | +slice flag.Args() or individually as flag.Arg(i). |
| 61 | +The arguments are indexed from 0 through flag.NArg()-1. |
| 62 | +
|
| 63 | +The pflag package also defines some new functions that are not in flag, |
| 64 | +that give one-letter shorthands for flags. You can use these by appending |
| 65 | +'P' to the name of any function that defines a flag. |
| 66 | +
|
| 67 | + var ip = flag.IntP("flagname", "f", 1234, "help message") |
| 68 | + var flagvar bool |
| 69 | + func init() { |
| 70 | + flag.BoolVarP(&flagvar, "boolname", "b", true, "help message") |
| 71 | + } |
| 72 | + flag.VarP(&flagval, "varname", "v", "help message") |
| 73 | +
|
| 74 | +Shorthand letters can be used with single dashes on the command line. |
| 75 | +Boolean shorthand flags can be combined with other shorthand flags. |
| 76 | +
|
| 77 | +Command line flag syntax: |
| 78 | +
|
| 79 | + --flag // boolean flags only |
| 80 | + --flag=x |
| 81 | +
|
| 82 | +Unlike the flag package, a single dash before an option means something |
| 83 | +different than a double dash. Single dashes signify a series of shorthand |
| 84 | +letters for flags. All but the last shorthand letter must be boolean flags. |
| 85 | +
|
| 86 | + // boolean flags |
| 87 | + -f |
| 88 | + -abc |
| 89 | + // non-boolean flags |
| 90 | + -n 1234 |
| 91 | + -Ifile |
| 92 | + // mixed |
| 93 | + -abcs "hello" |
| 94 | + -abcn1234 |
| 95 | +
|
| 96 | +Flag parsing stops after the terminator "--". Unlike the flag package, |
| 97 | +flags can be interspersed with arguments anywhere on the command line |
| 98 | +before this terminator. |
| 99 | +
|
| 100 | +Integer flags accept 1234, 0664, 0x1234 and may be negative. |
| 101 | +Boolean flags (in their long form) accept 1, 0, t, f, true, false, |
| 102 | +TRUE, FALSE, True, False. |
| 103 | +Duration flags accept any input valid for time.ParseDuration. |
| 104 | +
|
| 105 | +The default set of command-line flags is controlled by |
| 106 | +top-level functions. The FlagSet type allows one to define |
| 107 | +independent sets of flags, such as to implement subcommands |
| 108 | +in a command-line interface. The methods of FlagSet are |
| 109 | +analogous to the top-level functions for the command-line |
| 110 | +flag set. |
| 111 | +*/ |
110 | 112 | package pflag |
111 | 113 |
|
112 | 114 | import ( |
|
0 commit comments