├── .travis.yml ├── LICENSE ├── README.md ├── bool.go ├── bool_test.go ├── duration.go ├── example_test.go ├── export_test.go ├── flag.go ├── flag_test.go ├── float32.go ├── float64.go ├── int.go ├── int32.go ├── int64.go ├── int8.go ├── ip.go ├── ipmask.go ├── string.go ├── uint.go ├── uint16.go ├── uint32.go ├── uint64.go └── uint8.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | arch: 3 | - ppc64le 4 | - amd64 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Alex Ogier. All rights reserved. 2 | Copyright (c) 2012 The Go Authors. All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above 11 | copyright notice, this list of conditions and the following disclaimer 12 | in the documentation and/or other materials provided with the 13 | distribution. 14 | * Neither the name of Google Inc. nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/ogier/pflag.png?branch=master)](https://travis-ci.org/ogier/pflag) 2 | 3 | ## Description 4 | 5 | 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][1]. For a more precise description, see the 10 | "Command-line flag syntax" section below. 11 | 12 | [1]: http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html 13 | 14 | pflag is available under the same style of BSD license as the Go language, 15 | which can be found in the LICENSE file. 16 | 17 | ## Installation 18 | 19 | pflag is available using the standard `go get` command. 20 | 21 | Install by running: 22 | 23 | go get github.com/ogier/pflag 24 | 25 | Run tests by running: 26 | 27 | go test github.com/ogier/pflag 28 | 29 | ## Usage 30 | 31 | pflag is a drop-in replacement of Go's native flag package. If you import 32 | pflag under the name "flag" then all code should continue to function 33 | with no changes. 34 | 35 | ``` go 36 | import flag "github.com/ogier/pflag" 37 | ``` 38 | 39 | There is one exception to this: if you directly instantiate the Flag struct 40 | there is one more field "Shorthand" that you will need to set. 41 | Most code never instantiates this struct directly, and instead uses 42 | functions such as String(), BoolVar(), and Var(), and is therefore 43 | unaffected. 44 | 45 | Define flags using flag.String(), Bool(), Int(), etc. 46 | 47 | This declares an integer flag, -flagname, stored in the pointer ip, with type *int. 48 | 49 | ``` go 50 | var ip *int = flag.Int("flagname", 1234, "help message for flagname") 51 | ``` 52 | 53 | If you like, you can bind the flag to a variable using the Var() functions. 54 | 55 | ``` go 56 | var flagvar int 57 | func init() { 58 | flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") 59 | } 60 | ``` 61 | 62 | Or you can create custom flags that satisfy the Value interface (with 63 | pointer receivers) and couple them to flag parsing by 64 | 65 | ``` go 66 | flag.Var(&flagVal, "name", "help message for flagname") 67 | ``` 68 | 69 | For such flags, the default value is just the initial value of the variable. 70 | 71 | After all flags are defined, call 72 | 73 | ``` go 74 | flag.Parse() 75 | ``` 76 | 77 | to parse the command line into the defined flags. 78 | 79 | Flags may then be used directly. If you're using the flags themselves, 80 | they are all pointers; if you bind to variables, they're values. 81 | 82 | ``` go 83 | fmt.Println("ip has value ", *ip) 84 | fmt.Println("flagvar has value ", flagvar) 85 | ``` 86 | 87 | After parsing, the arguments after the flag are available as the 88 | slice flag.Args() or individually as flag.Arg(i). 89 | The arguments are indexed from 0 through flag.NArg()-1. 90 | 91 | The pflag package also defines some new functions that are not in flag, 92 | that give one-letter shorthands for flags. You can use these by appending 93 | 'P' to the name of any function that defines a flag. 94 | 95 | ``` go 96 | var ip = flag.IntP("flagname", "f", 1234, "help message") 97 | var flagvar bool 98 | func init() { 99 | flag.BoolVarP("boolname", "b", true, "help message") 100 | } 101 | flag.VarP(&flagVar, "varname", "v", 1234, "help message") 102 | ``` 103 | 104 | Shorthand letters can be used with single dashes on the command line. 105 | Boolean shorthand flags can be combined with other shorthand flags. 106 | 107 | The default set of command-line flags is controlled by 108 | top-level functions. The FlagSet type allows one to define 109 | independent sets of flags, such as to implement subcommands 110 | in a command-line interface. The methods of FlagSet are 111 | analogous to the top-level functions for the command-line 112 | flag set. 113 | 114 | ## Command line flag syntax 115 | 116 | ``` 117 | --flag // boolean flags only 118 | --flag=x 119 | ``` 120 | 121 | Unlike the flag package, a single dash before an option means something 122 | different than a double dash. Single dashes signify a series of shorthand 123 | letters for flags. All but the last shorthand letter must be boolean flags. 124 | 125 | ``` 126 | // boolean flags 127 | -f 128 | -abc 129 | 130 | // non-boolean flags 131 | -n 1234 132 | -Ifile 133 | 134 | // mixed 135 | -abcs "hello" 136 | -abcn1234 137 | ``` 138 | 139 | Flag parsing stops after the terminator "--". Unlike the flag package, 140 | flags can be interspersed with arguments anywhere on the command line 141 | before this terminator. 142 | 143 | Integer flags accept 1234, 0664, 0x1234 and may be negative. 144 | Boolean flags (in their long form) accept 1, 0, t, f, true, false, 145 | TRUE, FALSE, True, False. 146 | Duration flags accept any input valid for time.ParseDuration. 147 | 148 | ## More info 149 | 150 | You can see the full reference documentation of the pflag package 151 | [at godoc.org][3], or through go's standard documentation system by 152 | running `godoc -http=:6060` and browsing to 153 | [http://localhost:6060/pkg/github.com/ogier/pflag][2] after 154 | installation. 155 | 156 | [2]: http://localhost:6060/pkg/github.com/ogier/pflag 157 | [3]: http://godoc.org/github.com/ogier/pflag 158 | -------------------------------------------------------------------------------- /bool.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // optional interface to indicate boolean flags that can be 9 | // supplied without "=value" text 10 | type boolFlag interface { 11 | Value 12 | IsBoolFlag() bool 13 | } 14 | 15 | // -- bool Value 16 | type boolValue bool 17 | 18 | func newBoolValue(val bool, p *bool) *boolValue { 19 | *p = val 20 | return (*boolValue)(p) 21 | } 22 | 23 | func (b *boolValue) Set(s string) error { 24 | v, err := strconv.ParseBool(s) 25 | *b = boolValue(v) 26 | return err 27 | } 28 | 29 | func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) } 30 | 31 | func (b *boolValue) IsBoolFlag() bool { return true } 32 | 33 | // BoolVar defines a bool flag with specified name, default value, and usage string. 34 | // The argument p points to a bool variable in which to store the value of the flag. 35 | func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) { 36 | f.VarP(newBoolValue(value, p), name, "", usage) 37 | } 38 | 39 | // Like BoolVar, but accepts a shorthand letter that can be used after a single dash. 40 | func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) { 41 | f.VarP(newBoolValue(value, p), name, shorthand, usage) 42 | } 43 | 44 | // BoolVar defines a bool flag with specified name, default value, and usage string. 45 | // The argument p points to a bool variable in which to store the value of the flag. 46 | func BoolVar(p *bool, name string, value bool, usage string) { 47 | CommandLine.VarP(newBoolValue(value, p), name, "", usage) 48 | } 49 | 50 | // Like BoolVar, but accepts a shorthand letter that can be used after a single dash. 51 | func BoolVarP(p *bool, name, shorthand string, value bool, usage string) { 52 | CommandLine.VarP(newBoolValue(value, p), name, shorthand, usage) 53 | } 54 | 55 | // Bool defines a bool flag with specified name, default value, and usage string. 56 | // The return value is the address of a bool variable that stores the value of the flag. 57 | func (f *FlagSet) Bool(name string, value bool, usage string) *bool { 58 | p := new(bool) 59 | f.BoolVarP(p, name, "", value, usage) 60 | return p 61 | } 62 | 63 | // Like Bool, but accepts a shorthand letter that can be used after a single dash. 64 | func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool { 65 | p := new(bool) 66 | f.BoolVarP(p, name, shorthand, value, usage) 67 | return p 68 | } 69 | 70 | // Bool defines a bool flag with specified name, default value, and usage string. 71 | // The return value is the address of a bool variable that stores the value of the flag. 72 | func Bool(name string, value bool, usage string) *bool { 73 | return CommandLine.BoolP(name, "", value, usage) 74 | } 75 | 76 | // Like Bool, but accepts a shorthand letter that can be used after a single dash. 77 | func BoolP(name, shorthand string, value bool, usage string) *bool { 78 | return CommandLine.BoolP(name, shorthand, value, usage) 79 | } 80 | -------------------------------------------------------------------------------- /bool_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. 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 pflag 6 | 7 | import ( 8 | "bytes" 9 | "fmt" 10 | "strconv" 11 | "testing" 12 | ) 13 | 14 | // This value can be a boolean ("true", "false") or "maybe" 15 | type triStateValue int 16 | 17 | const ( 18 | triStateFalse triStateValue = 0 19 | triStateTrue triStateValue = 1 20 | triStateMaybe triStateValue = 2 21 | ) 22 | 23 | const strTriStateMaybe = "maybe" 24 | 25 | func (v *triStateValue) IsBoolFlag() bool { 26 | return true 27 | } 28 | 29 | func (v *triStateValue) Get() interface{} { 30 | return triStateValue(*v) 31 | } 32 | 33 | func (v *triStateValue) Set(s string) error { 34 | if s == strTriStateMaybe { 35 | *v = triStateMaybe 36 | return nil 37 | } 38 | boolVal, err := strconv.ParseBool(s) 39 | if boolVal { 40 | *v = triStateTrue 41 | } else { 42 | *v = triStateFalse 43 | } 44 | return err 45 | } 46 | 47 | func (v *triStateValue) String() string { 48 | if *v == triStateMaybe { 49 | return strTriStateMaybe 50 | } 51 | return fmt.Sprintf("%v", bool(*v == triStateTrue)) 52 | } 53 | 54 | // The type of the flag as required by the pflag.Value interface 55 | func (v *triStateValue) Type() string { 56 | return "version" 57 | } 58 | 59 | func setUpFlagSet(tristate *triStateValue) *FlagSet { 60 | f := NewFlagSet("test", ContinueOnError) 61 | *tristate = triStateFalse 62 | f.VarP(tristate, "tristate", "t", "tristate value (true, maybe or false)") 63 | return f 64 | } 65 | 66 | func TestExplicitTrue(t *testing.T) { 67 | var tristate triStateValue 68 | f := setUpFlagSet(&tristate) 69 | err := f.Parse([]string{"--tristate=true"}) 70 | if err != nil { 71 | t.Fatal("expected no error; got", err) 72 | } 73 | if tristate != triStateTrue { 74 | t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") 75 | } 76 | } 77 | 78 | func TestImplicitTrue(t *testing.T) { 79 | var tristate triStateValue 80 | f := setUpFlagSet(&tristate) 81 | err := f.Parse([]string{"--tristate"}) 82 | if err != nil { 83 | t.Fatal("expected no error; got", err) 84 | } 85 | if tristate != triStateTrue { 86 | t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") 87 | } 88 | } 89 | 90 | func TestShortFlag(t *testing.T) { 91 | var tristate triStateValue 92 | f := setUpFlagSet(&tristate) 93 | err := f.Parse([]string{"-t"}) 94 | if err != nil { 95 | t.Fatal("expected no error; got", err) 96 | } 97 | if tristate != triStateTrue { 98 | t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") 99 | } 100 | } 101 | 102 | func TestShortFlagExtraArgument(t *testing.T) { 103 | var tristate triStateValue 104 | f := setUpFlagSet(&tristate) 105 | // The"maybe"turns into an arg, since short boolean options will only do true/false 106 | err := f.Parse([]string{"-t", "maybe"}) 107 | if err != nil { 108 | t.Fatal("expected no error; got", err) 109 | } 110 | if tristate != triStateTrue { 111 | t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") 112 | } 113 | args := f.Args() 114 | if len(args) != 1 || args[0] != "maybe" { 115 | t.Fatal("expected an extra 'maybe' argument to stick around") 116 | } 117 | } 118 | 119 | func TestExplicitMaybe(t *testing.T) { 120 | var tristate triStateValue 121 | f := setUpFlagSet(&tristate) 122 | err := f.Parse([]string{"--tristate=maybe"}) 123 | if err != nil { 124 | t.Fatal("expected no error; got", err) 125 | } 126 | if tristate != triStateMaybe { 127 | t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead") 128 | } 129 | } 130 | 131 | func TestExplicitFalse(t *testing.T) { 132 | var tristate triStateValue 133 | f := setUpFlagSet(&tristate) 134 | err := f.Parse([]string{"--tristate=false"}) 135 | if err != nil { 136 | t.Fatal("expected no error; got", err) 137 | } 138 | if tristate != triStateFalse { 139 | t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead") 140 | } 141 | } 142 | 143 | func TestImplicitFalse(t *testing.T) { 144 | var tristate triStateValue 145 | f := setUpFlagSet(&tristate) 146 | err := f.Parse([]string{}) 147 | if err != nil { 148 | t.Fatal("expected no error; got", err) 149 | } 150 | if tristate != triStateFalse { 151 | t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead") 152 | } 153 | } 154 | 155 | func TestInvalidValue(t *testing.T) { 156 | var tristate triStateValue 157 | f := setUpFlagSet(&tristate) 158 | var buf bytes.Buffer 159 | f.SetOutput(&buf) 160 | err := f.Parse([]string{"--tristate=invalid"}) 161 | if err == nil { 162 | t.Fatal("expected an error but did not get any, tristate has value", tristate) 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /duration.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import "time" 4 | 5 | // -- time.Duration Value 6 | type durationValue time.Duration 7 | 8 | func newDurationValue(val time.Duration, p *time.Duration) *durationValue { 9 | *p = val 10 | return (*durationValue)(p) 11 | } 12 | 13 | func (d *durationValue) Set(s string) error { 14 | v, err := time.ParseDuration(s) 15 | *d = durationValue(v) 16 | return err 17 | } 18 | 19 | func (d *durationValue) String() string { return (*time.Duration)(d).String() } 20 | 21 | // Value is the interface to the dynamic value stored in a flag. 22 | // (The default value is represented as a string.) 23 | type Value interface { 24 | String() string 25 | Set(string) error 26 | } 27 | 28 | // DurationVar defines a time.Duration flag with specified name, default value, and usage string. 29 | // The argument p points to a time.Duration variable in which to store the value of the flag. 30 | func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) { 31 | f.VarP(newDurationValue(value, p), name, "", usage) 32 | } 33 | 34 | // Like DurationVar, but accepts a shorthand letter that can be used after a single dash. 35 | func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { 36 | f.VarP(newDurationValue(value, p), name, shorthand, usage) 37 | } 38 | 39 | // DurationVar defines a time.Duration flag with specified name, default value, and usage string. 40 | // The argument p points to a time.Duration variable in which to store the value of the flag. 41 | func DurationVar(p *time.Duration, name string, value time.Duration, usage string) { 42 | CommandLine.VarP(newDurationValue(value, p), name, "", usage) 43 | } 44 | 45 | // Like DurationVar, but accepts a shorthand letter that can be used after a single dash. 46 | func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { 47 | CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage) 48 | } 49 | 50 | // Duration defines a time.Duration flag with specified name, default value, and usage string. 51 | // The return value is the address of a time.Duration variable that stores the value of the flag. 52 | func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration { 53 | p := new(time.Duration) 54 | f.DurationVarP(p, name, "", value, usage) 55 | return p 56 | } 57 | 58 | // Like Duration, but accepts a shorthand letter that can be used after a single dash. 59 | func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { 60 | p := new(time.Duration) 61 | f.DurationVarP(p, name, shorthand, value, usage) 62 | return p 63 | } 64 | 65 | // Duration defines a time.Duration flag with specified name, default value, and usage string. 66 | // The return value is the address of a time.Duration variable that stores the value of the flag. 67 | func Duration(name string, value time.Duration, usage string) *time.Duration { 68 | return CommandLine.DurationP(name, "", value, usage) 69 | } 70 | 71 | // Like Duration, but accepts a shorthand letter that can be used after a single dash. 72 | func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { 73 | return CommandLine.DurationP(name, shorthand, value, usage) 74 | } 75 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Go Authors. 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 | // These examples demonstrate more intricate uses of the flag package. 6 | package pflag_test 7 | 8 | import ( 9 | "errors" 10 | "fmt" 11 | "strings" 12 | "time" 13 | 14 | flag "github.com/ogier/pflag" 15 | ) 16 | 17 | // Example 1: A single string flag called "species" with default value "gopher". 18 | var species = flag.String("species", "gopher", "the species we are studying") 19 | 20 | // Example 2: A flag with a shorthand letter. 21 | var gopherType = flag.StringP("gopher_type", "g", "pocket", "the variety of gopher") 22 | 23 | // Example 3: A user-defined flag type, a slice of durations. 24 | type interval []time.Duration 25 | 26 | // String is the method to format the flag's value, part of the flag.Value interface. 27 | // The String method's output will be used in diagnostics. 28 | func (i *interval) String() string { 29 | return fmt.Sprint(*i) 30 | } 31 | 32 | // Set is the method to set the flag value, part of the flag.Value interface. 33 | // Set's argument is a string to be parsed to set the flag. 34 | // It's a comma-separated list, so we split it. 35 | func (i *interval) Set(value string) error { 36 | // If we wanted to allow the flag to be set multiple times, 37 | // accumulating values, we would delete this if statement. 38 | // That would permit usages such as 39 | // -deltaT 10s -deltaT 15s 40 | // and other combinations. 41 | if len(*i) > 0 { 42 | return errors.New("interval flag already set") 43 | } 44 | for _, dt := range strings.Split(value, ",") { 45 | duration, err := time.ParseDuration(dt) 46 | if err != nil { 47 | return err 48 | } 49 | *i = append(*i, duration) 50 | } 51 | return nil 52 | } 53 | 54 | // Define a flag to accumulate durations. Because it has a special type, 55 | // we need to use the Var function and therefore create the flag during 56 | // init. 57 | 58 | var intervalFlag interval 59 | 60 | func init() { 61 | // Tie the command-line flag to the intervalFlag variable and 62 | // set a usage message. 63 | flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events") 64 | } 65 | 66 | func Example() { 67 | // All the interesting pieces are with the variables declared above, but 68 | // to enable the flag package to see the flags defined there, one must 69 | // execute, typically at the start of main (not init!): 70 | // flag.Parse() 71 | // We don't run it here because this is not a main function and 72 | // the testing suite has already parsed the flags. 73 | } 74 | -------------------------------------------------------------------------------- /export_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. 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 pflag 6 | 7 | import ( 8 | "io/ioutil" 9 | "os" 10 | ) 11 | 12 | // Additional routines compiled into the package only during testing. 13 | 14 | // ResetForTesting clears all flag state and sets the usage function as directed. 15 | // After calling ResetForTesting, parse errors in flag handling will not 16 | // exit the program. 17 | func ResetForTesting(usage func()) { 18 | CommandLine = &FlagSet{ 19 | name: os.Args[0], 20 | errorHandling: ContinueOnError, 21 | output: ioutil.Discard, 22 | } 23 | Usage = usage 24 | } 25 | 26 | // GetCommandLine returns the default FlagSet. 27 | func GetCommandLine() *FlagSet { 28 | return CommandLine 29 | } 30 | -------------------------------------------------------------------------------- /flag.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. 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 | /* 6 | 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/ogier/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 | var ip = flag.Int("flagname", 1234, "help message for flagname") 31 | If you like, you can bind the flag to a variable using the Var() functions. 32 | var flagvar int 33 | func init() { 34 | flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") 35 | } 36 | Or you can create custom flags that satisfy the Value interface (with 37 | pointer receivers) and couple them to flag parsing by 38 | flag.Var(&flagVal, "name", "help message for flagname") 39 | For such flags, the default value is just the initial value of the variable. 40 | 41 | After all flags are defined, call 42 | flag.Parse() 43 | to parse the command line into the defined flags. 44 | 45 | Flags may then be used directly. If you're using the flags themselves, 46 | they are all pointers; if you bind to variables, they're values. 47 | fmt.Println("ip has value ", *ip) 48 | fmt.Println("flagvar has value ", flagvar) 49 | 50 | After parsing, the arguments after the flag are available as the 51 | slice flag.Args() or individually as flag.Arg(i). 52 | The arguments are indexed from 0 through flag.NArg()-1. 53 | 54 | The pflag package also defines some new functions that are not in flag, 55 | that give one-letter shorthands for flags. You can use these by appending 56 | 'P' to the name of any function that defines a flag. 57 | var ip = flag.IntP("flagname", "f", 1234, "help message") 58 | var flagvar bool 59 | func init() { 60 | flag.BoolVarP("boolname", "b", true, "help message") 61 | } 62 | flag.VarP(&flagVar, "varname", "v", 1234, "help message") 63 | Shorthand letters can be used with single dashes on the command line. 64 | Boolean shorthand flags can be combined with other shorthand flags. 65 | 66 | Command line flag syntax: 67 | --flag // boolean flags only 68 | --flag=x 69 | 70 | Unlike the flag package, a single dash before an option means something 71 | different than a double dash. Single dashes signify a series of shorthand 72 | letters for flags. All but the last shorthand letter must be boolean flags. 73 | // boolean flags 74 | -f 75 | -abc 76 | // non-boolean flags 77 | -n 1234 78 | -Ifile 79 | // mixed 80 | -abcs "hello" 81 | -abcn1234 82 | 83 | Flag parsing stops after the terminator "--". Unlike the flag package, 84 | flags can be interspersed with arguments anywhere on the command line 85 | before this terminator. 86 | 87 | Integer flags accept 1234, 0664, 0x1234 and may be negative. 88 | Boolean flags (in their long form) accept 1, 0, t, f, true, false, 89 | TRUE, FALSE, True, False. 90 | Duration flags accept any input valid for time.ParseDuration. 91 | 92 | The default set of command-line flags is controlled by 93 | top-level functions. The FlagSet type allows one to define 94 | independent sets of flags, such as to implement subcommands 95 | in a command-line interface. The methods of FlagSet are 96 | analogous to the top-level functions for the command-line 97 | flag set. 98 | */ 99 | package pflag 100 | 101 | import ( 102 | "errors" 103 | "fmt" 104 | "io" 105 | "os" 106 | "sort" 107 | "strings" 108 | ) 109 | 110 | // ErrHelp is the error returned if the flag -help is invoked but no such flag is defined. 111 | var ErrHelp = errors.New("pflag: help requested") 112 | 113 | // ErrorHandling defines how to handle flag parsing errors. 114 | type ErrorHandling int 115 | 116 | const ( 117 | ContinueOnError ErrorHandling = iota 118 | ExitOnError 119 | PanicOnError 120 | ) 121 | 122 | // A FlagSet represents a set of defined flags. 123 | type FlagSet struct { 124 | // Usage is the function called when an error occurs while parsing flags. 125 | // The field is a function (not a method) that may be changed to point to 126 | // a custom error handler. 127 | Usage func() 128 | 129 | name string 130 | parsed bool 131 | actual map[string]*Flag 132 | formal map[string]*Flag 133 | shorthands map[byte]*Flag 134 | args []string // arguments after flags 135 | exitOnError bool // does the program exit if there's an error? 136 | errorHandling ErrorHandling 137 | output io.Writer // nil means stderr; use out() accessor 138 | interspersed bool // allow interspersed option/non-option args 139 | } 140 | 141 | // A Flag represents the state of a flag. 142 | type Flag struct { 143 | Name string // name as it appears on command line 144 | Shorthand string // one-letter abbreviated flag 145 | Usage string // help message 146 | Value Value // value as set 147 | DefValue string // default value (as text); for usage message 148 | } 149 | 150 | // sortFlags returns the flags as a slice in lexicographical sorted order. 151 | func sortFlags(flags map[string]*Flag) []*Flag { 152 | list := make(sort.StringSlice, len(flags)) 153 | i := 0 154 | for _, f := range flags { 155 | list[i] = f.Name 156 | i++ 157 | } 158 | list.Sort() 159 | result := make([]*Flag, len(list)) 160 | for i, name := range list { 161 | result[i] = flags[name] 162 | } 163 | return result 164 | } 165 | 166 | func (f *FlagSet) out() io.Writer { 167 | if f.output == nil { 168 | return os.Stderr 169 | } 170 | return f.output 171 | } 172 | 173 | // SetOutput sets the destination for usage and error messages. 174 | // If output is nil, os.Stderr is used. 175 | func (f *FlagSet) SetOutput(output io.Writer) { 176 | f.output = output 177 | } 178 | 179 | // VisitAll visits the flags in lexicographical order, calling fn for each. 180 | // It visits all flags, even those not set. 181 | func (f *FlagSet) VisitAll(fn func(*Flag)) { 182 | for _, flag := range sortFlags(f.formal) { 183 | fn(flag) 184 | } 185 | } 186 | 187 | // VisitAll visits the command-line flags in lexicographical order, calling 188 | // fn for each. It visits all flags, even those not set. 189 | func VisitAll(fn func(*Flag)) { 190 | CommandLine.VisitAll(fn) 191 | } 192 | 193 | // Visit visits the flags in lexicographical order, calling fn for each. 194 | // It visits only those flags that have been set. 195 | func (f *FlagSet) Visit(fn func(*Flag)) { 196 | for _, flag := range sortFlags(f.actual) { 197 | fn(flag) 198 | } 199 | } 200 | 201 | // Visit visits the command-line flags in lexicographical order, calling fn 202 | // for each. It visits only those flags that have been set. 203 | func Visit(fn func(*Flag)) { 204 | CommandLine.Visit(fn) 205 | } 206 | 207 | // Lookup returns the Flag structure of the named flag, returning nil if none exists. 208 | func (f *FlagSet) Lookup(name string) *Flag { 209 | return f.formal[name] 210 | } 211 | 212 | // Lookup returns the Flag structure of the named command-line flag, 213 | // returning nil if none exists. 214 | func Lookup(name string) *Flag { 215 | return CommandLine.formal[name] 216 | } 217 | 218 | // Set sets the value of the named flag. 219 | func (f *FlagSet) Set(name, value string) error { 220 | flag, ok := f.formal[name] 221 | if !ok { 222 | return fmt.Errorf("no such flag -%v", name) 223 | } 224 | err := flag.Value.Set(value) 225 | if err != nil { 226 | return err 227 | } 228 | if f.actual == nil { 229 | f.actual = make(map[string]*Flag) 230 | } 231 | f.actual[name] = flag 232 | return nil 233 | } 234 | 235 | // Set sets the value of the named command-line flag. 236 | func Set(name, value string) error { 237 | return CommandLine.Set(name, value) 238 | } 239 | 240 | // isZeroValue guesses whether the string represents the zero 241 | // value for a flag. It is not accurate but in practice works OK. 242 | func isZeroValue(value string) bool { 243 | switch value { 244 | case "false": 245 | return true 246 | case "": 247 | return true 248 | case "0": 249 | return true 250 | } 251 | return false 252 | } 253 | 254 | // UnquoteUsage extracts a back-quoted name from the usage 255 | // string for a flag and returns it and the un-quoted usage. 256 | // Given "a `name` to show" it returns ("name", "a name to show"). 257 | // If there are no back quotes, the name is an educated guess of the 258 | // type of the flag's value, or the empty string if the flag is boolean. 259 | func UnquoteUsage(flag *Flag) (name string, usage string) { 260 | // Look for a back-quoted name, but avoid the strings package. 261 | usage = flag.Usage 262 | for i := 0; i < len(usage); i++ { 263 | if usage[i] == '`' { 264 | for j := i + 1; j < len(usage); j++ { 265 | if usage[j] == '`' { 266 | name = usage[i+1 : j] 267 | usage = usage[:i] + name + usage[j+1:] 268 | return name, usage 269 | } 270 | } 271 | break // Only one back quote; use type name. 272 | } 273 | } 274 | // No explicit name, so use type if we can find one. 275 | name = "value" 276 | switch flag.Value.(type) { 277 | case boolFlag: 278 | name = "" 279 | case *durationValue: 280 | name = "duration" 281 | case *float64Value: 282 | name = "float" 283 | case *intValue, *int64Value: 284 | name = "int" 285 | case *stringValue: 286 | name = "string" 287 | case *uintValue, *uint64Value: 288 | name = "uint" 289 | } 290 | return 291 | } 292 | 293 | // PrintDefaults prints to standard error the default values of all 294 | // defined command-line flags in the set. See the documentation for 295 | // the global function PrintDefaults for more information. 296 | func (f *FlagSet) PrintDefaults() { 297 | f.VisitAll(func(flag *Flag) { 298 | s := "" 299 | if len(flag.Shorthand) > 0 { 300 | s = fmt.Sprintf(" -%s, --%s", flag.Shorthand, flag.Name) 301 | } else { 302 | s = fmt.Sprintf(" --%s", flag.Name) 303 | } 304 | 305 | name, usage := UnquoteUsage(flag) 306 | if len(name) > 0 { 307 | s += " " + name 308 | } 309 | 310 | s += "\n \t" 311 | s += usage 312 | if !isZeroValue(flag.DefValue) { 313 | if _, ok := flag.Value.(*stringValue); ok { 314 | // put quotes on the value 315 | s += fmt.Sprintf(" (default %q)", flag.DefValue) 316 | } else { 317 | s += fmt.Sprintf(" (default %v)", flag.DefValue) 318 | } 319 | } 320 | fmt.Fprint(f.out(), s, "\n") 321 | }) 322 | } 323 | 324 | // PrintDefaults prints to standard error the default values of all defined command-line flags. 325 | func PrintDefaults() { 326 | CommandLine.PrintDefaults() 327 | } 328 | 329 | // defaultUsage is the default function to print a usage message. 330 | func defaultUsage(f *FlagSet) { 331 | if f.name == "" { 332 | fmt.Fprintf(f.out(), "Usage:\n") 333 | } else { 334 | fmt.Fprintf(f.out(), "Usage of %s:\n", f.name) 335 | } 336 | f.PrintDefaults() 337 | } 338 | 339 | // NOTE: Usage is not just defaultUsage(CommandLine) 340 | // because it serves (via godoc flag Usage) as the example 341 | // for how to write your own usage function. 342 | 343 | // Usage prints to standard error a usage message documenting all defined command-line flags. 344 | // The function is a variable that may be changed to point to a custom function. 345 | var Usage = func() { 346 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) 347 | PrintDefaults() 348 | } 349 | 350 | // NFlag returns the number of flags that have been set. 351 | func (f *FlagSet) NFlag() int { return len(f.actual) } 352 | 353 | // NFlag returns the number of command-line flags that have been set. 354 | func NFlag() int { return len(CommandLine.actual) } 355 | 356 | // Arg returns the i'th argument. Arg(0) is the first remaining argument 357 | // after flags have been processed. 358 | func (f *FlagSet) Arg(i int) string { 359 | if i < 0 || i >= len(f.args) { 360 | return "" 361 | } 362 | return f.args[i] 363 | } 364 | 365 | // Arg returns the i'th command-line argument. Arg(0) is the first remaining argument 366 | // after flags have been processed. 367 | func Arg(i int) string { 368 | return CommandLine.Arg(i) 369 | } 370 | 371 | // NArg is the number of arguments remaining after flags have been processed. 372 | func (f *FlagSet) NArg() int { return len(f.args) } 373 | 374 | // NArg is the number of arguments remaining after flags have been processed. 375 | func NArg() int { return len(CommandLine.args) } 376 | 377 | // Args returns the non-flag arguments. 378 | func (f *FlagSet) Args() []string { return f.args } 379 | 380 | // Args returns the non-flag command-line arguments. 381 | func Args() []string { return CommandLine.args } 382 | 383 | // Var defines a flag with the specified name and usage string. The type and 384 | // value of the flag are represented by the first argument, of type Value, which 385 | // typically holds a user-defined implementation of Value. For instance, the 386 | // caller could create a flag that turns a comma-separated string into a slice 387 | // of strings by giving the slice the methods of Value; in particular, Set would 388 | // decompose the comma-separated string into the slice. 389 | func (f *FlagSet) Var(value Value, name string, usage string) { 390 | f.VarP(value, name, "", usage) 391 | } 392 | 393 | // Like Var, but accepts a shorthand letter that can be used after a single dash. 394 | func (f *FlagSet) VarP(value Value, name, shorthand, usage string) { 395 | // Remember the default value as a string; it won't change. 396 | flag := &Flag{name, shorthand, usage, value, value.String()} 397 | _, alreadythere := f.formal[name] 398 | if alreadythere { 399 | msg := fmt.Sprintf("%s flag redefined: %s", f.name, name) 400 | fmt.Fprintln(f.out(), msg) 401 | panic(msg) // Happens only if flags are declared with identical names 402 | } 403 | if f.formal == nil { 404 | f.formal = make(map[string]*Flag) 405 | } 406 | f.formal[name] = flag 407 | 408 | if len(shorthand) == 0 { 409 | return 410 | } 411 | if len(shorthand) > 1 { 412 | fmt.Fprintf(f.out(), "%s shorthand more than ASCII character: %s\n", f.name, shorthand) 413 | panic("shorthand is more than one character") 414 | } 415 | if f.shorthands == nil { 416 | f.shorthands = make(map[byte]*Flag) 417 | } 418 | c := shorthand[0] 419 | old, alreadythere := f.shorthands[c] 420 | if alreadythere { 421 | fmt.Fprintf(f.out(), "%s shorthand reused: %q for %s already used for %s\n", f.name, c, name, old.Name) 422 | panic("shorthand redefinition") 423 | } 424 | f.shorthands[c] = flag 425 | } 426 | 427 | // Var defines a flag with the specified name and usage string. The type and 428 | // value of the flag are represented by the first argument, of type Value, which 429 | // typically holds a user-defined implementation of Value. For instance, the 430 | // caller could create a flag that turns a comma-separated string into a slice 431 | // of strings by giving the slice the methods of Value; in particular, Set would 432 | // decompose the comma-separated string into the slice. 433 | func Var(value Value, name string, usage string) { 434 | CommandLine.VarP(value, name, "", usage) 435 | } 436 | 437 | // Like Var, but accepts a shorthand letter that can be used after a single dash. 438 | func VarP(value Value, name, shorthand, usage string) { 439 | CommandLine.VarP(value, name, shorthand, usage) 440 | } 441 | 442 | // failf prints to standard error a formatted error and usage message and 443 | // returns the error. 444 | func (f *FlagSet) failf(format string, a ...interface{}) error { 445 | err := fmt.Errorf(format, a...) 446 | fmt.Fprintln(f.out(), err) 447 | f.usage() 448 | return err 449 | } 450 | 451 | // usage calls the Usage method for the flag set, or the usage function if 452 | // the flag set is CommandLine. 453 | func (f *FlagSet) usage() { 454 | if f == CommandLine { 455 | Usage() 456 | } else if f.Usage == nil { 457 | defaultUsage(f) 458 | } else { 459 | f.Usage() 460 | } 461 | } 462 | 463 | func (f *FlagSet) setFlag(flag *Flag, value string, origArg string) error { 464 | if err := flag.Value.Set(value); err != nil { 465 | return f.failf("invalid argument %q for %s: %v", value, origArg, err) 466 | } 467 | // mark as visited for Visit() 468 | if f.actual == nil { 469 | f.actual = make(map[string]*Flag) 470 | } 471 | f.actual[flag.Name] = flag 472 | 473 | return nil 474 | } 475 | 476 | func (f *FlagSet) parseArgs(args []string) error { 477 | for len(args) > 0 { 478 | s := args[0] 479 | args = args[1:] 480 | if len(s) == 0 || s[0] != '-' || len(s) == 1 { 481 | if !f.interspersed { 482 | f.args = append(f.args, s) 483 | f.args = append(f.args, args...) 484 | return nil 485 | } 486 | f.args = append(f.args, s) 487 | continue 488 | } 489 | 490 | if s[1] == '-' { 491 | if len(s) == 2 { // "--" terminates the flags 492 | f.args = append(f.args, args...) 493 | return nil 494 | } 495 | name := s[2:] 496 | if len(name) == 0 || name[0] == '-' || name[0] == '=' { 497 | return f.failf("bad flag syntax: %s", s) 498 | } 499 | split := strings.SplitN(name, "=", 2) 500 | name = split[0] 501 | m := f.formal 502 | flag, alreadythere := m[name] // BUG 503 | if !alreadythere { 504 | if name == "help" { // special case for nice help message. 505 | f.usage() 506 | return ErrHelp 507 | } 508 | return f.failf("unknown flag: --%s", name) 509 | } 510 | if len(split) == 1 { 511 | if bv, ok := flag.Value.(boolFlag); !ok || !bv.IsBoolFlag() { 512 | return f.failf("flag needs an argument: %s", s) 513 | } 514 | f.setFlag(flag, "true", s) 515 | } else { 516 | if err := f.setFlag(flag, split[1], s); err != nil { 517 | return err 518 | } 519 | } 520 | } else { 521 | shorthands := s[1:] 522 | for i := 0; i < len(shorthands); i++ { 523 | c := shorthands[i] 524 | flag, alreadythere := f.shorthands[c] 525 | if !alreadythere { 526 | if c == 'h' { // special case for nice help message. 527 | f.usage() 528 | return ErrHelp 529 | } 530 | return f.failf("unknown shorthand flag: %q in -%s", c, shorthands) 531 | } 532 | if bv, ok := flag.Value.(boolFlag); ok && bv.IsBoolFlag() { 533 | f.setFlag(flag, "true", s) 534 | continue 535 | } 536 | if i < len(shorthands)-1 { 537 | if err := f.setFlag(flag, shorthands[i+1:], s); err != nil { 538 | return err 539 | } 540 | break 541 | } 542 | if len(args) == 0 { 543 | return f.failf("flag needs an argument: %q in -%s", c, shorthands) 544 | } 545 | if err := f.setFlag(flag, args[0], s); err != nil { 546 | return err 547 | } 548 | args = args[1:] 549 | break // should be unnecessary 550 | } 551 | } 552 | } 553 | return nil 554 | } 555 | 556 | // Parse parses flag definitions from the argument list, which should not 557 | // include the command name. Must be called after all flags in the FlagSet 558 | // are defined and before flags are accessed by the program. 559 | // The return value will be ErrHelp if -help was set but not defined. 560 | func (f *FlagSet) Parse(arguments []string) error { 561 | f.parsed = true 562 | f.args = make([]string, 0, len(arguments)) 563 | err := f.parseArgs(arguments) 564 | if err != nil { 565 | switch f.errorHandling { 566 | case ContinueOnError: 567 | return err 568 | case ExitOnError: 569 | os.Exit(2) 570 | case PanicOnError: 571 | panic(err) 572 | } 573 | } 574 | return nil 575 | } 576 | 577 | // Parsed reports whether f.Parse has been called. 578 | func (f *FlagSet) Parsed() bool { 579 | return f.parsed 580 | } 581 | 582 | // Parse parses the command-line flags from os.Args[1:]. Must be called 583 | // after all flags are defined and before flags are accessed by the program. 584 | func Parse() { 585 | // Ignore errors; CommandLine is set for ExitOnError. 586 | CommandLine.Parse(os.Args[1:]) 587 | } 588 | 589 | // Whether to support interspersed option/non-option arguments. 590 | func SetInterspersed(interspersed bool) { 591 | CommandLine.SetInterspersed(interspersed) 592 | } 593 | 594 | // Parsed returns true if the command-line flags have been parsed. 595 | func Parsed() bool { 596 | return CommandLine.Parsed() 597 | } 598 | 599 | // The default set of command-line flags, parsed from os.Args. 600 | var CommandLine = NewFlagSet(os.Args[0], ExitOnError) 601 | 602 | // NewFlagSet returns a new, empty flag set with the specified name and 603 | // error handling property. 604 | func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet { 605 | f := &FlagSet{ 606 | name: name, 607 | errorHandling: errorHandling, 608 | interspersed: true, 609 | } 610 | return f 611 | } 612 | 613 | // Whether to support interspersed option/non-option arguments. 614 | func (f *FlagSet) SetInterspersed(interspersed bool) { 615 | f.interspersed = interspersed 616 | } 617 | 618 | // Init sets the name and error handling property for a flag set. 619 | // By default, the zero FlagSet uses an empty name and the 620 | // ContinueOnError error handling policy. 621 | func (f *FlagSet) Init(name string, errorHandling ErrorHandling) { 622 | f.name = name 623 | f.errorHandling = errorHandling 624 | } 625 | -------------------------------------------------------------------------------- /flag_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. 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 pflag 6 | 7 | import ( 8 | "bytes" 9 | "fmt" 10 | "os" 11 | "sort" 12 | "strings" 13 | "testing" 14 | "time" 15 | ) 16 | 17 | var ( 18 | test_bool = Bool("test_bool", false, "bool value") 19 | test_int = Int("test_int", 0, "int value") 20 | test_int64 = Int64("test_int64", 0, "int64 value") 21 | test_uint = Uint("test_uint", 0, "uint value") 22 | test_uint64 = Uint64("test_uint64", 0, "uint64 value") 23 | test_string = String("test_string", "0", "string value") 24 | test_float64 = Float64("test_float64", 0, "float64 value") 25 | test_duration = Duration("test_duration", 0, "time.Duration value") 26 | ) 27 | 28 | func boolString(s string) string { 29 | if s == "0" { 30 | return "false" 31 | } 32 | return "true" 33 | } 34 | 35 | func TestEverything(t *testing.T) { 36 | m := make(map[string]*Flag) 37 | desired := "0" 38 | visitor := func(f *Flag) { 39 | if len(f.Name) > 5 && f.Name[0:5] == "test_" { 40 | m[f.Name] = f 41 | ok := false 42 | switch { 43 | case f.Value.String() == desired: 44 | ok = true 45 | case f.Name == "test_bool" && f.Value.String() == boolString(desired): 46 | ok = true 47 | case f.Name == "test_duration" && f.Value.String() == desired+"s": 48 | ok = true 49 | } 50 | if !ok { 51 | t.Error("Visit: bad value", f.Value.String(), "for", f.Name) 52 | } 53 | } 54 | } 55 | VisitAll(visitor) 56 | if len(m) != 8 { 57 | t.Error("VisitAll misses some flags") 58 | for k, v := range m { 59 | t.Log(k, *v) 60 | } 61 | } 62 | m = make(map[string]*Flag) 63 | Visit(visitor) 64 | if len(m) != 0 { 65 | t.Errorf("Visit sees unset flags") 66 | for k, v := range m { 67 | t.Log(k, *v) 68 | } 69 | } 70 | // Now set all flags 71 | Set("test_bool", "true") 72 | Set("test_int", "1") 73 | Set("test_int64", "1") 74 | Set("test_uint", "1") 75 | Set("test_uint64", "1") 76 | Set("test_string", "1") 77 | Set("test_float64", "1") 78 | Set("test_duration", "1s") 79 | desired = "1" 80 | Visit(visitor) 81 | if len(m) != 8 { 82 | t.Error("Visit fails after set") 83 | for k, v := range m { 84 | t.Log(k, *v) 85 | } 86 | } 87 | // Now test they're visited in sort order. 88 | var flagNames []string 89 | Visit(func(f *Flag) { flagNames = append(flagNames, f.Name) }) 90 | if !sort.StringsAreSorted(flagNames) { 91 | t.Errorf("flag names not sorted: %v", flagNames) 92 | } 93 | } 94 | 95 | func TestUsage(t *testing.T) { 96 | called := false 97 | ResetForTesting(func() { called = true }) 98 | if GetCommandLine().Parse([]string{"--x"}) == nil { 99 | t.Error("parse did not fail for unknown flag") 100 | } 101 | if !called { 102 | t.Error("did not call Usage for unknown flag") 103 | } 104 | } 105 | 106 | func testParse(f *FlagSet, t *testing.T) { 107 | if f.Parsed() { 108 | t.Error("f.Parse() = true before Parse") 109 | } 110 | boolFlag := f.Bool("bool", false, "bool value") 111 | bool2Flag := f.Bool("bool2", false, "bool2 value") 112 | bool3Flag := f.Bool("bool3", false, "bool3 value") 113 | intFlag := f.Int("int", 0, "int value") 114 | int64Flag := f.Int64("int64", 0, "int64 value") 115 | uintFlag := f.Uint("uint", 0, "uint value") 116 | uint64Flag := f.Uint64("uint64", 0, "uint64 value") 117 | stringFlag := f.String("string", "0", "string value") 118 | float64Flag := f.Float64("float64", 0, "float64 value") 119 | durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value") 120 | extra := "one-extra-argument" 121 | args := []string{ 122 | "--bool", 123 | "--bool2=true", 124 | "--bool3=false", 125 | "--int=22", 126 | "--int64=0x23", 127 | "--uint=24", 128 | "--uint64=25", 129 | "--string=hello", 130 | "--float64=2718e28", 131 | "--duration=2m", 132 | extra, 133 | } 134 | if err := f.Parse(args); err != nil { 135 | t.Fatal(err) 136 | } 137 | if !f.Parsed() { 138 | t.Error("f.Parse() = false after Parse") 139 | } 140 | if *boolFlag != true { 141 | t.Error("bool flag should be true, is ", *boolFlag) 142 | } 143 | if *bool2Flag != true { 144 | t.Error("bool2 flag should be true, is ", *bool2Flag) 145 | } 146 | if *bool3Flag != false { 147 | t.Error("bool3 flag should be false, is ", *bool2Flag) 148 | } 149 | if *intFlag != 22 { 150 | t.Error("int flag should be 22, is ", *intFlag) 151 | } 152 | if *int64Flag != 0x23 { 153 | t.Error("int64 flag should be 0x23, is ", *int64Flag) 154 | } 155 | if *uintFlag != 24 { 156 | t.Error("uint flag should be 24, is ", *uintFlag) 157 | } 158 | if *uint64Flag != 25 { 159 | t.Error("uint64 flag should be 25, is ", *uint64Flag) 160 | } 161 | if *stringFlag != "hello" { 162 | t.Error("string flag should be `hello`, is ", *stringFlag) 163 | } 164 | if *float64Flag != 2718e28 { 165 | t.Error("float64 flag should be 2718e28, is ", *float64Flag) 166 | } 167 | if *durationFlag != 2*time.Minute { 168 | t.Error("duration flag should be 2m, is ", *durationFlag) 169 | } 170 | if len(f.Args()) != 1 { 171 | t.Error("expected one argument, got", len(f.Args())) 172 | } else if f.Args()[0] != extra { 173 | t.Errorf("expected argument %q got %q", extra, f.Args()[0]) 174 | } 175 | } 176 | 177 | func TestShorthand(t *testing.T) { 178 | f := NewFlagSet("shorthand", ContinueOnError) 179 | if f.Parsed() { 180 | t.Error("f.Parse() = true before Parse") 181 | } 182 | boolaFlag := f.BoolP("boola", "a", false, "bool value") 183 | boolbFlag := f.BoolP("boolb", "b", false, "bool2 value") 184 | boolcFlag := f.BoolP("boolc", "c", false, "bool3 value") 185 | stringFlag := f.StringP("string", "s", "0", "string value") 186 | extra := "interspersed-argument" 187 | notaflag := "--i-look-like-a-flag" 188 | args := []string{ 189 | "-ab", 190 | extra, 191 | "-cs", 192 | "hello", 193 | "--", 194 | notaflag, 195 | } 196 | if err := f.Parse(args); err != nil { 197 | t.Fatal(err) 198 | } 199 | if !f.Parsed() { 200 | t.Error("f.Parse() = false after Parse") 201 | } 202 | if *boolaFlag != true { 203 | t.Error("boola flag should be true, is ", *boolaFlag) 204 | } 205 | if *boolbFlag != true { 206 | t.Error("boolb flag should be true, is ", *boolbFlag) 207 | } 208 | if *boolcFlag != true { 209 | t.Error("boolc flag should be true, is ", *boolcFlag) 210 | } 211 | if *stringFlag != "hello" { 212 | t.Error("string flag should be `hello`, is ", *stringFlag) 213 | } 214 | if len(f.Args()) != 2 { 215 | t.Error("expected one argument, got", len(f.Args())) 216 | } else if f.Args()[0] != extra { 217 | t.Errorf("expected argument %q got %q", extra, f.Args()[0]) 218 | } else if f.Args()[1] != notaflag { 219 | t.Errorf("expected argument %q got %q", notaflag, f.Args()[1]) 220 | } 221 | } 222 | 223 | func TestParse(t *testing.T) { 224 | ResetForTesting(func() { t.Error("bad parse") }) 225 | testParse(GetCommandLine(), t) 226 | } 227 | 228 | func TestFlagSetParse(t *testing.T) { 229 | testParse(NewFlagSet("test", ContinueOnError), t) 230 | } 231 | 232 | // Declare a user-defined flag type. 233 | type flagVar []string 234 | 235 | func (f *flagVar) String() string { 236 | return fmt.Sprint([]string(*f)) 237 | } 238 | 239 | func (f *flagVar) Set(value string) error { 240 | *f = append(*f, value) 241 | return nil 242 | } 243 | 244 | func TestUserDefined(t *testing.T) { 245 | var flags FlagSet 246 | flags.Init("test", ContinueOnError) 247 | var v flagVar 248 | flags.VarP(&v, "v", "v", "usage") 249 | if err := flags.Parse([]string{"--v=1", "-v2", "-v", "3"}); err != nil { 250 | t.Error(err) 251 | } 252 | if len(v) != 3 { 253 | t.Fatal("expected 3 args; got ", len(v)) 254 | } 255 | expect := "[1 2 3]" 256 | if v.String() != expect { 257 | t.Errorf("expected value %q got %q", expect, v.String()) 258 | } 259 | } 260 | 261 | func TestSetOutput(t *testing.T) { 262 | var flags FlagSet 263 | var buf bytes.Buffer 264 | flags.SetOutput(&buf) 265 | flags.Init("test", ContinueOnError) 266 | flags.Parse([]string{"--unknown"}) 267 | if out := buf.String(); !strings.Contains(out, "--unknown") { 268 | t.Logf("expected output mentioning unknown; got %q", out) 269 | } 270 | } 271 | 272 | // This tests that one can reset the flags. This still works but not well, and is 273 | // superseded by FlagSet. 274 | func TestChangingArgs(t *testing.T) { 275 | ResetForTesting(func() { t.Fatal("bad parse") }) 276 | oldArgs := os.Args 277 | defer func() { os.Args = oldArgs }() 278 | os.Args = []string{"cmd", "--before", "subcmd"} 279 | before := Bool("before", false, "") 280 | if err := GetCommandLine().Parse(os.Args[1:]); err != nil { 281 | t.Fatal(err) 282 | } 283 | cmd := Arg(0) 284 | os.Args = []string{"subcmd", "--after", "args"} 285 | after := Bool("after", false, "") 286 | Parse() 287 | args := Args() 288 | 289 | if !*before || cmd != "subcmd" || !*after || len(args) != 1 || args[0] != "args" { 290 | t.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before, cmd, *after, args) 291 | } 292 | } 293 | 294 | // Test that -help invokes the usage message and returns ErrHelp. 295 | func TestHelp(t *testing.T) { 296 | var helpCalled = false 297 | fs := NewFlagSet("help test", ContinueOnError) 298 | fs.Usage = func() { helpCalled = true } 299 | var flag bool 300 | fs.BoolVar(&flag, "flag", false, "regular flag") 301 | // Regular flag invocation should work 302 | err := fs.Parse([]string{"--flag=true"}) 303 | if err != nil { 304 | t.Fatal("expected no error; got ", err) 305 | } 306 | if !flag { 307 | t.Error("flag was not set by --flag") 308 | } 309 | if helpCalled { 310 | t.Error("help called for regular flag") 311 | helpCalled = false // reset for next test 312 | } 313 | // Help flag should work as expected. 314 | err = fs.Parse([]string{"--help"}) 315 | if err == nil { 316 | t.Fatal("error expected") 317 | } 318 | if err != ErrHelp { 319 | t.Fatal("expected ErrHelp; got ", err) 320 | } 321 | if !helpCalled { 322 | t.Fatal("help was not called") 323 | } 324 | // If we define a help flag, that should override. 325 | var help bool 326 | fs.BoolVar(&help, "help", false, "help flag") 327 | helpCalled = false 328 | err = fs.Parse([]string{"--help"}) 329 | if err != nil { 330 | t.Fatal("expected no error for defined --help; got ", err) 331 | } 332 | if helpCalled { 333 | t.Fatal("help was called; should not have been for defined help flag") 334 | } 335 | } 336 | 337 | func TestNoInterspersed(t *testing.T) { 338 | f := NewFlagSet("test", ContinueOnError) 339 | f.SetInterspersed(false) 340 | f.Bool("true", true, "always true") 341 | f.Bool("false", false, "always false") 342 | err := f.Parse([]string{"--true", "break", "--false"}) 343 | if err != nil { 344 | t.Fatal("expected no error; got ", err) 345 | } 346 | args := f.Args() 347 | if len(args) != 2 || args[0] != "break" || args[1] != "--false" { 348 | t.Fatal("expected interspersed options/non-options to fail") 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /float32.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- float32 Value 9 | type float32Value float32 10 | 11 | func newFloat32Value(val float32, p *float32) *float32Value { 12 | *p = val 13 | return (*float32Value)(p) 14 | } 15 | 16 | func (f *float32Value) Set(s string) error { 17 | v, err := strconv.ParseFloat(s, 32) 18 | *f = float32Value(v) 19 | return err 20 | } 21 | 22 | func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) } 23 | 24 | // Float32Var defines a float32 flag with specified name, default value, and usage string. 25 | // The argument p points to a float32 variable in which to store the value of the flag. 26 | func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) { 27 | f.VarP(newFloat32Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Float32Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) { 32 | f.VarP(newFloat32Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Float32Var defines a float32 flag with specified name, default value, and usage string. 36 | // The argument p points to a float32 variable in which to store the value of the flag. 37 | func Float32Var(p *float32, name string, value float32, usage string) { 38 | CommandLine.VarP(newFloat32Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Float32Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Float32VarP(p *float32, name, shorthand string, value float32, usage string) { 43 | CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Float32 defines a float32 flag with specified name, default value, and usage string. 47 | // The return value is the address of a float32 variable that stores the value of the flag. 48 | func (f *FlagSet) Float32(name string, value float32, usage string) *float32 { 49 | p := new(float32) 50 | f.Float32VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Float32, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 { 56 | p := new(float32) 57 | f.Float32VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Float32 defines a float32 flag with specified name, default value, and usage string. 62 | // The return value is the address of a float32 variable that stores the value of the flag. 63 | func Float32(name string, value float32, usage string) *float32 { 64 | return CommandLine.Float32P(name, "", value, usage) 65 | } 66 | 67 | // Like Float32, but accepts a shorthand letter that can be used after a single dash. 68 | func Float32P(name, shorthand string, value float32, usage string) *float32 { 69 | return CommandLine.Float32P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /float64.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- float64 Value 9 | type float64Value float64 10 | 11 | func newFloat64Value(val float64, p *float64) *float64Value { 12 | *p = val 13 | return (*float64Value)(p) 14 | } 15 | 16 | func (f *float64Value) Set(s string) error { 17 | v, err := strconv.ParseFloat(s, 64) 18 | *f = float64Value(v) 19 | return err 20 | } 21 | 22 | func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) } 23 | 24 | // Float64Var defines a float64 flag with specified name, default value, and usage string. 25 | // The argument p points to a float64 variable in which to store the value of the flag. 26 | func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) { 27 | f.VarP(newFloat64Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Float64Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string) { 32 | f.VarP(newFloat64Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Float64Var defines a float64 flag with specified name, default value, and usage string. 36 | // The argument p points to a float64 variable in which to store the value of the flag. 37 | func Float64Var(p *float64, name string, value float64, usage string) { 38 | CommandLine.VarP(newFloat64Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Float64Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Float64VarP(p *float64, name, shorthand string, value float64, usage string) { 43 | CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Float64 defines a float64 flag with specified name, default value, and usage string. 47 | // The return value is the address of a float64 variable that stores the value of the flag. 48 | func (f *FlagSet) Float64(name string, value float64, usage string) *float64 { 49 | p := new(float64) 50 | f.Float64VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Float64, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64 { 56 | p := new(float64) 57 | f.Float64VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Float64 defines a float64 flag with specified name, default value, and usage string. 62 | // The return value is the address of a float64 variable that stores the value of the flag. 63 | func Float64(name string, value float64, usage string) *float64 { 64 | return CommandLine.Float64P(name, "", value, usage) 65 | } 66 | 67 | // Like Float64, but accepts a shorthand letter that can be used after a single dash. 68 | func Float64P(name, shorthand string, value float64, usage string) *float64 { 69 | return CommandLine.Float64P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /int.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- int Value 9 | type intValue int 10 | 11 | func newIntValue(val int, p *int) *intValue { 12 | *p = val 13 | return (*intValue)(p) 14 | } 15 | 16 | func (i *intValue) Set(s string) error { 17 | v, err := strconv.ParseInt(s, 0, 64) 18 | *i = intValue(v) 19 | return err 20 | } 21 | 22 | func (i *intValue) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // IntVar defines an int flag with specified name, default value, and usage string. 25 | // The argument p points to an int variable in which to store the value of the flag. 26 | func (f *FlagSet) IntVar(p *int, name string, value int, usage string) { 27 | f.VarP(newIntValue(value, p), name, "", usage) 28 | } 29 | 30 | // Like IntVar, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) { 32 | f.VarP(newIntValue(value, p), name, shorthand, usage) 33 | } 34 | 35 | // IntVar defines an int flag with specified name, default value, and usage string. 36 | // The argument p points to an int variable in which to store the value of the flag. 37 | func IntVar(p *int, name string, value int, usage string) { 38 | CommandLine.VarP(newIntValue(value, p), name, "", usage) 39 | } 40 | 41 | // Like IntVar, but accepts a shorthand letter that can be used after a single dash. 42 | func IntVarP(p *int, name, shorthand string, value int, usage string) { 43 | CommandLine.VarP(newIntValue(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Int defines an int flag with specified name, default value, and usage string. 47 | // The return value is the address of an int variable that stores the value of the flag. 48 | func (f *FlagSet) Int(name string, value int, usage string) *int { 49 | p := new(int) 50 | f.IntVarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Int, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int { 56 | p := new(int) 57 | f.IntVarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Int defines an int flag with specified name, default value, and usage string. 62 | // The return value is the address of an int variable that stores the value of the flag. 63 | func Int(name string, value int, usage string) *int { 64 | return CommandLine.IntP(name, "", value, usage) 65 | } 66 | 67 | // Like Int, but accepts a shorthand letter that can be used after a single dash. 68 | func IntP(name, shorthand string, value int, usage string) *int { 69 | return CommandLine.IntP(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /int32.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- int32 Value 9 | type int32Value int32 10 | 11 | func newInt32Value(val int32, p *int32) *int32Value { 12 | *p = val 13 | return (*int32Value)(p) 14 | } 15 | 16 | func (i *int32Value) Set(s string) error { 17 | v, err := strconv.ParseInt(s, 0, 32) 18 | *i = int32Value(v) 19 | return err 20 | } 21 | 22 | func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // Int32Var defines an int32 flag with specified name, default value, and usage string. 25 | // The argument p points to an int32 variable in which to store the value of the flag. 26 | func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) { 27 | f.VarP(newInt32Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Int32Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string) { 32 | f.VarP(newInt32Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Int32Var defines an int32 flag with specified name, default value, and usage string. 36 | // The argument p points to an int32 variable in which to store the value of the flag. 37 | func Int32Var(p *int32, name string, value int32, usage string) { 38 | CommandLine.VarP(newInt32Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Int32Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Int32VarP(p *int32, name, shorthand string, value int32, usage string) { 43 | CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Int32 defines an int32 flag with specified name, default value, and usage string. 47 | // The return value is the address of an int32 variable that stores the value of the flag. 48 | func (f *FlagSet) Int32(name string, value int32, usage string) *int32 { 49 | p := new(int32) 50 | f.Int32VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Int32, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32 { 56 | p := new(int32) 57 | f.Int32VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Int32 defines an int32 flag with specified name, default value, and usage string. 62 | // The return value is the address of an int32 variable that stores the value of the flag. 63 | func Int32(name string, value int32, usage string) *int32 { 64 | return CommandLine.Int32P(name, "", value, usage) 65 | } 66 | 67 | // Like Int32, but accepts a shorthand letter that can be used after a single dash. 68 | func Int32P(name, shorthand string, value int32, usage string) *int32 { 69 | return CommandLine.Int32P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /int64.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- int64 Value 9 | type int64Value int64 10 | 11 | func newInt64Value(val int64, p *int64) *int64Value { 12 | *p = val 13 | return (*int64Value)(p) 14 | } 15 | 16 | func (i *int64Value) Set(s string) error { 17 | v, err := strconv.ParseInt(s, 0, 64) 18 | *i = int64Value(v) 19 | return err 20 | } 21 | 22 | func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // Int64Var defines an int64 flag with specified name, default value, and usage string. 25 | // The argument p points to an int64 variable in which to store the value of the flag. 26 | func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) { 27 | f.VarP(newInt64Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Int64Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string) { 32 | f.VarP(newInt64Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Int64Var defines an int64 flag with specified name, default value, and usage string. 36 | // The argument p points to an int64 variable in which to store the value of the flag. 37 | func Int64Var(p *int64, name string, value int64, usage string) { 38 | CommandLine.VarP(newInt64Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Int64Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Int64VarP(p *int64, name, shorthand string, value int64, usage string) { 43 | CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Int64 defines an int64 flag with specified name, default value, and usage string. 47 | // The return value is the address of an int64 variable that stores the value of the flag. 48 | func (f *FlagSet) Int64(name string, value int64, usage string) *int64 { 49 | p := new(int64) 50 | f.Int64VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Int64, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64 { 56 | p := new(int64) 57 | f.Int64VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Int64 defines an int64 flag with specified name, default value, and usage string. 62 | // The return value is the address of an int64 variable that stores the value of the flag. 63 | func Int64(name string, value int64, usage string) *int64 { 64 | return CommandLine.Int64P(name, "", value, usage) 65 | } 66 | 67 | // Like Int64, but accepts a shorthand letter that can be used after a single dash. 68 | func Int64P(name, shorthand string, value int64, usage string) *int64 { 69 | return CommandLine.Int64P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /int8.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- int8 Value 9 | type int8Value int8 10 | 11 | func newInt8Value(val int8, p *int8) *int8Value { 12 | *p = val 13 | return (*int8Value)(p) 14 | } 15 | 16 | func (i *int8Value) Set(s string) error { 17 | v, err := strconv.ParseInt(s, 0, 8) 18 | *i = int8Value(v) 19 | return err 20 | } 21 | 22 | func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // Int8Var defines an int8 flag with specified name, default value, and usage string. 25 | // The argument p points to an int8 variable in which to store the value of the flag. 26 | func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) { 27 | f.VarP(newInt8Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Int8Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string) { 32 | f.VarP(newInt8Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Int8Var defines an int8 flag with specified name, default value, and usage string. 36 | // The argument p points to an int8 variable in which to store the value of the flag. 37 | func Int8Var(p *int8, name string, value int8, usage string) { 38 | CommandLine.VarP(newInt8Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Int8Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Int8VarP(p *int8, name, shorthand string, value int8, usage string) { 43 | CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Int8 defines an int8 flag with specified name, default value, and usage string. 47 | // The return value is the address of an int8 variable that stores the value of the flag. 48 | func (f *FlagSet) Int8(name string, value int8, usage string) *int8 { 49 | p := new(int8) 50 | f.Int8VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Int8, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8 { 56 | p := new(int8) 57 | f.Int8VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Int8 defines an int8 flag with specified name, default value, and usage string. 62 | // The return value is the address of an int8 variable that stores the value of the flag. 63 | func Int8(name string, value int8, usage string) *int8 { 64 | return CommandLine.Int8P(name, "", value, usage) 65 | } 66 | 67 | // Like Int8, but accepts a shorthand letter that can be used after a single dash. 68 | func Int8P(name, shorthand string, value int8, usage string) *int8 { 69 | return CommandLine.Int8P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /ip.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | ) 7 | 8 | // -- net.IP value 9 | type ipValue net.IP 10 | 11 | func newIPValue(val net.IP, p *net.IP) *ipValue { 12 | *p = val 13 | return (*ipValue)(p) 14 | } 15 | 16 | func (i *ipValue) String() string { return net.IP(*i).String() } 17 | func (i *ipValue) Set(s string) error { 18 | ip := net.ParseIP(s) 19 | if ip == nil { 20 | return fmt.Errorf("failed to parse IP: %q", s) 21 | } 22 | *i = ipValue(ip) 23 | return nil 24 | } 25 | func (i *ipValue) Get() interface{} { 26 | return net.IP(*i) 27 | } 28 | 29 | // IPVar defines an net.IP flag with specified name, default value, and usage string. 30 | // The argument p points to an net.IP variable in which to store the value of the flag. 31 | func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) { 32 | f.VarP(newIPValue(value, p), name, "", usage) 33 | } 34 | 35 | // Like IPVar, but accepts a shorthand letter that can be used after a single dash. 36 | func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) { 37 | f.VarP(newIPValue(value, p), name, shorthand, usage) 38 | } 39 | 40 | // IPVar defines an net.IP flag with specified name, default value, and usage string. 41 | // The argument p points to an net.IP variable in which to store the value of the flag. 42 | func IPVar(p *net.IP, name string, value net.IP, usage string) { 43 | CommandLine.VarP(newIPValue(value, p), name, "", usage) 44 | } 45 | 46 | // Like IPVar, but accepts a shorthand letter that can be used after a single dash. 47 | func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) { 48 | CommandLine.VarP(newIPValue(value, p), name, shorthand, usage) 49 | } 50 | 51 | // IP defines an net.IP flag with specified name, default value, and usage string. 52 | // The return value is the address of an net.IP variable that stores the value of the flag. 53 | func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP { 54 | p := new(net.IP) 55 | f.IPVarP(p, name, "", value, usage) 56 | return p 57 | } 58 | 59 | // Like IP, but accepts a shorthand letter that can be used after a single dash. 60 | func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP { 61 | p := new(net.IP) 62 | f.IPVarP(p, name, shorthand, value, usage) 63 | return p 64 | } 65 | 66 | // IP defines an net.IP flag with specified name, default value, and usage string. 67 | // The return value is the address of an net.IP variable that stores the value of the flag. 68 | func IP(name string, value net.IP, usage string) *net.IP { 69 | return CommandLine.IPP(name, "", value, usage) 70 | } 71 | 72 | // Like IP, but accepts a shorthand letter that can be used after a single dash. 73 | func IPP(name, shorthand string, value net.IP, usage string) *net.IP { 74 | return CommandLine.IPP(name, shorthand, value, usage) 75 | } 76 | -------------------------------------------------------------------------------- /ipmask.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | ) 7 | 8 | // -- net.IPMask value 9 | type ipMaskValue net.IPMask 10 | 11 | func newIPMaskValue(val net.IPMask, p *net.IPMask) *ipMaskValue { 12 | *p = val 13 | return (*ipMaskValue)(p) 14 | } 15 | 16 | func (i *ipMaskValue) String() string { return net.IPMask(*i).String() } 17 | func (i *ipMaskValue) Set(s string) error { 18 | ip := ParseIPv4Mask(s) 19 | if ip == nil { 20 | return fmt.Errorf("failed to parse IP mask: %q", s) 21 | } 22 | *i = ipMaskValue(ip) 23 | return nil 24 | } 25 | func (i *ipMaskValue) Get() interface{} { 26 | return net.IPMask(*i) 27 | } 28 | 29 | // Parse IPv4 netmask written in IP form (e.g. 255.255.255.0). 30 | // This function should really belong to the net package. 31 | func ParseIPv4Mask(s string) net.IPMask { 32 | mask := net.ParseIP(s) 33 | if mask == nil { 34 | return nil 35 | } 36 | return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15]) 37 | } 38 | 39 | // IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string. 40 | // The argument p points to an net.IPMask variable in which to store the value of the flag. 41 | func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) { 42 | f.VarP(newIPMaskValue(value, p), name, "", usage) 43 | } 44 | 45 | // Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash. 46 | func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) { 47 | f.VarP(newIPMaskValue(value, p), name, shorthand, usage) 48 | } 49 | 50 | // IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string. 51 | // The argument p points to an net.IPMask variable in which to store the value of the flag. 52 | func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) { 53 | CommandLine.VarP(newIPMaskValue(value, p), name, "", usage) 54 | } 55 | 56 | // Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash. 57 | func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) { 58 | CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage) 59 | } 60 | 61 | // IPMask defines an net.IPMask flag with specified name, default value, and usage string. 62 | // The return value is the address of an net.IPMask variable that stores the value of the flag. 63 | func (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMask { 64 | p := new(net.IPMask) 65 | f.IPMaskVarP(p, name, "", value, usage) 66 | return p 67 | } 68 | 69 | // Like IPMask, but accepts a shorthand letter that can be used after a single dash. 70 | func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask { 71 | p := new(net.IPMask) 72 | f.IPMaskVarP(p, name, shorthand, value, usage) 73 | return p 74 | } 75 | 76 | // IPMask defines an net.IPMask flag with specified name, default value, and usage string. 77 | // The return value is the address of an net.IPMask variable that stores the value of the flag. 78 | func IPMask(name string, value net.IPMask, usage string) *net.IPMask { 79 | return CommandLine.IPMaskP(name, "", value, usage) 80 | } 81 | 82 | // Like IP, but accepts a shorthand letter that can be used after a single dash. 83 | func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask { 84 | return CommandLine.IPMaskP(name, shorthand, value, usage) 85 | } 86 | -------------------------------------------------------------------------------- /string.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import "fmt" 4 | 5 | // -- string Value 6 | type stringValue string 7 | 8 | func newStringValue(val string, p *string) *stringValue { 9 | *p = val 10 | return (*stringValue)(p) 11 | } 12 | 13 | func (s *stringValue) Set(val string) error { 14 | *s = stringValue(val) 15 | return nil 16 | } 17 | 18 | func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) } 19 | 20 | // StringVar defines a string flag with specified name, default value, and usage string. 21 | // The argument p points to a string variable in which to store the value of the flag. 22 | func (f *FlagSet) StringVar(p *string, name string, value string, usage string) { 23 | f.VarP(newStringValue(value, p), name, "", usage) 24 | } 25 | 26 | // Like StringVar, but accepts a shorthand letter that can be used after a single dash. 27 | func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) { 28 | f.VarP(newStringValue(value, p), name, shorthand, usage) 29 | } 30 | 31 | // StringVar defines a string flag with specified name, default value, and usage string. 32 | // The argument p points to a string variable in which to store the value of the flag. 33 | func StringVar(p *string, name string, value string, usage string) { 34 | CommandLine.VarP(newStringValue(value, p), name, "", usage) 35 | } 36 | 37 | // Like StringVar, but accepts a shorthand letter that can be used after a single dash. 38 | func StringVarP(p *string, name, shorthand string, value string, usage string) { 39 | CommandLine.VarP(newStringValue(value, p), name, shorthand, usage) 40 | } 41 | 42 | // String defines a string flag with specified name, default value, and usage string. 43 | // The return value is the address of a string variable that stores the value of the flag. 44 | func (f *FlagSet) String(name string, value string, usage string) *string { 45 | p := new(string) 46 | f.StringVarP(p, name, "", value, usage) 47 | return p 48 | } 49 | 50 | // Like String, but accepts a shorthand letter that can be used after a single dash. 51 | func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string { 52 | p := new(string) 53 | f.StringVarP(p, name, shorthand, value, usage) 54 | return p 55 | } 56 | 57 | // String defines a string flag with specified name, default value, and usage string. 58 | // The return value is the address of a string variable that stores the value of the flag. 59 | func String(name string, value string, usage string) *string { 60 | return CommandLine.StringP(name, "", value, usage) 61 | } 62 | 63 | // Like String, but accepts a shorthand letter that can be used after a single dash. 64 | func StringP(name, shorthand string, value string, usage string) *string { 65 | return CommandLine.StringP(name, shorthand, value, usage) 66 | } 67 | -------------------------------------------------------------------------------- /uint.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- uint Value 9 | type uintValue uint 10 | 11 | func newUintValue(val uint, p *uint) *uintValue { 12 | *p = val 13 | return (*uintValue)(p) 14 | } 15 | 16 | func (i *uintValue) Set(s string) error { 17 | v, err := strconv.ParseUint(s, 0, 64) 18 | *i = uintValue(v) 19 | return err 20 | } 21 | 22 | func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // UintVar defines a uint flag with specified name, default value, and usage string. 25 | // The argument p points to a uint variable in which to store the value of the flag. 26 | func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) { 27 | f.VarP(newUintValue(value, p), name, "", usage) 28 | } 29 | 30 | // Like UintVar, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string) { 32 | f.VarP(newUintValue(value, p), name, shorthand, usage) 33 | } 34 | 35 | // UintVar defines a uint flag with specified name, default value, and usage string. 36 | // The argument p points to a uint variable in which to store the value of the flag. 37 | func UintVar(p *uint, name string, value uint, usage string) { 38 | CommandLine.VarP(newUintValue(value, p), name, "", usage) 39 | } 40 | 41 | // Like UintVar, but accepts a shorthand letter that can be used after a single dash. 42 | func UintVarP(p *uint, name, shorthand string, value uint, usage string) { 43 | CommandLine.VarP(newUintValue(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Uint defines a uint flag with specified name, default value, and usage string. 47 | // The return value is the address of a uint variable that stores the value of the flag. 48 | func (f *FlagSet) Uint(name string, value uint, usage string) *uint { 49 | p := new(uint) 50 | f.UintVarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Uint, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint { 56 | p := new(uint) 57 | f.UintVarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Uint defines a uint flag with specified name, default value, and usage string. 62 | // The return value is the address of a uint variable that stores the value of the flag. 63 | func Uint(name string, value uint, usage string) *uint { 64 | return CommandLine.UintP(name, "", value, usage) 65 | } 66 | 67 | // Like Uint, but accepts a shorthand letter that can be used after a single dash. 68 | func UintP(name, shorthand string, value uint, usage string) *uint { 69 | return CommandLine.UintP(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /uint16.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- uint16 value 9 | type uint16Value uint16 10 | 11 | func newUint16Value(val uint16, p *uint16) *uint16Value { 12 | *p = val 13 | return (*uint16Value)(p) 14 | } 15 | func (i *uint16Value) String() string { return fmt.Sprintf("%d", *i) } 16 | func (i *uint16Value) Set(s string) error { 17 | v, err := strconv.ParseUint(s, 0, 16) 18 | *i = uint16Value(v) 19 | return err 20 | } 21 | func (i *uint16Value) Get() interface{} { 22 | return uint16(*i) 23 | } 24 | 25 | // Uint16Var defines a uint flag with specified name, default value, and usage string. 26 | // The argument p points to a uint variable in which to store the value of the flag. 27 | func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string) { 28 | f.VarP(newUint16Value(value, p), name, "", usage) 29 | } 30 | 31 | // Like Uint16Var, but accepts a shorthand letter that can be used after a single dash. 32 | func (f *FlagSet) Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) { 33 | f.VarP(newUint16Value(value, p), name, shorthand, usage) 34 | } 35 | 36 | // Uint16Var defines a uint flag with specified name, default value, and usage string. 37 | // The argument p points to a uint variable in which to store the value of the flag. 38 | func Uint16Var(p *uint16, name string, value uint16, usage string) { 39 | CommandLine.VarP(newUint16Value(value, p), name, "", usage) 40 | } 41 | 42 | // Like Uint16Var, but accepts a shorthand letter that can be used after a single dash. 43 | func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) { 44 | CommandLine.VarP(newUint16Value(value, p), name, shorthand, usage) 45 | } 46 | 47 | // Uint16 defines a uint flag with specified name, default value, and usage string. 48 | // The return value is the address of a uint variable that stores the value of the flag. 49 | func (f *FlagSet) Uint16(name string, value uint16, usage string) *uint16 { 50 | p := new(uint16) 51 | f.Uint16VarP(p, name, "", value, usage) 52 | return p 53 | } 54 | 55 | // Like Uint16, but accepts a shorthand letter that can be used after a single dash. 56 | func (f *FlagSet) Uint16P(name, shorthand string, value uint16, usage string) *uint16 { 57 | p := new(uint16) 58 | f.Uint16VarP(p, name, shorthand, value, usage) 59 | return p 60 | } 61 | 62 | // Uint16 defines a uint flag with specified name, default value, and usage string. 63 | // The return value is the address of a uint variable that stores the value of the flag. 64 | func Uint16(name string, value uint16, usage string) *uint16 { 65 | return CommandLine.Uint16P(name, "", value, usage) 66 | } 67 | 68 | // Like Uint16, but accepts a shorthand letter that can be used after a single dash. 69 | func Uint16P(name, shorthand string, value uint16, usage string) *uint16 { 70 | return CommandLine.Uint16P(name, shorthand, value, usage) 71 | } 72 | -------------------------------------------------------------------------------- /uint32.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- uint16 value 9 | type uint32Value uint32 10 | 11 | func newUint32Value(val uint32, p *uint32) *uint32Value { 12 | *p = val 13 | return (*uint32Value)(p) 14 | } 15 | func (i *uint32Value) String() string { return fmt.Sprintf("%d", *i) } 16 | func (i *uint32Value) Set(s string) error { 17 | v, err := strconv.ParseUint(s, 0, 32) 18 | *i = uint32Value(v) 19 | return err 20 | } 21 | func (i *uint32Value) Get() interface{} { 22 | return uint32(*i) 23 | } 24 | 25 | // Uint32Var defines a uint32 flag with specified name, default value, and usage string. 26 | // The argument p points to a uint32 variable in which to store the value of the flag. 27 | func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string) { 28 | f.VarP(newUint32Value(value, p), name, "", usage) 29 | } 30 | 31 | // Like Uint32Var, but accepts a shorthand letter that can be used after a single dash. 32 | func (f *FlagSet) Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) { 33 | f.VarP(newUint32Value(value, p), name, shorthand, usage) 34 | } 35 | 36 | // Uint32Var defines a uint32 flag with specified name, default value, and usage string. 37 | // The argument p points to a uint32 variable in which to store the value of the flag. 38 | func Uint32Var(p *uint32, name string, value uint32, usage string) { 39 | CommandLine.VarP(newUint32Value(value, p), name, "", usage) 40 | } 41 | 42 | // Like Uint32Var, but accepts a shorthand letter that can be used after a single dash. 43 | func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) { 44 | CommandLine.VarP(newUint32Value(value, p), name, shorthand, usage) 45 | } 46 | 47 | // Uint32 defines a uint32 flag with specified name, default value, and usage string. 48 | // The return value is the address of a uint32 variable that stores the value of the flag. 49 | func (f *FlagSet) Uint32(name string, value uint32, usage string) *uint32 { 50 | p := new(uint32) 51 | f.Uint32VarP(p, name, "", value, usage) 52 | return p 53 | } 54 | 55 | // Like Uint32, but accepts a shorthand letter that can be used after a single dash. 56 | func (f *FlagSet) Uint32P(name, shorthand string, value uint32, usage string) *uint32 { 57 | p := new(uint32) 58 | f.Uint32VarP(p, name, shorthand, value, usage) 59 | return p 60 | } 61 | 62 | // Uint32 defines a uint32 flag with specified name, default value, and usage string. 63 | // The return value is the address of a uint32 variable that stores the value of the flag. 64 | func Uint32(name string, value uint32, usage string) *uint32 { 65 | return CommandLine.Uint32P(name, "", value, usage) 66 | } 67 | 68 | // Like Uint32, but accepts a shorthand letter that can be used after a single dash. 69 | func Uint32P(name, shorthand string, value uint32, usage string) *uint32 { 70 | return CommandLine.Uint32P(name, shorthand, value, usage) 71 | } 72 | -------------------------------------------------------------------------------- /uint64.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- uint64 Value 9 | type uint64Value uint64 10 | 11 | func newUint64Value(val uint64, p *uint64) *uint64Value { 12 | *p = val 13 | return (*uint64Value)(p) 14 | } 15 | 16 | func (i *uint64Value) Set(s string) error { 17 | v, err := strconv.ParseUint(s, 0, 64) 18 | *i = uint64Value(v) 19 | return err 20 | } 21 | 22 | func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // Uint64Var defines a uint64 flag with specified name, default value, and usage string. 25 | // The argument p points to a uint64 variable in which to store the value of the flag. 26 | func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) { 27 | f.VarP(newUint64Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Uint64Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { 32 | f.VarP(newUint64Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Uint64Var defines a uint64 flag with specified name, default value, and usage string. 36 | // The argument p points to a uint64 variable in which to store the value of the flag. 37 | func Uint64Var(p *uint64, name string, value uint64, usage string) { 38 | CommandLine.VarP(newUint64Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Uint64Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { 43 | CommandLine.VarP(newUint64Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Uint64 defines a uint64 flag with specified name, default value, and usage string. 47 | // The return value is the address of a uint64 variable that stores the value of the flag. 48 | func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 { 49 | p := new(uint64) 50 | f.Uint64VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Uint64, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Uint64P(name, shorthand string, value uint64, usage string) *uint64 { 56 | p := new(uint64) 57 | f.Uint64VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Uint64 defines a uint64 flag with specified name, default value, and usage string. 62 | // The return value is the address of a uint64 variable that stores the value of the flag. 63 | func Uint64(name string, value uint64, usage string) *uint64 { 64 | return CommandLine.Uint64P(name, "", value, usage) 65 | } 66 | 67 | // Like Uint64, but accepts a shorthand letter that can be used after a single dash. 68 | func Uint64P(name, shorthand string, value uint64, usage string) *uint64 { 69 | return CommandLine.Uint64P(name, shorthand, value, usage) 70 | } 71 | -------------------------------------------------------------------------------- /uint8.go: -------------------------------------------------------------------------------- 1 | package pflag 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // -- uint8 Value 9 | type uint8Value uint8 10 | 11 | func newUint8Value(val uint8, p *uint8) *uint8Value { 12 | *p = val 13 | return (*uint8Value)(p) 14 | } 15 | 16 | func (i *uint8Value) Set(s string) error { 17 | v, err := strconv.ParseUint(s, 0, 8) 18 | *i = uint8Value(v) 19 | return err 20 | } 21 | 22 | func (i *uint8Value) String() string { return fmt.Sprintf("%v", *i) } 23 | 24 | // Uint8Var defines a uint8 flag with specified name, default value, and usage string. 25 | // The argument p points to a uint8 variable in which to store the value of the flag. 26 | func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string) { 27 | f.VarP(newUint8Value(value, p), name, "", usage) 28 | } 29 | 30 | // Like Uint8Var, but accepts a shorthand letter that can be used after a single dash. 31 | func (f *FlagSet) Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) { 32 | f.VarP(newUint8Value(value, p), name, shorthand, usage) 33 | } 34 | 35 | // Uint8Var defines a uint8 flag with specified name, default value, and usage string. 36 | // The argument p points to a uint8 variable in which to store the value of the flag. 37 | func Uint8Var(p *uint8, name string, value uint8, usage string) { 38 | CommandLine.VarP(newUint8Value(value, p), name, "", usage) 39 | } 40 | 41 | // Like Uint8Var, but accepts a shorthand letter that can be used after a single dash. 42 | func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) { 43 | CommandLine.VarP(newUint8Value(value, p), name, shorthand, usage) 44 | } 45 | 46 | // Uint8 defines a uint8 flag with specified name, default value, and usage string. 47 | // The return value is the address of a uint8 variable that stores the value of the flag. 48 | func (f *FlagSet) Uint8(name string, value uint8, usage string) *uint8 { 49 | p := new(uint8) 50 | f.Uint8VarP(p, name, "", value, usage) 51 | return p 52 | } 53 | 54 | // Like Uint8, but accepts a shorthand letter that can be used after a single dash. 55 | func (f *FlagSet) Uint8P(name, shorthand string, value uint8, usage string) *uint8 { 56 | p := new(uint8) 57 | f.Uint8VarP(p, name, shorthand, value, usage) 58 | return p 59 | } 60 | 61 | // Uint8 defines a uint8 flag with specified name, default value, and usage string. 62 | // The return value is the address of a uint8 variable that stores the value of the flag. 63 | func Uint8(name string, value uint8, usage string) *uint8 { 64 | return CommandLine.Uint8P(name, "", value, usage) 65 | } 66 | 67 | // Like Uint8, but accepts a shorthand letter that can be used after a single dash. 68 | func Uint8P(name, shorthand string, value uint8, usage string) *uint8 { 69 | return CommandLine.Uint8P(name, shorthand, value, usage) 70 | } 71 | --------------------------------------------------------------------------------