aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlec Thomas <[email protected]>2012-12-19 20:36:31 -1000
committerAlec Thomas <[email protected]>2012-12-19 20:36:31 -1000
commit2e481152920c23cb939e7224aac245973a00ee95 (patch)
treec30310acbdf1f6c74676e209fbd3f8edaaa257d3
parent929490184252ab7e27a45d03738303e99df438f7 (diff)
Add support for non-interspersed option/non-option args.
-rw-r--r--flag.go33
-rw-r--r--flag_test.go15
2 files changed, 39 insertions, 9 deletions
diff --git a/flag.go b/flag.go
index 06edf20..4f6f93f 100644
--- a/flag.go
+++ b/flag.go
@@ -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.
diff --git a/flag_test.go b/flag_test.go
index 9caa8c4..635f431 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -335,3 +335,18 @@ func TestHelp(t *testing.T) {
t.Fatal("help was called; should not have been for defined help flag")
}
}
+
+func TestNoInterspersed(t *testing.T) {
+ f := NewFlagSet("test", ContinueOnError)
+ f.NoInterspersed()
+ f.Bool("true", true, "always true")
+ f.Bool("false", false, "always false")
+ err := f.Parse([]string{"--true", "break", "--false"})
+ if err != nil {
+ t.Fatal("expected no error; got ", err)
+ }
+ args := f.Args()
+ if len(args) != 2 || args[0] != "break" || args[1] != "--false" {
+ t.Fatal("expected interspersed options/non-options to fail")
+ }
+}