aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorAndy Goldstein <[email protected]>2015-01-26 16:18:00 -0500
committerAndy Goldstein <[email protected]>2015-02-17 20:24:54 -0500
commitf160cd4dadf5efaabbeecea67ad3486a452ab72b (patch)
tree962e5398db836210679537773d6430b3ad574165 /flag_test.go
parentf82776d6cc998e3c026baef7b24409ff49fe5c8d (diff)
Fix handling of -- termination
Fix handling of -- termination. Previously, if you ran $command -flag1 -etc -- some args after the command's args would be doubled (some args after some args after). Additionally, if there were items after the -- termination that looked like flags, the parser would throw an error. It's better to terminate parsing when -- is seen, because that usually indicates the caller wants to pass the remainder of the arguments (including flags) as-is to something else.
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go38
1 files changed, 36 insertions, 2 deletions
diff --git a/flag_test.go b/flag_test.go
index 47865bd..a33c601 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -197,8 +197,8 @@ func TestShorthand(t *testing.T) {
notaflag,
}
f.SetOutput(ioutil.Discard)
- if err := f.Parse(args); err == nil {
- t.Error("--i-look-like-a-flag should throw an error")
+ if err := f.Parse(args); err != nil {
+ t.Error("expected no error, got ", err)
}
if !f.Parsed() {
t.Error("f.Parse() = false after Parse")
@@ -356,3 +356,37 @@ func TestNoInterspersed(t *testing.T) {
t.Fatal("expected interspersed options/non-options to fail")
}
}
+
+func TestTermination(t *testing.T) {
+ f := NewFlagSet("termination", ContinueOnError)
+ boolFlag := f.BoolP("bool", "l", false, "bool value")
+ if f.Parsed() {
+ t.Error("f.Parse() = true before Parse")
+ }
+ arg1 := "ls"
+ arg2 := "-l"
+ args := []string{
+ "--",
+ arg1,
+ arg2,
+ }
+ f.SetOutput(ioutil.Discard)
+ if err := f.Parse(args); err != nil {
+ t.Fatal("expected no error; got ", err)
+ }
+ if !f.Parsed() {
+ t.Error("f.Parse() = false after Parse")
+ }
+ if *boolFlag {
+ t.Error("expected boolFlag=false, got true")
+ }
+ if len(f.Args()) != 2 {
+ t.Errorf("expected 2 arguments, got %d: %v", len(f.Args()), f.Args())
+ }
+ if f.Args()[0] != arg1 {
+ t.Errorf("expected argument %q got %q", arg1, f.Args()[0])
+ }
+ if f.Args()[1] != arg2 {
+ t.Errorf("expected argument %q got %q", arg2, f.Args()[1])
+ }
+}