From f160cd4dadf5efaabbeecea67ad3486a452ab72b Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Mon, 26 Jan 2015 16:18:00 -0500 Subject: 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. --- flag_test.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'flag_test.go') 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]) + } +} -- cgit v1.2.3