mirror of https://github.com/k3s-io/k3s
Update godeps for cobra and pflags
parent
02eb68aa0b
commit
3fe85d46ff
|
@ -480,12 +480,12 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "github.com/spf13/cobra",
|
||||
"Rev": "8f5946caaeeff40a98d67f60c25e89c3525038a3"
|
||||
"Rev": "385fc87e4343efec233811d3d933509e8975d11a"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/spf13/pflag",
|
||||
"Comment": "v0.0.1-44-gb91b2a9",
|
||||
"Rev": "b91b2a94780f4e6b4d3b0c12fd9b5f4b05b1aa45"
|
||||
"Comment": "v0.0.1-73-g534019b",
|
||||
"Rev": "534019bcaea096fc0f0641afa2aed1e80cbb0ccc"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/stretchr/objx",
|
||||
|
|
|
@ -62,7 +62,7 @@ and flags that are only available to that command.
|
|||
In the example above 'port' is the flag.
|
||||
|
||||
Flag functionality is provided by the [pflag
|
||||
libary](https://github.com/ogier/pflag), a fork of the flag standard library
|
||||
library](https://github.com/ogier/pflag), a fork of the flag standard library
|
||||
which maintains the same interface while adding posix compliance.
|
||||
|
||||
## Usage
|
||||
|
|
|
@ -219,9 +219,13 @@ func writeFlagHandler(name string, annotations map[string][]string, out *bytes.B
|
|||
case BashCompFilenameExt:
|
||||
fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)
|
||||
|
||||
ext := strings.Join(value, "|")
|
||||
ext = "__handle_filename_extension_flag " + ext
|
||||
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||
if len(value) > 0 {
|
||||
ext := "__handle_filename_extension_flag " + strings.Join(value, "|")
|
||||
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||
} else {
|
||||
ext := "_filedir"
|
||||
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -343,15 +347,24 @@ func (cmd *Command) GenBashCompletionFile(filename string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (cmd *Command) MarkFlagRequired(name string) {
|
||||
flag := cmd.Flags().Lookup(name)
|
||||
if flag == nil {
|
||||
return
|
||||
}
|
||||
if flag.Annotations == nil {
|
||||
flag.Annotations = make(map[string][]string)
|
||||
}
|
||||
annotation := make([]string, 1)
|
||||
annotation[0] = "true"
|
||||
flag.Annotations[BashCompOneRequiredFlag] = annotation
|
||||
// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag, if it exists.
|
||||
func (cmd *Command) MarkFlagRequired(name string) error {
|
||||
return MarkFlagRequired(cmd.Flags(), name)
|
||||
}
|
||||
|
||||
// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.
|
||||
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
|
||||
return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
|
||||
}
|
||||
|
||||
// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists.
|
||||
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
|
||||
func (cmd *Command) MarkFlagFilename(name string, extensions ...string) error {
|
||||
return MarkFlagFilename(cmd.Flags(), name, extensions...)
|
||||
}
|
||||
|
||||
// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
|
||||
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
|
||||
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
|
||||
return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ and you'll get something like
|
|||
-c --container= -p --pod=
|
||||
```
|
||||
|
||||
# Specify valid filename extentions for flags that take a filename
|
||||
# Specify valid filename extensions for flags that take a filename
|
||||
|
||||
In this example we use --filename= and expect to get a json or yaml file as the argument. To make this easier we annotate the --filename flag with valid filename extensions.
|
||||
|
||||
|
|
|
@ -42,23 +42,19 @@ func TestBashCompletions(t *testing.T) {
|
|||
// required flag
|
||||
c.MarkFlagRequired("introot")
|
||||
|
||||
// valid nounds
|
||||
// valid nouns
|
||||
validArgs := []string{"pods", "nodes", "services", "replicationControllers"}
|
||||
c.ValidArgs = validArgs
|
||||
|
||||
// filename extentions
|
||||
annotations := make([]string, 3)
|
||||
annotations[0] = "json"
|
||||
annotations[1] = "yaml"
|
||||
annotations[2] = "yml"
|
||||
|
||||
annotation := make(map[string][]string)
|
||||
annotation[BashCompFilenameExt] = annotations
|
||||
|
||||
// filename
|
||||
var flagval string
|
||||
c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")
|
||||
flag := c.Flags().Lookup("filename")
|
||||
flag.Annotations = annotation
|
||||
c.MarkFlagFilename("filename", "json", "yaml", "yml")
|
||||
|
||||
// filename extensions
|
||||
var flagvalExt string
|
||||
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
||||
c.MarkFlagFilename("filename-ext")
|
||||
|
||||
out := new(bytes.Buffer)
|
||||
c.GenBashCompletion(out)
|
||||
|
@ -75,7 +71,9 @@ func TestBashCompletions(t *testing.T) {
|
|||
check(t, str, `COMPREPLY=( "hello" )`)
|
||||
// check for required nouns
|
||||
check(t, str, `must_have_one_noun+=("pods")`)
|
||||
// check for filename extention flags
|
||||
// check for filename extension flags
|
||||
check(t, str, `flags_completion+=("_filedir")`)
|
||||
// check for filename extension flags
|
||||
check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)
|
||||
|
||||
checkOmit(t, str, cmdDeprecated.Name())
|
||||
|
|
|
@ -378,6 +378,24 @@ func TestChildSameName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGrandChildSameName(t *testing.T) {
|
||||
c := initializeWithSameName()
|
||||
cmdTimes.AddCommand(cmdPrint)
|
||||
c.AddCommand(cmdTimes)
|
||||
c.SetArgs(strings.Split("times print one two", " "))
|
||||
c.Execute()
|
||||
|
||||
if te != nil || tt != nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if tp == nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if strings.Join(tp, " ") != "one two" {
|
||||
t.Error("Command didn't parse correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagLong(t *testing.T) {
|
||||
noRRSetupTest("echo --intone=13 something here")
|
||||
|
||||
|
@ -750,11 +768,16 @@ func TestRootNoCommandHelp(t *testing.T) {
|
|||
|
||||
func TestRootUnknownCommand(t *testing.T) {
|
||||
r := noRRSetupTest("bogus")
|
||||
s := "Error: unknown command \"bogus\"\nRun 'cobra-test help' for usage.\n"
|
||||
s := "Error: unknown command \"bogus\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n"
|
||||
|
||||
if r.Output != s {
|
||||
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
|
||||
}
|
||||
|
||||
r = noRRSetupTest("--strtwo=a bogus")
|
||||
if r.Output != s {
|
||||
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagsBeforeCommand(t *testing.T) {
|
||||
|
|
|
@ -84,7 +84,6 @@ type Command struct {
|
|||
commandsMaxNameLen int
|
||||
|
||||
flagErrorBuf *bytes.Buffer
|
||||
cmdErrorBuf *bytes.Buffer
|
||||
|
||||
args []string // actual args parsed from flags
|
||||
output *io.Writer // nil means stderr; use Out() method instead
|
||||
|
@ -260,21 +259,21 @@ Aliases:
|
|||
{{end}}{{if .HasExample}}
|
||||
|
||||
Examples:
|
||||
{{ .Example }}
|
||||
{{end}}{{ if .HasRunnableSubCommands}}
|
||||
{{ .Example }}{{end}}{{ if .HasNonHelpSubCommands}}
|
||||
|
||||
Available Commands: {{range .Commands}}{{if and (.Runnable) (not .Deprecated)}}
|
||||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
|
||||
{{end}}
|
||||
{{ if .HasLocalFlags}}Flags:
|
||||
{{.LocalFlags.FlagUsages}}{{end}}
|
||||
{{ if .HasInheritedFlags}}Global Flags:
|
||||
{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
|
||||
Additional help topics:
|
||||
{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}
|
||||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
||||
{{end}}{{ if .HasSubCommands }}
|
||||
Use "{{.Root.Name}} help [command]" for more information about a command.
|
||||
Available Commands: {{range .Commands}}{{if (not .IsHelpCommand)}}
|
||||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}
|
||||
|
||||
Flags:
|
||||
{{.LocalFlags.FlagUsages}}{{end}}{{ if .HasInheritedFlags}}
|
||||
|
||||
Global Flags:
|
||||
{{.InheritedFlags.FlagUsages}}{{end}}{{if .HasHelpSubCommands}}
|
||||
|
||||
Additional help topics: {{range .Commands}}{{if .IsHelpCommand}}
|
||||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}}{{end}}{{end}}{{ if .HasSubCommands }}
|
||||
|
||||
Use "{{.CommandPath}} [command] --help" for more information about a command.
|
||||
{{end}}`
|
||||
}
|
||||
}
|
||||
|
@ -381,50 +380,49 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
|||
return nil, nil, fmt.Errorf("Called find() on a nil Command")
|
||||
}
|
||||
|
||||
// If there are no arguments, return the root command. If the root has no
|
||||
// subcommands, args reflects arguments that should actually be passed to
|
||||
// the root command, so also return the root command.
|
||||
if len(args) == 0 || !c.Root().HasSubCommands() {
|
||||
return c.Root(), args, nil
|
||||
}
|
||||
|
||||
var innerfind func(*Command, []string) (*Command, []string)
|
||||
|
||||
innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
|
||||
if len(innerArgs) > 0 && c.HasSubCommands() {
|
||||
argsWOflags := stripFlags(innerArgs, c)
|
||||
if len(argsWOflags) > 0 {
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, argsWOflags[0]))
|
||||
} else if EnablePrefixMatching {
|
||||
if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
for _, x := range cmd.Aliases {
|
||||
if strings.HasPrefix(x, argsWOflags[0]) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
argsWOflags := stripFlags(innerArgs, c)
|
||||
if len(argsWOflags) == 0 {
|
||||
return c, innerArgs
|
||||
}
|
||||
nextSubCmd := argsWOflags[0]
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
||||
}
|
||||
if EnablePrefixMatching {
|
||||
if strings.HasPrefix(cmd.Name(), nextSubCmd) { // prefix match
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
for _, x := range cmd.Aliases {
|
||||
if strings.HasPrefix(x, nextSubCmd) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// only accept a single prefix match - multiple matches would be ambiguous
|
||||
if len(matches) == 1 {
|
||||
return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// only accept a single prefix match - multiple matches would be ambiguous
|
||||
if len(matches) == 1 {
|
||||
return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
|
||||
}
|
||||
|
||||
return c, innerArgs
|
||||
}
|
||||
|
||||
commandFound, a := innerfind(c, args)
|
||||
argsWOflags := stripFlags(a, commandFound)
|
||||
|
||||
// If we matched on the root, but we asked for a subcommand, return an error
|
||||
if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] {
|
||||
return nil, a, fmt.Errorf("unknown command %q", a[0])
|
||||
// no subcommand, always take args
|
||||
if !commandFound.HasSubCommands() {
|
||||
return commandFound, a, nil
|
||||
}
|
||||
// root command with subcommands, do subcommand checking
|
||||
if commandFound == c && len(argsWOflags) > 0 {
|
||||
return commandFound, a, fmt.Errorf("unknown command %q for %q", argsWOflags[0], commandFound.CommandPath())
|
||||
}
|
||||
|
||||
return commandFound, a, nil
|
||||
|
@ -454,29 +452,13 @@ func (c *Command) execute(a []string) (err error) {
|
|||
}
|
||||
|
||||
err = c.ParseFlags(a)
|
||||
if err == flag.ErrHelp {
|
||||
c.Help()
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
// We're writing subcommand usage to root command's error buffer to have it displayed to the user
|
||||
r := c.Root()
|
||||
if r.cmdErrorBuf == nil {
|
||||
r.cmdErrorBuf = new(bytes.Buffer)
|
||||
}
|
||||
// for writing the usage to the buffer we need to switch the output temporarily
|
||||
// since Out() returns root output, you also need to revert that on root
|
||||
out := r.Out()
|
||||
r.SetOutput(r.cmdErrorBuf)
|
||||
c.Usage()
|
||||
r.SetOutput(out)
|
||||
return err
|
||||
}
|
||||
// If help is called, regardless of other flags, we print that.
|
||||
// Print help also if c.Run is nil.
|
||||
// If help is called, regardless of other flags, return we want help
|
||||
// Also say we need help if c.Run is nil.
|
||||
if c.helpFlagVal || !c.Runnable() {
|
||||
c.Help()
|
||||
return nil
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
||||
c.preRun()
|
||||
|
@ -556,18 +538,24 @@ func (c *Command) Execute() (err error) {
|
|||
}
|
||||
|
||||
cmd, flags, err := c.Find(args)
|
||||
if err == nil {
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// If found parse to a subcommand and then failed, talk about the subcommand
|
||||
if cmd != nil {
|
||||
c = cmd
|
||||
}
|
||||
c.Println("Error:", err.Error())
|
||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||
return err
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
if err == flag.ErrHelp {
|
||||
c.Help()
|
||||
|
||||
} else {
|
||||
c.Println("Error:", err.Error())
|
||||
c.Printf("Run '%v help' for usage.\n", c.Root().Name())
|
||||
cmd.Help()
|
||||
return nil
|
||||
}
|
||||
c.Println(cmd.UsageString())
|
||||
c.Println("Error:", err.Error())
|
||||
}
|
||||
|
||||
return
|
||||
|
@ -596,8 +584,6 @@ func (c *Command) initHelp() {
|
|||
func (c *Command) ResetCommands() {
|
||||
c.commands = nil
|
||||
c.helpCommand = nil
|
||||
c.cmdErrorBuf = new(bytes.Buffer)
|
||||
c.cmdErrorBuf.Reset()
|
||||
}
|
||||
|
||||
//Commands returns a slice of child commands.
|
||||
|
@ -817,31 +803,39 @@ func (c *Command) HasSubCommands() bool {
|
|||
return len(c.commands) > 0
|
||||
}
|
||||
|
||||
func (c *Command) HasRunnableSiblings() bool {
|
||||
if !c.HasParent() {
|
||||
func (c *Command) IsHelpCommand() bool {
|
||||
if c.Runnable() {
|
||||
return false
|
||||
}
|
||||
for _, sub := range c.parent.commands {
|
||||
if sub.Runnable() {
|
||||
return true
|
||||
for _, sub := range c.commands {
|
||||
if len(sub.Deprecated) != 0 {
|
||||
continue
|
||||
}
|
||||
if !sub.IsHelpCommand() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Command) HasHelpSubCommands() bool {
|
||||
for _, sub := range c.commands {
|
||||
if !sub.Runnable() {
|
||||
if len(sub.Deprecated) != 0 {
|
||||
continue
|
||||
}
|
||||
if sub.IsHelpCommand() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Determine if the command has runnable children commands
|
||||
func (c *Command) HasRunnableSubCommands() bool {
|
||||
func (c *Command) HasNonHelpSubCommands() bool {
|
||||
for _, sub := range c.commands {
|
||||
if sub.Runnable() {
|
||||
if len(sub.Deprecated) != 0 {
|
||||
continue
|
||||
}
|
||||
if !sub.IsHelpCommand() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,11 +20,11 @@ pflag is available using the standard `go get` command.
|
|||
|
||||
Install by running:
|
||||
|
||||
go get github.com/ogier/pflag
|
||||
go get github.com/spf13/pflag
|
||||
|
||||
Run tests by running:
|
||||
|
||||
go test github.com/ogier/pflag
|
||||
go test github.com/spf13/pflag
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -33,7 +33,7 @@ pflag under the name "flag" then all code should continue to function
|
|||
with no changes.
|
||||
|
||||
``` go
|
||||
import flag "github.com/ogier/pflag"
|
||||
import flag "github.com/spf13/pflag"
|
||||
```
|
||||
|
||||
There is one exception to this: if you directly instantiate the Flag struct
|
||||
|
@ -84,6 +84,16 @@ fmt.Println("ip has value ", *ip)
|
|||
fmt.Println("flagvar has value ", flagvar)
|
||||
```
|
||||
|
||||
There are helpers function to get values later if you have the FlagSet but
|
||||
it was difficult to keep up with all of the the flag pointers in your code.
|
||||
If you have a pflag.FlagSet with a flag called 'flagname' of type int you
|
||||
can use GetInt() to get the int value. But notice that 'flagname' must exist
|
||||
and it must be an int. GetString("flagname") will fail.
|
||||
|
||||
``` go
|
||||
i, err := flagset.GetInt("flagname")
|
||||
```
|
||||
|
||||
After parsing, the arguments after the flag are available as the
|
||||
slice flag.Args() or individually as flag.Arg(i).
|
||||
The arguments are indexed from 0 through flag.NArg()-1.
|
||||
|
@ -111,29 +121,56 @@ in a command-line interface. The methods of FlagSet are
|
|||
analogous to the top-level functions for the command-line
|
||||
flag set.
|
||||
|
||||
## Setting no option default values for flags
|
||||
|
||||
After you create a flag it is possible to set the pflag.NoOptDefVal for
|
||||
the given flag. Doing this changes the meaning of the flag slightly. If
|
||||
a flag has a NoOptDefVal and the flag is set on the command line without
|
||||
an option the flag will be set to the NoOptDefVal. For example given:
|
||||
|
||||
``` go
|
||||
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||
flag.Lookup("flagname").NoOptDefVal = "4321"
|
||||
```
|
||||
|
||||
Would result in something like
|
||||
|
||||
| Parsed Arguments | Resulting Value |
|
||||
| ------------- | ------------- |
|
||||
| --flagname=1357 | ip=1357 |
|
||||
| --flagname | ip=4321 |
|
||||
| [nothing] | ip=1234 |
|
||||
|
||||
## Command line flag syntax
|
||||
|
||||
```
|
||||
--flag // boolean flags only
|
||||
--flag // boolean flags, or flags with no option default values
|
||||
--flag x // only on flags without a default value
|
||||
--flag=x
|
||||
```
|
||||
|
||||
Unlike the flag package, a single dash before an option means something
|
||||
different than a double dash. Single dashes signify a series of shorthand
|
||||
letters for flags. All but the last shorthand letter must be boolean flags.
|
||||
letters for flags. All but the last shorthand letter must be boolean flags
|
||||
or a flag with a default value
|
||||
|
||||
```
|
||||
// boolean flags
|
||||
// boolean or flags where the 'no option default value' is set
|
||||
-f
|
||||
-f=true
|
||||
-abc
|
||||
but
|
||||
-b true is INVALID
|
||||
|
||||
// non-boolean flags
|
||||
// non-boolean and flags without a 'no option default value'
|
||||
-n 1234
|
||||
-Ifile
|
||||
-n=1234
|
||||
-n1234
|
||||
|
||||
// mixed
|
||||
-abcs "hello"
|
||||
-abcn1234
|
||||
-absd="hello"
|
||||
-abcs1234
|
||||
```
|
||||
|
||||
Flag parsing stops after the terminator "--". Unlike the flag package,
|
||||
|
@ -151,7 +188,7 @@ It is possible to set a custom flag name 'normalization function.' It allows fla
|
|||
|
||||
**Example #1**: You want -, _, and . in flags to compare the same. aka --my-flag == --my_flag == --my.flag
|
||||
|
||||
```go
|
||||
``` go
|
||||
func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
from := []string{"-", "_"}
|
||||
to := "."
|
||||
|
@ -166,7 +203,7 @@ myFlagSet.SetNormalizeFunc(wordSepNormalizeFunc)
|
|||
|
||||
**Example #2**: You want to alias two flags. aka --old-flag-name == --new-flag-name
|
||||
|
||||
```go
|
||||
``` go
|
||||
func aliasNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
switch name {
|
||||
case "old-flag-name":
|
||||
|
|
|
@ -34,34 +34,47 @@ func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
|
|||
|
||||
func (b *boolValue) IsBoolFlag() bool { return true }
|
||||
|
||||
func boolConv(sval string) (interface{}, error) {
|
||||
return strconv.ParseBool(sval)
|
||||
}
|
||||
|
||||
// GetBool return the bool value of a flag with the given name
|
||||
func (f *FlagSet) GetBool(name string) (bool, error) {
|
||||
val, err := f.getFlagType(name, "bool", boolConv)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return val.(bool), nil
|
||||
}
|
||||
|
||||
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
||||
// The argument p points to a bool variable in which to store the value of the flag.
|
||||
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
|
||||
f.VarP(newBoolValue(value, p), name, "", usage)
|
||||
f.BoolVarP(p, name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like BoolVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
|
||||
f.VarP(newBoolValue(value, p), name, shorthand, usage)
|
||||
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
|
||||
flag.NoOptDefVal = "true"
|
||||
}
|
||||
|
||||
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
||||
// The argument p points to a bool variable in which to store the value of the flag.
|
||||
func BoolVar(p *bool, name string, value bool, usage string) {
|
||||
CommandLine.VarP(newBoolValue(value, p), name, "", usage)
|
||||
BoolVarP(p, name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like BoolVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
|
||||
CommandLine.VarP(newBoolValue(value, p), name, shorthand, usage)
|
||||
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
|
||||
flag.NoOptDefVal = "true"
|
||||
}
|
||||
|
||||
// Bool defines a bool flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a bool variable that stores the value of the flag.
|
||||
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
|
||||
p := new(bool)
|
||||
f.BoolVarP(p, name, "", value, usage)
|
||||
return p
|
||||
return f.BoolP(name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like Bool, but accepts a shorthand letter that can be used after a single dash.
|
||||
|
@ -74,10 +87,11 @@ func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool
|
|||
// Bool defines a bool flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a bool variable that stores the value of the flag.
|
||||
func Bool(name string, value bool, usage string) *bool {
|
||||
return CommandLine.BoolP(name, "", value, usage)
|
||||
return BoolP(name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like Bool, but accepts a shorthand letter that can be used after a single dash.
|
||||
func BoolP(name, shorthand string, value bool, usage string) *bool {
|
||||
return CommandLine.BoolP(name, shorthand, value, usage)
|
||||
b := CommandLine.BoolP(name, shorthand, value, usage)
|
||||
return b
|
||||
}
|
||||
|
|
|
@ -59,7 +59,8 @@ func (v *triStateValue) Type() string {
|
|||
func setUpFlagSet(tristate *triStateValue) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
*tristate = triStateFalse
|
||||
f.VarP(tristate, "tristate", "t", "tristate value (true, maybe or false)")
|
||||
flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
|
||||
flag.NoOptDefVal = "true"
|
||||
return f
|
||||
}
|
||||
|
||||
|
@ -162,3 +163,18 @@ func TestInvalidValue(t *testing.T) {
|
|||
t.Fatal("expected an error but did not get any, tristate has value", tristate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolP(t *testing.T) {
|
||||
b := BoolP("bool", "b", false, "bool value in CommandLine")
|
||||
c := BoolP("c", "c", false, "other bool value")
|
||||
args := []string{"--bool"}
|
||||
if err := CommandLine.Parse(args); err != nil {
|
||||
t.Error("expected no error, got ", err)
|
||||
}
|
||||
if *b != true {
|
||||
t.Errorf("expected b=true got b=%s", b)
|
||||
}
|
||||
if *c != false {
|
||||
t.Errorf("expect c=false got c=%s", c)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// -- count Value
|
||||
type countValue int
|
||||
|
||||
func newCountValue(val int, p *int) *countValue {
|
||||
*p = val
|
||||
return (*countValue)(p)
|
||||
}
|
||||
|
||||
func (i *countValue) Set(s string) error {
|
||||
v, err := strconv.ParseInt(s, 0, 64)
|
||||
// -1 means that no specific value was passed, so increment
|
||||
if v == -1 {
|
||||
*i = countValue(*i + 1)
|
||||
} else {
|
||||
*i = countValue(v)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (i *countValue) Type() string {
|
||||
return "count"
|
||||
}
|
||||
|
||||
func (i *countValue) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func countConv(sval string) (interface{}, error) {
|
||||
i, err := strconv.Atoi(sval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (f *FlagSet) GetCount(name string) (int, error) {
|
||||
val, err := f.getFlagType(name, "count", countConv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(int), nil
|
||||
}
|
||||
|
||||
func (f *FlagSet) CountVar(p *int, name string, usage string) {
|
||||
f.CountVarP(p, name, "", usage)
|
||||
}
|
||||
|
||||
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
|
||||
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
|
||||
flag.NoOptDefVal = "-1"
|
||||
}
|
||||
|
||||
func CountVar(p *int, name string, usage string) {
|
||||
CommandLine.CountVar(p, name, usage)
|
||||
}
|
||||
|
||||
func CountVarP(p *int, name, shorthand string, usage string) {
|
||||
CommandLine.CountVarP(p, name, shorthand, usage)
|
||||
}
|
||||
|
||||
func (f *FlagSet) Count(name string, usage string) *int {
|
||||
p := new(int)
|
||||
f.CountVarP(p, name, "", usage)
|
||||
return p
|
||||
}
|
||||
|
||||
func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
|
||||
p := new(int)
|
||||
f.CountVarP(p, name, shorthand, usage)
|
||||
return p
|
||||
}
|
||||
|
||||
func Count(name string, usage string) *int {
|
||||
return CommandLine.CountP(name, "", usage)
|
||||
}
|
||||
|
||||
func CountP(name, shorthand string, usage string) *int {
|
||||
return CommandLine.CountP(name, shorthand, usage)
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = fmt.Printf
|
||||
|
||||
func setUpCount(c *int) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
f.CountVarP(c, "verbose", "v", "a counter")
|
||||
return f
|
||||
}
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input []string
|
||||
success bool
|
||||
expected int
|
||||
}{
|
||||
{[]string{"-vvv"}, true, 3},
|
||||
{[]string{"-v", "-v", "-v"}, true, 3},
|
||||
{[]string{"-v", "--verbose", "-v"}, true, 3},
|
||||
{[]string{"-v=3", "-v"}, true, 4},
|
||||
{[]string{"-v=a"}, false, 0},
|
||||
}
|
||||
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
os.Stderr = devnull
|
||||
for i := range testCases {
|
||||
var count int
|
||||
f := setUpCount(&count)
|
||||
|
||||
tc := &testCases[i]
|
||||
|
||||
err := f.Parse(tc.input)
|
||||
if err != nil && tc.success == true {
|
||||
t.Errorf("expected success, got %q", err)
|
||||
continue
|
||||
} else if err == nil && tc.success == false {
|
||||
t.Errorf("expected failure, got success")
|
||||
continue
|
||||
} else if tc.success {
|
||||
c, err := f.GetCount("verbose")
|
||||
if err != nil {
|
||||
t.Errorf("Got error trying to fetch the counter flag")
|
||||
}
|
||||
if c != tc.expected {
|
||||
t.Errorf("expected %q, got %q", tc.expected, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package pflag
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// -- time.Duration Value
|
||||
type durationValue time.Duration
|
||||
|
@ -22,6 +24,19 @@ func (d *durationValue) Type() string {
|
|||
|
||||
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
|
||||
|
||||
func durationConv(sval string) (interface{}, error) {
|
||||
return time.ParseDuration(sval)
|
||||
}
|
||||
|
||||
// GetDuration return the duration value of a flag with the given name
|
||||
func (f *FlagSet) GetDuration(name string) (time.Duration, error) {
|
||||
val, err := f.getFlagType(name, "duration", durationConv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(time.Duration), nil
|
||||
}
|
||||
|
||||
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
||||
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
||||
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
||||
|
|
|
@ -152,6 +152,7 @@ type Flag struct {
|
|||
Value Value // value as set
|
||||
DefValue string // default value (as text); for usage message
|
||||
Changed bool // If the user set the value (or if left to default)
|
||||
NoOptDefVal string //default value (as text); if the flag is on the command line without any options
|
||||
Deprecated string // If this flag is deprecated, this string is the new or now thing to use
|
||||
Annotations map[string][]string // used by cobra.Command bash autocomple code
|
||||
}
|
||||
|
@ -257,6 +258,27 @@ func (f *FlagSet) lookup(name NormalizedName) *Flag {
|
|||
return f.formal[name]
|
||||
}
|
||||
|
||||
// func to return a given type for a given flag name
|
||||
func (f *FlagSet) getFlagType(name string, ftype string, convFunc func(sval string) (interface{}, error)) (interface{}, error) {
|
||||
flag := f.Lookup(name)
|
||||
if flag == nil {
|
||||
err := fmt.Errorf("flag accessed but not defined: %s", name)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if flag.Value.Type() != ftype {
|
||||
err := fmt.Errorf("trying to get %s value of flag of type %s", ftype, flag.Value.Type())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sval := flag.Value.String()
|
||||
result, err := convFunc(sval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Mark a flag deprecated in your program
|
||||
func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error {
|
||||
flag := f.Lookup(name)
|
||||
|
@ -295,6 +317,19 @@ func (f *FlagSet) Set(name, value string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (f *FlagSet) SetAnnotation(name, key string, values []string) error {
|
||||
normalName := f.normalizeFlagName(name)
|
||||
flag, ok := f.formal[normalName]
|
||||
if !ok {
|
||||
return fmt.Errorf("no such flag -%v", name)
|
||||
}
|
||||
if flag.Annotations == nil {
|
||||
flag.Annotations = map[string][]string{}
|
||||
}
|
||||
flag.Annotations[key] = values
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set sets the value of the named command-line flag.
|
||||
func Set(name, value string) error {
|
||||
return CommandLine.Set(name, value)
|
||||
|
@ -307,16 +342,25 @@ func (f *FlagSet) PrintDefaults() {
|
|||
if len(flag.Deprecated) > 0 {
|
||||
return
|
||||
}
|
||||
format := "--%s=%s: %s\n"
|
||||
if _, ok := flag.Value.(*stringValue); ok {
|
||||
// put quotes on the value
|
||||
format = "--%s=%q: %s\n"
|
||||
}
|
||||
format := ""
|
||||
// ex: w/ option string argument '-%s, --%s[=%q]: %s\n'
|
||||
if len(flag.Shorthand) > 0 {
|
||||
format = " -%s, " + format
|
||||
format = " -%s, --%s"
|
||||
} else {
|
||||
format = " %s " + format
|
||||
format = " %s --%s"
|
||||
}
|
||||
if len(flag.NoOptDefVal) > 0 {
|
||||
format = format + "["
|
||||
}
|
||||
if _, ok := flag.Value.(*stringValue); ok {
|
||||
format = format + "=%q"
|
||||
} else {
|
||||
format = format + "=%s"
|
||||
}
|
||||
if len(flag.NoOptDefVal) > 0 {
|
||||
format = format + "]"
|
||||
}
|
||||
format = format + ": %s\n"
|
||||
fmt.Fprintf(f.out(), format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
|
||||
})
|
||||
}
|
||||
|
@ -409,8 +453,8 @@ func (f *FlagSet) Var(value Value, name string, usage string) {
|
|||
f.VarP(value, name, "", usage)
|
||||
}
|
||||
|
||||
// Like Var, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
|
||||
// Like VarP, but returns the flag created
|
||||
func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
|
||||
// Remember the default value as a string; it won't change.
|
||||
flag := &Flag{
|
||||
Name: name,
|
||||
|
@ -420,6 +464,12 @@ func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
|
|||
DefValue: value.String(),
|
||||
}
|
||||
f.AddFlag(flag)
|
||||
return flag
|
||||
}
|
||||
|
||||
// Like Var, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
|
||||
_ = f.VarPF(value, name, shorthand, usage)
|
||||
}
|
||||
|
||||
func (f *FlagSet) AddFlag(flag *Flag) {
|
||||
|
@ -529,14 +579,20 @@ func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error)
|
|||
return
|
||||
}
|
||||
var value string
|
||||
if len(split) == 1 {
|
||||
if bv, ok := flag.Value.(boolFlag); !ok || !bv.IsBoolFlag() {
|
||||
err = f.failf("flag needs an argument: %s", s)
|
||||
return
|
||||
}
|
||||
value = "true"
|
||||
} else {
|
||||
if len(split) == 2 {
|
||||
// '--flag=arg'
|
||||
value = split[1]
|
||||
} else if len(flag.NoOptDefVal) > 0 {
|
||||
// '--flag' (arg was optional)
|
||||
value = flag.NoOptDefVal
|
||||
} else if len(a) > 0 {
|
||||
// '--flag arg'
|
||||
value = a[0]
|
||||
a = a[1:]
|
||||
} else {
|
||||
// '--flag' (arg was required)
|
||||
err = f.failf("flag needs an argument: %s", s)
|
||||
return
|
||||
}
|
||||
err = f.setFlag(flag, value, s)
|
||||
return
|
||||
|
@ -562,8 +618,8 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string) (outShor
|
|||
if len(shorthands) > 2 && shorthands[1] == '=' {
|
||||
value = shorthands[2:]
|
||||
outShorts = ""
|
||||
} else if bv, ok := flag.Value.(boolFlag); ok && bv.IsBoolFlag() {
|
||||
value = "true"
|
||||
} else if len(flag.NoOptDefVal) > 0 {
|
||||
value = flag.NoOptDefVal
|
||||
} else if len(shorthands) > 1 {
|
||||
value = shorthands[1:]
|
||||
outShorts = ""
|
||||
|
|
|
@ -9,7 +9,9 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -25,6 +27,7 @@ var (
|
|||
test_string = String("test_string", "0", "string value")
|
||||
test_float64 = Float64("test_float64", 0, "float64 value")
|
||||
test_duration = Duration("test_duration", 0, "time.Duration value")
|
||||
test_optional_int = Int("test_optional_int", 0, "optional int value")
|
||||
normalizeFlagNameInvocations = 0
|
||||
)
|
||||
|
||||
|
@ -56,7 +59,7 @@ func TestEverything(t *testing.T) {
|
|||
}
|
||||
}
|
||||
VisitAll(visitor)
|
||||
if len(m) != 8 {
|
||||
if len(m) != 9 {
|
||||
t.Error("VisitAll misses some flags")
|
||||
for k, v := range m {
|
||||
t.Log(k, *v)
|
||||
|
@ -79,9 +82,10 @@ func TestEverything(t *testing.T) {
|
|||
Set("test_string", "1")
|
||||
Set("test_float64", "1")
|
||||
Set("test_duration", "1s")
|
||||
Set("test_optional_int", "1")
|
||||
desired = "1"
|
||||
Visit(visitor)
|
||||
if len(m) != 8 {
|
||||
if len(m) != 9 {
|
||||
t.Error("Visit fails after set")
|
||||
for k, v := range m {
|
||||
t.Log(k, *v)
|
||||
|
@ -106,6 +110,37 @@ func TestUsage(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAnnotation(t *testing.T) {
|
||||
f := NewFlagSet("shorthand", ContinueOnError)
|
||||
|
||||
if err := f.SetAnnotation("missing-flag", "key", nil); err == nil {
|
||||
t.Errorf("Expected error setting annotation on non-existent flag")
|
||||
}
|
||||
|
||||
f.StringP("stringa", "a", "", "string value")
|
||||
if err := f.SetAnnotation("stringa", "key", nil); err != nil {
|
||||
t.Errorf("Unexpected error setting new nil annotation: %v", err)
|
||||
}
|
||||
if annotation := f.Lookup("stringa").Annotations["key"]; annotation != nil {
|
||||
t.Errorf("Unexpected annotation: %v", annotation)
|
||||
}
|
||||
|
||||
f.StringP("stringb", "b", "", "string2 value")
|
||||
if err := f.SetAnnotation("stringb", "key", []string{"value1"}); err != nil {
|
||||
t.Errorf("Unexpected error setting new annotation: %v", err)
|
||||
}
|
||||
if annotation := f.Lookup("stringb").Annotations["key"]; !reflect.DeepEqual(annotation, []string{"value1"}) {
|
||||
t.Errorf("Unexpected annotation: %v", annotation)
|
||||
}
|
||||
|
||||
if err := f.SetAnnotation("stringb", "key", []string{"value2"}); err != nil {
|
||||
t.Errorf("Unexpected error updating annotation: %v", err)
|
||||
}
|
||||
if annotation := f.Lookup("stringb").Annotations["key"]; !reflect.DeepEqual(annotation, []string{"value2"}) {
|
||||
t.Errorf("Unexpected annotation: %v", annotation)
|
||||
}
|
||||
}
|
||||
|
||||
func testParse(f *FlagSet, t *testing.T) {
|
||||
if f.Parsed() {
|
||||
t.Error("f.Parse() = true before Parse")
|
||||
|
@ -114,24 +149,46 @@ func testParse(f *FlagSet, t *testing.T) {
|
|||
bool2Flag := f.Bool("bool2", false, "bool2 value")
|
||||
bool3Flag := f.Bool("bool3", false, "bool3 value")
|
||||
intFlag := f.Int("int", 0, "int value")
|
||||
int8Flag := f.Int8("int8", 0, "int value")
|
||||
int32Flag := f.Int32("int32", 0, "int value")
|
||||
int64Flag := f.Int64("int64", 0, "int64 value")
|
||||
uintFlag := f.Uint("uint", 0, "uint value")
|
||||
uint8Flag := f.Uint8("uint8", 0, "uint value")
|
||||
uint16Flag := f.Uint16("uint16", 0, "uint value")
|
||||
uint32Flag := f.Uint32("uint32", 0, "uint value")
|
||||
uint64Flag := f.Uint64("uint64", 0, "uint64 value")
|
||||
stringFlag := f.String("string", "0", "string value")
|
||||
float32Flag := f.Float32("float32", 0, "float32 value")
|
||||
float64Flag := f.Float64("float64", 0, "float64 value")
|
||||
ipFlag := f.IP("ip", net.ParseIP("127.0.0.1"), "ip value")
|
||||
maskFlag := f.IPMask("mask", ParseIPv4Mask("0.0.0.0"), "mask value")
|
||||
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
|
||||
optionalIntNoValueFlag := f.Int("optional-int-no-value", 0, "int value")
|
||||
f.Lookup("optional-int-no-value").NoOptDefVal = "9"
|
||||
optionalIntWithValueFlag := f.Int("optional-int-with-value", 0, "int value")
|
||||
f.Lookup("optional-int-no-value").NoOptDefVal = "9"
|
||||
extra := "one-extra-argument"
|
||||
args := []string{
|
||||
"--bool",
|
||||
"--bool2=true",
|
||||
"--bool3=false",
|
||||
"--int=22",
|
||||
"--int8=-8",
|
||||
"--int32=-32",
|
||||
"--int64=0x23",
|
||||
"--uint=24",
|
||||
"--uint", "24",
|
||||
"--uint8=8",
|
||||
"--uint16=16",
|
||||
"--uint32=32",
|
||||
"--uint64=25",
|
||||
"--string=hello",
|
||||
"--float32=-172e12",
|
||||
"--float64=2718e28",
|
||||
"--ip=10.11.12.13",
|
||||
"--mask=255.255.255.0",
|
||||
"--duration=2m",
|
||||
"--optional-int-no-value",
|
||||
"--optional-int-with-value=42",
|
||||
extra,
|
||||
}
|
||||
if err := f.Parse(args); err != nil {
|
||||
|
@ -143,6 +200,9 @@ func testParse(f *FlagSet, t *testing.T) {
|
|||
if *boolFlag != true {
|
||||
t.Error("bool flag should be true, is ", *boolFlag)
|
||||
}
|
||||
if v, err := f.GetBool("bool"); err != nil || v != *boolFlag {
|
||||
t.Error("GetBool does not work.")
|
||||
}
|
||||
if *bool2Flag != true {
|
||||
t.Error("bool2 flag should be true, is ", *bool2Flag)
|
||||
}
|
||||
|
@ -152,24 +212,102 @@ func testParse(f *FlagSet, t *testing.T) {
|
|||
if *intFlag != 22 {
|
||||
t.Error("int flag should be 22, is ", *intFlag)
|
||||
}
|
||||
if v, err := f.GetInt("int"); err != nil || v != *intFlag {
|
||||
t.Error("GetInt does not work.")
|
||||
}
|
||||
if *int8Flag != -8 {
|
||||
t.Error("int8 flag should be 0x23, is ", *int8Flag)
|
||||
}
|
||||
if v, err := f.GetInt8("int8"); err != nil || v != *int8Flag {
|
||||
t.Error("GetInt8 does not work.")
|
||||
}
|
||||
if *int32Flag != -32 {
|
||||
t.Error("int32 flag should be 0x23, is ", *int32Flag)
|
||||
}
|
||||
if v, err := f.GetInt32("int32"); err != nil || v != *int32Flag {
|
||||
t.Error("GetInt32 does not work.")
|
||||
}
|
||||
if *int64Flag != 0x23 {
|
||||
t.Error("int64 flag should be 0x23, is ", *int64Flag)
|
||||
}
|
||||
if v, err := f.GetInt64("int64"); err != nil || v != *int64Flag {
|
||||
t.Error("GetInt64 does not work.")
|
||||
}
|
||||
if *uintFlag != 24 {
|
||||
t.Error("uint flag should be 24, is ", *uintFlag)
|
||||
}
|
||||
if v, err := f.GetUint("uint"); err != nil || v != *uintFlag {
|
||||
t.Error("GetUint does not work.")
|
||||
}
|
||||
if *uint8Flag != 8 {
|
||||
t.Error("uint8 flag should be 8, is ", *uint8Flag)
|
||||
}
|
||||
if v, err := f.GetUint8("uint8"); err != nil || v != *uint8Flag {
|
||||
t.Error("GetUint8 does not work.")
|
||||
}
|
||||
if *uint16Flag != 16 {
|
||||
t.Error("uint16 flag should be 16, is ", *uint16Flag)
|
||||
}
|
||||
if v, err := f.GetUint16("uint16"); err != nil || v != *uint16Flag {
|
||||
t.Error("GetUint16 does not work.")
|
||||
}
|
||||
if *uint32Flag != 32 {
|
||||
t.Error("uint32 flag should be 32, is ", *uint32Flag)
|
||||
}
|
||||
if v, err := f.GetUint32("uint32"); err != nil || v != *uint32Flag {
|
||||
t.Error("GetUint32 does not work.")
|
||||
}
|
||||
if *uint64Flag != 25 {
|
||||
t.Error("uint64 flag should be 25, is ", *uint64Flag)
|
||||
}
|
||||
if v, err := f.GetUint64("uint64"); err != nil || v != *uint64Flag {
|
||||
t.Error("GetUint64 does not work.")
|
||||
}
|
||||
if *stringFlag != "hello" {
|
||||
t.Error("string flag should be `hello`, is ", *stringFlag)
|
||||
}
|
||||
if v, err := f.GetString("string"); err != nil || v != *stringFlag {
|
||||
t.Error("GetString does not work.")
|
||||
}
|
||||
if *float32Flag != -172e12 {
|
||||
t.Error("float32 flag should be -172e12, is ", *float32Flag)
|
||||
}
|
||||
if v, err := f.GetFloat32("float32"); err != nil || v != *float32Flag {
|
||||
t.Errorf("GetFloat32 returned %v but float32Flag was %v", v, *float32Flag)
|
||||
}
|
||||
if *float64Flag != 2718e28 {
|
||||
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
|
||||
}
|
||||
if v, err := f.GetFloat64("float64"); err != nil || v != *float64Flag {
|
||||
t.Errorf("GetFloat64 returned %v but float64Flag was %v", v, *float64Flag)
|
||||
}
|
||||
if !(*ipFlag).Equal(net.ParseIP("10.11.12.13")) {
|
||||
t.Error("ip flag should be 10.11.12.13, is ", *ipFlag)
|
||||
}
|
||||
if v, err := f.GetIP("ip"); err != nil || !v.Equal(*ipFlag) {
|
||||
t.Errorf("GetIP returned %v but ipFlag was %v", v, *ipFlag)
|
||||
}
|
||||
if (*maskFlag).String() != ParseIPv4Mask("255.255.255.0").String() {
|
||||
t.Error("mask flag should be 255.255.255.0, is ", (*maskFlag).String())
|
||||
}
|
||||
if v, err := f.GetIPv4Mask("mask"); err != nil || v.String() != (*maskFlag).String() {
|
||||
t.Errorf("GetIP returned %v but maskFlag was %v", v, *maskFlag, err)
|
||||
}
|
||||
if *durationFlag != 2*time.Minute {
|
||||
t.Error("duration flag should be 2m, is ", *durationFlag)
|
||||
}
|
||||
if v, err := f.GetDuration("duration"); err != nil || v != *durationFlag {
|
||||
t.Error("GetDuration does not work.")
|
||||
}
|
||||
if _, err := f.GetInt("duration"); err == nil {
|
||||
t.Error("GetInt parsed a time.Duration?!?!")
|
||||
}
|
||||
if *optionalIntNoValueFlag != 9 {
|
||||
t.Error("optional int flag should be the default value, is ", *optionalIntNoValueFlag)
|
||||
}
|
||||
if *optionalIntWithValueFlag != 42 {
|
||||
t.Error("optional int flag should be 42, is ", *optionalIntWithValueFlag)
|
||||
}
|
||||
if len(f.Args()) != 1 {
|
||||
t.Error("expected one argument, got", len(f.Args()))
|
||||
} else if f.Args()[0] != extra {
|
||||
|
|
|
@ -25,6 +25,23 @@ func (f *float32Value) Type() string {
|
|||
|
||||
func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) }
|
||||
|
||||
func float32Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseFloat(sval, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return float32(v), nil
|
||||
}
|
||||
|
||||
// GetFloat32 return the float32 value of a flag with the given name
|
||||
func (f *FlagSet) GetFloat32(name string) (float32, error) {
|
||||
val, err := f.getFlagType(name, "float32", float32Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(float32), nil
|
||||
}
|
||||
|
||||
// Float32Var defines a float32 flag with specified name, default value, and usage string.
|
||||
// The argument p points to a float32 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) {
|
||||
|
|
|
@ -25,6 +25,19 @@ func (f *float64Value) Type() string {
|
|||
|
||||
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
|
||||
|
||||
func float64Conv(sval string) (interface{}, error) {
|
||||
return strconv.ParseFloat(sval, 64)
|
||||
}
|
||||
|
||||
// GetFloat64 return the float64 value of a flag with the given name
|
||||
func (f *FlagSet) GetFloat64(name string) (float64, error) {
|
||||
val, err := f.getFlagType(name, "float64", float64Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(float64), nil
|
||||
}
|
||||
|
||||
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
||||
// The argument p points to a float64 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
|
||||
|
|
|
@ -25,6 +25,19 @@ func (i *intValue) Type() string {
|
|||
|
||||
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func intConv(sval string) (interface{}, error) {
|
||||
return strconv.Atoi(sval)
|
||||
}
|
||||
|
||||
// GetInt return the int value of a flag with the given name
|
||||
func (f *FlagSet) GetInt(name string) (int, error) {
|
||||
val, err := f.getFlagType(name, "int", intConv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(int), nil
|
||||
}
|
||||
|
||||
// IntVar defines an int flag with specified name, default value, and usage string.
|
||||
// The argument p points to an int variable in which to store the value of the flag.
|
||||
func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
|
||||
|
|
|
@ -25,6 +25,23 @@ func (i *int32Value) Type() string {
|
|||
|
||||
func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func int32Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseInt(sval, 0, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(v), nil
|
||||
}
|
||||
|
||||
// GetInt32 return the int32 value of a flag with the given name
|
||||
func (f *FlagSet) GetInt32(name string) (int32, error) {
|
||||
val, err := f.getFlagType(name, "int32", int32Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(int32), nil
|
||||
}
|
||||
|
||||
// Int32Var defines an int32 flag with specified name, default value, and usage string.
|
||||
// The argument p points to an int32 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) {
|
||||
|
|
|
@ -25,6 +25,19 @@ func (i *int64Value) Type() string {
|
|||
|
||||
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func int64Conv(sval string) (interface{}, error) {
|
||||
return strconv.ParseInt(sval, 0, 64)
|
||||
}
|
||||
|
||||
// GetInt64 return the int64 value of a flag with the given name
|
||||
func (f *FlagSet) GetInt64(name string) (int64, error) {
|
||||
val, err := f.getFlagType(name, "int64", int64Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(int64), nil
|
||||
}
|
||||
|
||||
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
||||
// The argument p points to an int64 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
|
||||
|
|
|
@ -25,6 +25,23 @@ func (i *int8Value) Type() string {
|
|||
|
||||
func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func int8Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseInt(sval, 0, 8)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int8(v), nil
|
||||
}
|
||||
|
||||
// GetInt8 return the int8 value of a flag with the given name
|
||||
func (f *FlagSet) GetInt8(name string) (int8, error) {
|
||||
val, err := f.getFlagType(name, "int8", int8Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(int8), nil
|
||||
}
|
||||
|
||||
// Int8Var defines an int8 flag with specified name, default value, and usage string.
|
||||
// The argument p points to an int8 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) {
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// -- intSlice Value
|
||||
type intSliceValue []int
|
||||
|
||||
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
|
||||
*p = val
|
||||
return (*intSliceValue)(p)
|
||||
}
|
||||
|
||||
func (s *intSliceValue) Set(val string) error {
|
||||
ss := strings.Split(val, ",")
|
||||
out := make([]int, len(ss))
|
||||
for i, d := range ss {
|
||||
var err error
|
||||
out[i], err = strconv.Atoi(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
*s = append(*s, out...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *intSliceValue) Type() string {
|
||||
return "intSlice"
|
||||
}
|
||||
|
||||
func (s *intSliceValue) String() string {
|
||||
out := make([]string, len(*s))
|
||||
for i, d := range *s {
|
||||
out[i] = fmt.Sprintf("%d", d)
|
||||
}
|
||||
return "[" + strings.Join(out, ",") + "]"
|
||||
}
|
||||
|
||||
func intSliceConv(val string) (interface{}, error) {
|
||||
val = strings.Trim(val, "[]")
|
||||
// Empty string would cause a slice with one (empty) entry
|
||||
if len(val) == 0 {
|
||||
return []int{}, nil
|
||||
}
|
||||
ss := strings.Split(val, ",")
|
||||
out := make([]int, len(ss))
|
||||
for i, d := range ss {
|
||||
var err error
|
||||
out[i], err = strconv.Atoi(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetIntSlice return the []int value of a flag with the given name
|
||||
func (f *FlagSet) GetIntSlice(name string) ([]int, error) {
|
||||
val, err := f.getFlagType(name, "intSlice", intSliceConv)
|
||||
if err != nil {
|
||||
return []int{}, err
|
||||
}
|
||||
return val.([]int), nil
|
||||
}
|
||||
|
||||
// IntSliceVar defines a intSlice flag with specified name, default value, and usage string.
|
||||
// The argument p points to a []int variable in which to store the value of the flag.
|
||||
func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) {
|
||||
f.VarP(newIntSliceValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
|
||||
f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// IntSliceVar defines a int[] flag with specified name, default value, and usage string.
|
||||
// The argument p points to a int[] variable in which to store the value of the flag.
|
||||
func IntSliceVar(p *[]int, name string, value []int, usage string) {
|
||||
CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
|
||||
CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// IntSlice defines a []int flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a []int variable that stores the value of the flag.
|
||||
func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
|
||||
p := make([]int, 0)
|
||||
f.IntSliceVarP(&p, name, "", value, usage)
|
||||
return &p
|
||||
}
|
||||
|
||||
// Like IntSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
|
||||
p := make([]int, 0)
|
||||
f.IntSliceVarP(&p, name, shorthand, value, usage)
|
||||
return &p
|
||||
}
|
||||
|
||||
// IntSlice defines a []int flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a []int variable that stores the value of the flag.
|
||||
func IntSlice(name string, value []int, usage string) *[]int {
|
||||
return CommandLine.IntSliceP(name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like IntSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||
func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
|
||||
return CommandLine.IntSliceP(name, shorthand, value, usage)
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setUpISFlagSet(isp *[]int) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
f.IntSliceVar(isp, "is", []int{}, "Command seperated list!")
|
||||
return f
|
||||
}
|
||||
|
||||
func TestEmptyIS(t *testing.T) {
|
||||
var is []int
|
||||
f := setUpISFlagSet(&is)
|
||||
err := f.Parse([]string{})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
|
||||
getIS, err := f.GetIntSlice("is")
|
||||
if err != nil {
|
||||
t.Fatal("got an error from GetStringSlice():", err)
|
||||
}
|
||||
if len(getIS) != 0 {
|
||||
t.Fatalf("got is %v with len=%d but expected length=0", getIS, len(getIS))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIS(t *testing.T) {
|
||||
var is []int
|
||||
f := setUpISFlagSet(&is)
|
||||
|
||||
vals := []string{"1", "2", "4", "3"}
|
||||
arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
|
||||
err := f.Parse([]string{arg})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
for i, v := range is {
|
||||
d, err := strconv.Atoi(vals[i])
|
||||
if err != nil {
|
||||
t.Fatalf("got error: %v", err)
|
||||
}
|
||||
if d != v {
|
||||
t.Fatalf("expected is[%d] to be %s but got: %d", i, vals[i], v)
|
||||
}
|
||||
}
|
||||
getIS, err := f.GetIntSlice("is")
|
||||
for i, v := range getIS {
|
||||
d, err := strconv.Atoi(vals[i])
|
||||
if err != nil {
|
||||
t.Fatalf("got error: %v", err)
|
||||
}
|
||||
if d != v {
|
||||
t.Fatalf("expected is[%d] to be %s but got: %d from GetIntSlice", i, vals[i], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestISCalledTwice(t *testing.T) {
|
||||
var is []int
|
||||
f := setUpISFlagSet(&is)
|
||||
|
||||
in := []string{"1,2", "3"}
|
||||
expected := []int{1, 2, 3}
|
||||
argfmt := "--is=%s"
|
||||
arg1 := fmt.Sprintf(argfmt, in[0])
|
||||
arg2 := fmt.Sprintf(argfmt, in[1])
|
||||
err := f.Parse([]string{arg1, arg2})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
for i, v := range is {
|
||||
if expected[i] != v {
|
||||
t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,8 +3,11 @@ package pflag
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ = strings.TrimSpace
|
||||
|
||||
// -- net.IP value
|
||||
type ipValue net.IP
|
||||
|
||||
|
@ -15,21 +18,35 @@ func newIPValue(val net.IP, p *net.IP) *ipValue {
|
|||
|
||||
func (i *ipValue) String() string { return net.IP(*i).String() }
|
||||
func (i *ipValue) Set(s string) error {
|
||||
ip := net.ParseIP(s)
|
||||
ip := net.ParseIP(strings.TrimSpace(s))
|
||||
if ip == nil {
|
||||
return fmt.Errorf("failed to parse IP: %q", s)
|
||||
}
|
||||
*i = ipValue(ip)
|
||||
return nil
|
||||
}
|
||||
func (i *ipValue) Get() interface{} {
|
||||
return net.IP(*i)
|
||||
}
|
||||
|
||||
func (i *ipValue) Type() string {
|
||||
return "ip"
|
||||
}
|
||||
|
||||
func ipConv(sval string) (interface{}, error) {
|
||||
ip := net.ParseIP(sval)
|
||||
if ip != nil {
|
||||
return ip, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
|
||||
}
|
||||
|
||||
// GetIP return the net.IP value of a flag with the given name
|
||||
func (f *FlagSet) GetIP(name string) (net.IP, error) {
|
||||
val, err := f.getFlagType(name, "ip", ipConv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return val.(net.IP), nil
|
||||
}
|
||||
|
||||
// IPVar defines an net.IP flag with specified name, default value, and usage string.
|
||||
// The argument p points to an net.IP variable in which to store the value of the flag.
|
||||
func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) {
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setUpIP(ip *net.IP) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
f.IPVar(ip, "address", net.ParseIP("0.0.0.0"), "IP Address")
|
||||
return f
|
||||
}
|
||||
|
||||
func TestIP(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
success bool
|
||||
expected string
|
||||
}{
|
||||
{"0.0.0.0", true, "0.0.0.0"},
|
||||
{" 0.0.0.0 ", true, "0.0.0.0"},
|
||||
{"1.2.3.4", true, "1.2.3.4"},
|
||||
{"127.0.0.1", true, "127.0.0.1"},
|
||||
{"255.255.255.255", true, "255.255.255.255"},
|
||||
{"", false, ""},
|
||||
{"0", false, ""},
|
||||
{"localhost", false, ""},
|
||||
{"0.0.0", false, ""},
|
||||
{"0.0.0.", false, ""},
|
||||
{"0.0.0.0.", false, ""},
|
||||
{"0.0.0.256", false, ""},
|
||||
{"0 . 0 . 0 . 0", false, ""},
|
||||
}
|
||||
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
os.Stderr = devnull
|
||||
for i := range testCases {
|
||||
var addr net.IP
|
||||
f := setUpIP(&addr)
|
||||
|
||||
tc := &testCases[i]
|
||||
|
||||
arg := fmt.Sprintf("--address=%s", tc.input)
|
||||
err := f.Parse([]string{arg})
|
||||
if err != nil && tc.success == true {
|
||||
t.Errorf("expected success, got %q", err)
|
||||
continue
|
||||
} else if err == nil && tc.success == false {
|
||||
t.Errorf("expected failure")
|
||||
continue
|
||||
} else if tc.success {
|
||||
ip, err := f.GetIP("address")
|
||||
if err != nil {
|
||||
t.Errorf("Got error trying to fetch the IP flag: %v", err)
|
||||
}
|
||||
if ip.String() != tc.expected {
|
||||
t.Errorf("expected %q, got %q", tc.expected, ip.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package pflag
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// -- net.IPMask value
|
||||
|
@ -22,9 +23,6 @@ func (i *ipMaskValue) Set(s string) error {
|
|||
*i = ipMaskValue(ip)
|
||||
return nil
|
||||
}
|
||||
func (i *ipMaskValue) Get() interface{} {
|
||||
return net.IPMask(*i)
|
||||
}
|
||||
|
||||
func (i *ipMaskValue) Type() string {
|
||||
return "ipMask"
|
||||
|
@ -35,11 +33,46 @@ func (i *ipMaskValue) Type() string {
|
|||
func ParseIPv4Mask(s string) net.IPMask {
|
||||
mask := net.ParseIP(s)
|
||||
if mask == nil {
|
||||
return nil
|
||||
if len(s) != 8 {
|
||||
return nil
|
||||
}
|
||||
// net.IPMask.String() actually outputs things like ffffff00
|
||||
// so write a horrible parser for that as well :-(
|
||||
m := []int{}
|
||||
for i := 0; i < 4; i++ {
|
||||
b := "0x" + s[2*i:2*i+2]
|
||||
d, err := strconv.ParseInt(b, 0, 0)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
m = append(m, int(d))
|
||||
}
|
||||
s := fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
|
||||
mask = net.ParseIP(s)
|
||||
if mask == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
|
||||
}
|
||||
|
||||
func parseIPv4Mask(sval string) (interface{}, error) {
|
||||
mask := ParseIPv4Mask(sval)
|
||||
if mask == nil {
|
||||
return nil, fmt.Errorf("unable to parse %s as net.IPMask", sval)
|
||||
}
|
||||
return mask, nil
|
||||
}
|
||||
|
||||
// GetIPv4Mask return the net.IPv4Mask value of a flag with the given name
|
||||
func (f *FlagSet) GetIPv4Mask(name string) (net.IPMask, error) {
|
||||
val, err := f.getFlagType(name, "ipMask", parseIPv4Mask)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return val.(net.IPMask), nil
|
||||
}
|
||||
|
||||
// IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string.
|
||||
// The argument p points to an net.IPMask variable in which to store the value of the flag.
|
||||
func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) {
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IPNet adapts net.IPNet for use as a flag.
|
||||
type IPNetValue net.IPNet
|
||||
|
||||
func (ipnet IPNetValue) String() string {
|
||||
n := net.IPNet(ipnet)
|
||||
return n.String()
|
||||
}
|
||||
|
||||
func (ipnet *IPNetValue) Set(value string) error {
|
||||
_, n, err := net.ParseCIDR(strings.TrimSpace(value))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*ipnet = IPNetValue(*n)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*IPNetValue) Type() string {
|
||||
return "ipNet"
|
||||
}
|
||||
|
||||
var _ = strings.TrimSpace
|
||||
|
||||
func newIPNetValue(val net.IPNet, p *net.IPNet) *IPNetValue {
|
||||
*p = val
|
||||
return (*IPNetValue)(p)
|
||||
}
|
||||
|
||||
func ipNetConv(sval string) (interface{}, error) {
|
||||
_, n, err := net.ParseCIDR(strings.TrimSpace(sval))
|
||||
if err == nil {
|
||||
return *n, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid string being converted to IPNet: %s", sval)
|
||||
}
|
||||
|
||||
// GetIPNet return the net.IPNet value of a flag with the given name
|
||||
func (f *FlagSet) GetIPNet(name string) (net.IPNet, error) {
|
||||
val, err := f.getFlagType(name, "ipNet", ipNetConv)
|
||||
if err != nil {
|
||||
return net.IPNet{}, err
|
||||
}
|
||||
return val.(net.IPNet), nil
|
||||
}
|
||||
|
||||
// IPNetVar defines an net.IPNet flag with specified name, default value, and usage string.
|
||||
// The argument p points to an net.IPNet variable in which to store the value of the flag.
|
||||
func (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
|
||||
f.VarP(newIPNetValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
|
||||
f.VarP(newIPNetValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// IPNetVar defines an net.IPNet flag with specified name, default value, and usage string.
|
||||
// The argument p points to an net.IPNet variable in which to store the value of the flag.
|
||||
func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
|
||||
CommandLine.VarP(newIPNetValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
|
||||
CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// IPNet defines an net.IPNet flag with specified name, default value, and usage string.
|
||||
// The return value is the address of an net.IPNet variable that stores the value of the flag.
|
||||
func (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet {
|
||||
p := new(net.IPNet)
|
||||
f.IPNetVarP(p, name, "", value, usage)
|
||||
return p
|
||||
}
|
||||
|
||||
// Like IPNet, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
|
||||
p := new(net.IPNet)
|
||||
f.IPNetVarP(p, name, shorthand, value, usage)
|
||||
return p
|
||||
}
|
||||
|
||||
// IPNet defines an net.IPNet flag with specified name, default value, and usage string.
|
||||
// The return value is the address of an net.IPNet variable that stores the value of the flag.
|
||||
func IPNet(name string, value net.IPNet, usage string) *net.IPNet {
|
||||
return CommandLine.IPNetP(name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like IPNet, but accepts a shorthand letter that can be used after a single dash.
|
||||
func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
|
||||
return CommandLine.IPNetP(name, shorthand, value, usage)
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setUpIPNet(ip *net.IPNet) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
_, def, _ := net.ParseCIDR("0.0.0.0/0")
|
||||
f.IPNetVar(ip, "address", *def, "IP Address")
|
||||
return f
|
||||
}
|
||||
|
||||
func TestIPNet(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
success bool
|
||||
expected string
|
||||
}{
|
||||
{"0.0.0.0/0", true, "0.0.0.0/0"},
|
||||
{" 0.0.0.0/0 ", true, "0.0.0.0/0"},
|
||||
{"1.2.3.4/8", true, "1.0.0.0/8"},
|
||||
{"127.0.0.1/16", true, "127.0.0.0/16"},
|
||||
{"255.255.255.255/19", true, "255.255.224.0/19"},
|
||||
{"255.255.255.255/32", true, "255.255.255.255/32"},
|
||||
{"", false, ""},
|
||||
{"/0", false, ""},
|
||||
{"0", false, ""},
|
||||
{"0/0", false, ""},
|
||||
{"localhost/0", false, ""},
|
||||
{"0.0.0/4", false, ""},
|
||||
{"0.0.0./8", false, ""},
|
||||
{"0.0.0.0./12", false, ""},
|
||||
{"0.0.0.256/16", false, ""},
|
||||
{"0.0.0.0 /20", false, ""},
|
||||
{"0.0.0.0/ 24", false, ""},
|
||||
{"0 . 0 . 0 . 0 / 28", false, ""},
|
||||
{"0.0.0.0/33", false, ""},
|
||||
}
|
||||
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
os.Stderr = devnull
|
||||
for i := range testCases {
|
||||
var addr net.IPNet
|
||||
f := setUpIPNet(&addr)
|
||||
|
||||
tc := &testCases[i]
|
||||
|
||||
arg := fmt.Sprintf("--address=%s", tc.input)
|
||||
err := f.Parse([]string{arg})
|
||||
if err != nil && tc.success == true {
|
||||
t.Errorf("expected success, got %q", err)
|
||||
continue
|
||||
} else if err == nil && tc.success == false {
|
||||
t.Errorf("expected failure")
|
||||
continue
|
||||
} else if tc.success {
|
||||
ip, err := f.GetIPNet("address")
|
||||
if err != nil {
|
||||
t.Errorf("Got error trying to fetch the IP flag: %v", err)
|
||||
}
|
||||
if ip.String() != tc.expected {
|
||||
t.Errorf("expected %q, got %q", tc.expected, ip.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,6 +20,19 @@ func (s *stringValue) Type() string {
|
|||
|
||||
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
|
||||
|
||||
func stringConv(sval string) (interface{}, error) {
|
||||
return sval, nil
|
||||
}
|
||||
|
||||
// GetString return the string value of a flag with the given name
|
||||
func (f *FlagSet) GetString(name string) (string, error) {
|
||||
val, err := f.getFlagType(name, "string", stringConv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
// StringVar defines a string flag with specified name, default value, and usage string.
|
||||
// The argument p points to a string variable in which to store the value of the flag.
|
||||
func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
package pflag
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// -- stringSlice Value
|
||||
type stringSliceValue []string
|
||||
|
||||
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
|
||||
*p = val
|
||||
return (*stringSliceValue)(p)
|
||||
}
|
||||
|
||||
func (s *stringSliceValue) Set(val string) error {
|
||||
v := strings.Split(val, ",")
|
||||
*s = append(*s, v...)
|
||||
return nil
|
||||
}
|
||||
func (s *stringSliceValue) Type() string {
|
||||
return "stringSlice"
|
||||
}
|
||||
|
||||
func (s *stringSliceValue) String() string { return "[" + strings.Join(*s, ",") + "]" }
|
||||
|
||||
func stringSliceConv(sval string) (interface{}, error) {
|
||||
sval = strings.Trim(sval, "[]")
|
||||
// An empty string would cause a slice with one (empty) string
|
||||
if len(sval) == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
v := strings.Split(sval, ",")
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// GetStringSlice return the []string value of a flag with the given name
|
||||
func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
|
||||
val, err := f.getFlagType(name, "stringSlice", stringSliceConv)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
return val.([]string), nil
|
||||
}
|
||||
|
||||
// StringSliceVar defines a string flag with specified name, default value, and usage string.
|
||||
// The argument p points to a []string variable in which to store the value of the flag.
|
||||
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||
f.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
|
||||
f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// StringSliceVar defines a string flag with specified name, default value, and usage string.
|
||||
// The argument p points to a []string variable in which to store the value of the flag.
|
||||
func StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||
}
|
||||
|
||||
// Like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
|
||||
func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
|
||||
CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
|
||||
}
|
||||
|
||||
// StringSlice defines a string flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a []string variable that stores the value of the flag.
|
||||
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
|
||||
p := make([]string, 0)
|
||||
f.StringSliceVarP(&p, name, "", value, usage)
|
||||
return &p
|
||||
}
|
||||
|
||||
// Like StringSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||
func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
|
||||
p := make([]string, 0)
|
||||
f.StringSliceVarP(&p, name, shorthand, value, usage)
|
||||
return &p
|
||||
}
|
||||
|
||||
// StringSlice defines a string flag with specified name, default value, and usage string.
|
||||
// The return value is the address of a []string variable that stores the value of the flag.
|
||||
func StringSlice(name string, value []string, usage string) *[]string {
|
||||
return CommandLine.StringSliceP(name, "", value, usage)
|
||||
}
|
||||
|
||||
// Like StringSlice, but accepts a shorthand letter that can be used after a single dash.
|
||||
func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
|
||||
return CommandLine.StringSliceP(name, shorthand, value, usage)
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package pflag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setUpSSFlagSet(ssp *[]string) *FlagSet {
|
||||
f := NewFlagSet("test", ContinueOnError)
|
||||
f.StringSliceVar(ssp, "ss", []string{}, "Command seperated list!")
|
||||
return f
|
||||
}
|
||||
|
||||
func TestEmptySS(t *testing.T) {
|
||||
var ss []string
|
||||
f := setUpSSFlagSet(&ss)
|
||||
err := f.Parse([]string{})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
|
||||
getSS, err := f.GetStringSlice("ss")
|
||||
if err != nil {
|
||||
t.Fatal("got an error from GetStringSlice():", err)
|
||||
}
|
||||
if len(getSS) != 0 {
|
||||
t.Fatalf("got ss %v with len=%d but expected length=0", getSS, len(getSS))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSS(t *testing.T) {
|
||||
var ss []string
|
||||
f := setUpSSFlagSet(&ss)
|
||||
|
||||
vals := []string{"one", "two", "4", "3"}
|
||||
arg := fmt.Sprintf("--ss=%s", strings.Join(vals, ","))
|
||||
err := f.Parse([]string{arg})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
for i, v := range ss {
|
||||
if vals[i] != v {
|
||||
t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v)
|
||||
}
|
||||
}
|
||||
|
||||
getSS, err := f.GetStringSlice("ss")
|
||||
if err != nil {
|
||||
t.Fatal("got an error from GetStringSlice():", err)
|
||||
}
|
||||
for i, v := range getSS {
|
||||
if vals[i] != v {
|
||||
t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSCalledTwice(t *testing.T) {
|
||||
var ss []string
|
||||
f := setUpSSFlagSet(&ss)
|
||||
|
||||
in := []string{"one,two", "three"}
|
||||
expected := []string{"one", "two", "three"}
|
||||
argfmt := "--ss=%s"
|
||||
arg1 := fmt.Sprintf(argfmt, in[0])
|
||||
arg2 := fmt.Sprintf(argfmt, in[1])
|
||||
err := f.Parse([]string{arg1, arg2})
|
||||
if err != nil {
|
||||
t.Fatal("expected no error; got", err)
|
||||
}
|
||||
for i, v := range ss {
|
||||
if expected[i] != v {
|
||||
t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,6 +25,23 @@ func (i *uintValue) Type() string {
|
|||
|
||||
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func uintConv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseUint(sval, 0, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint(v), nil
|
||||
}
|
||||
|
||||
// GetUint return the uint value of a flag with the given name
|
||||
func (f *FlagSet) GetUint(name string) (uint, error) {
|
||||
val, err := f.getFlagType(name, "uint", uintConv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(uint), nil
|
||||
}
|
||||
|
||||
// UintVar defines a uint flag with specified name, default value, and usage string.
|
||||
// The argument p points to a uint variable in which to store the value of the flag.
|
||||
func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
|
||||
|
|
|
@ -19,14 +19,27 @@ func (i *uint16Value) Set(s string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (i *uint16Value) Get() interface{} {
|
||||
return uint16(*i)
|
||||
}
|
||||
|
||||
func (i *uint16Value) Type() string {
|
||||
return "uint16"
|
||||
}
|
||||
|
||||
func uint16Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseUint(sval, 0, 16)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint16(v), nil
|
||||
}
|
||||
|
||||
// GetUint16 return the uint16 value of a flag with the given name
|
||||
func (f *FlagSet) GetUint16(name string) (uint16, error) {
|
||||
val, err := f.getFlagType(name, "uint16", uint16Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(uint16), nil
|
||||
}
|
||||
|
||||
// Uint16Var defines a uint flag with specified name, default value, and usage string.
|
||||
// The argument p points to a uint variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string) {
|
||||
|
|
|
@ -18,14 +18,28 @@ func (i *uint32Value) Set(s string) error {
|
|||
*i = uint32Value(v)
|
||||
return err
|
||||
}
|
||||
func (i *uint32Value) Get() interface{} {
|
||||
return uint32(*i)
|
||||
}
|
||||
|
||||
func (i *uint32Value) Type() string {
|
||||
return "uint32"
|
||||
}
|
||||
|
||||
func uint32Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseUint(sval, 0, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint32(v), nil
|
||||
}
|
||||
|
||||
// GetUint32 return the uint32 value of a flag with the given name
|
||||
func (f *FlagSet) GetUint32(name string) (uint32, error) {
|
||||
val, err := f.getFlagType(name, "uint32", uint32Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(uint32), nil
|
||||
}
|
||||
|
||||
// Uint32Var defines a uint32 flag with specified name, default value, and usage string.
|
||||
// The argument p points to a uint32 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string) {
|
||||
|
|
|
@ -25,6 +25,23 @@ func (i *uint64Value) Type() string {
|
|||
|
||||
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func uint64Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseUint(sval, 0, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint64(v), nil
|
||||
}
|
||||
|
||||
// GetUint64 return the uint64 value of a flag with the given name
|
||||
func (f *FlagSet) GetUint64(name string) (uint64, error) {
|
||||
val, err := f.getFlagType(name, "uint64", uint64Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(uint64), nil
|
||||
}
|
||||
|
||||
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
||||
// The argument p points to a uint64 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
|
||||
|
|
|
@ -25,6 +25,23 @@ func (i *uint8Value) Type() string {
|
|||
|
||||
func (i *uint8Value) String() string { return fmt.Sprintf("%v", *i) }
|
||||
|
||||
func uint8Conv(sval string) (interface{}, error) {
|
||||
v, err := strconv.ParseUint(sval, 0, 8)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint8(v), nil
|
||||
}
|
||||
|
||||
// GetUint8 return the uint8 value of a flag with the given name
|
||||
func (f *FlagSet) GetUint8(name string) (uint8, error) {
|
||||
val, err := f.getFlagType(name, "uint8", uint8Conv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return val.(uint8), nil
|
||||
}
|
||||
|
||||
// Uint8Var defines a uint8 flag with specified name, default value, and usage string.
|
||||
// The argument p points to a uint8 variable in which to store the value of the flag.
|
||||
func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string) {
|
||||
|
|
Loading…
Reference in New Issue