Merge pull request #80 from wendorf/master
Add SkipFlagParsing option to Command
This commit is contained in:
commit
73aae5979c
@ -27,6 +27,8 @@ type Command struct {
|
|||||||
Subcommands []Command
|
Subcommands []Command
|
||||||
// List of flags to parse
|
// List of flags to parse
|
||||||
Flags []Flag
|
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
|
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||||
@ -58,7 +60,7 @@ func (c Command) Run(ctx *Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if firstFlagIndex > -1 {
|
if firstFlagIndex > -1 && !c.SkipFlagParsing{
|
||||||
args := ctx.Args()
|
args := ctx.Args()
|
||||||
regularArgs := args[1:firstFlagIndex]
|
regularArgs := args[1:firstFlagIndex]
|
||||||
flagArgs := args[firstFlagIndex:]
|
flagArgs := args[firstFlagIndex:]
|
||||||
|
48
command_test.go
Normal file
48
command_test.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user