diff options
| author | Alec Thomas <[email protected]> | 2012-12-19 20:36:31 -1000 |
|---|---|---|
| committer | Alec Thomas <[email protected]> | 2012-12-19 20:36:31 -1000 |
| commit | 2e481152920c23cb939e7224aac245973a00ee95 (patch) | |
| tree | c30310acbdf1f6c74676e209fbd3f8edaaa257d3 /flag.go | |
| parent | 929490184252ab7e27a45d03738303e99df438f7 (diff) | |
Add support for non-interspersed option/non-option args.
Diffstat (limited to 'flag.go')
| -rw-r--r-- | flag.go | 33 |
1 files changed, 24 insertions, 9 deletions
@@ -260,15 +260,16 @@ type FlagSet struct { // a custom error handler. Usage func() - name string - parsed bool - actual map[string]*Flag - formal map[string]*Flag - shorthands map[byte]*Flag - args []string // arguments after flags - exitOnError bool // does the program exit if there's an error? - errorHandling ErrorHandling - output io.Writer // nil means stderr; use out() accessor + name string + parsed bool + actual map[string]*Flag + formal map[string]*Flag + shorthands map[byte]*Flag + args []string // arguments after flags + exitOnError bool // does the program exit if there's an error? + errorHandling ErrorHandling + output io.Writer // nil means stderr; use out() accessor + noInterspersed bool // do not allow interspersed option/non-option args } // A Flag represents the state of a flag. @@ -919,6 +920,11 @@ func (f *FlagSet) parseArgs(args []string) error { s := args[0] args = args[1:] if len(s) == 0 || s[0] != '-' || len(s) == 1 { + if f.noInterspersed { + f.args = append(f.args, s) + f.args = append(f.args, args...) + return nil + } f.args = append(f.args, s) continue } @@ -1022,6 +1028,11 @@ func Parse() { commandLine.Parse(os.Args[1:]) } +// Do not support interspersed option/non-option arguments. +func NoInterspersed() { + commandLine.NoInterspersed() +} + // Parsed returns true if the command-line flags have been parsed. func Parsed() bool { return commandLine.Parsed() @@ -1040,6 +1051,10 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet { return f } +func (f *FlagSet) NoInterspersed() { + f.noInterspersed = true +} + // Init sets the name and error handling property for a flag set. // By default, the zero FlagSet uses an empty name and the // ContinueOnError error handling policy. |
