From 6c5f810d8fc8e7ecc1c92e391c5be4bc5a028a4e Mon Sep 17 00:00:00 2001 From: Dan Wendorf and Karen Wang Date: Mon, 14 Apr 2014 14:44:32 -0700 Subject: [PATCH] Add SkipFlagParsing option to Command --- command.go | 4 +++- command_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 command_test.go diff --git a/command.go b/command.go index 60d29aa..73cfb05 100644 --- a/command.go +++ b/command.go @@ -22,6 +22,8 @@ type Command struct { Action func(context *Context) // List of flags to parse Flags []Flag + // Treat all flags as normal arguments if true + SkipFlagParsing bool } // Invokes the command given the context, parses ctx.Args() to generate command-specific flags @@ -48,7 +50,7 @@ func (c Command) Run(ctx *Context) error { } var err error - if firstFlagIndex > -1 { + if firstFlagIndex > -1 && !c.SkipFlagParsing{ args := ctx.Args() regularArgs := args[1:firstFlagIndex] flagArgs := args[firstFlagIndex:] diff --git a/command_test.go b/command_test.go new file mode 100644 index 0000000..3afd83e --- /dev/null +++ b/command_test.go @@ -0,0 +1,48 @@ +package cli_test + +import ( + "flag" + "github.com/codegangsta/cli" + "testing" +) + +func TestCommandDoNotIgnoreFlags(t *testing.T) { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + test := []string{"blah", "blah", "-break"} + set.Parse(test) + + c := cli.NewContext(app, set, set) + + command := cli.Command { + Name: "test-cmd", + ShortName: "tc", + Usage: "this is for testing", + Description: "testing", + Action: func(_ *cli.Context) { }, + } + err := command.Run(c) + + expect(t, err.Error(), "flag provided but not defined: -break") +} + +func TestCommandIgnoreFlags(t *testing.T) { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + test := []string{"blah", "blah"} + set.Parse(test) + + c := cli.NewContext(app, set, set) + + command := cli.Command { + Name: "test-cmd", + ShortName: "tc", + Usage: "this is for testing", + Description: "testing", + Action: func(_ *cli.Context) { }, + SkipFlagParsing: true, + } + err := command.Run(c) + + expect(t, err, nil) +}