From edde52e321465d7e0fbb1e1a32a8a8225ddd1f6a Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Wed, 16 Sep 2015 12:31:41 -0400 Subject: [PATCH] Provide the length of Args when a -- is found in commandline This allows a program to know what args came before the -- and what args came after. --- cobra_test.go | 10 ++++++++-- command.go | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 8b43380..c9eef5d 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -416,8 +416,11 @@ func TestGrandChildSameName(t *testing.T) { } func TestFlagLong(t *testing.T) { - noRRSetupTest("echo --intone=13 something here") + noRRSetupTest("echo --intone=13 something -- here") + if cmdEcho.ArgsLenAtDash() != 1 { + t.Errorf("expected argsLenAtDash: %d but got %d", 1, cmdRootNoRun.ArgsLenAtDash()) + } if strings.Join(te, " ") != "something here" { t.Errorf("flags didn't leave proper args remaining..%s given", te) } @@ -430,8 +433,11 @@ func TestFlagLong(t *testing.T) { } func TestFlagShort(t *testing.T) { - noRRSetupTest("echo -i13 something here") + noRRSetupTest("echo -i13 -- something here") + if cmdEcho.ArgsLenAtDash() != 0 { + t.Errorf("expected argsLenAtDash: %d but got %d", 0, cmdRootNoRun.ArgsLenAtDash()) + } if strings.Join(te, " ") != "something here" { t.Errorf("flags didn't leave proper args remaining..%s given", te) } diff --git a/command.go b/command.go index 6f73f71..0cc0b72 100644 --- a/command.go +++ b/command.go @@ -470,6 +470,10 @@ func (c *Command) Root() *Command { return findRoot(c) } +func (c *Command) ArgsLenAtDash() int { + return c.Flags().ArgsLenAtDash() +} + func (c *Command) execute(a []string) (err error) { if c == nil { return fmt.Errorf("Called Execute() on a nil Command")