diff --git a/app.go b/app.go index db9e984..b8ea635 100644 --- a/app.go +++ b/app.go @@ -86,8 +86,8 @@ func (a *App) Run(arguments []string) error { } args := context.Args() - if len(args) > 0 { - name := args[0] + if args.Present() { + name := args.First() c := a.Command(name) if c != nil { return c.Run(context) diff --git a/app_test.go b/app_test.go index f3646e9..893b799 100644 --- a/app_test.go +++ b/app_test.go @@ -29,7 +29,7 @@ func TestApp_Run(t *testing.T) { app := cli.NewApp() app.Action = func(c *cli.Context) { - s = s + c.Args()[0] + s = s + c.Args().First() } err := app.Run([]string{"command", "foo"}) @@ -76,7 +76,7 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) { }, Action: func(c *cli.Context) { parsedOption = c.String("option") - firstArg = c.Args()[0] + firstArg = c.Args().First() }, } app.Commands = []cli.Command{command} @@ -103,7 +103,7 @@ func TestApp_ParseSliceFlags(t *testing.T) { parsedIntSlice = c.IntSlice("p") parsedStringSlice = c.StringSlice("ip") parsedOption = c.String("option") - firstArg = c.Args()[0] + firstArg = c.Args().First() }, } app.Commands = []cli.Command{command} diff --git a/cli_test.go b/cli_test.go index 8e535ec..772e90f 100644 --- a/cli_test.go +++ b/cli_test.go @@ -15,7 +15,7 @@ func Example() { ShortName: "a", Usage: "add a task to the list", Action: func(c *cli.Context) { - println("added task: ", c.Args()[0]) + println("added task: ", c.Args().First()) }, }, { @@ -23,7 +23,7 @@ func Example() { ShortName: "c", Usage: "complete a task on the list", Action: func(c *cli.Context) { - println("completed task: ", c.Args()[0]) + println("completed task: ", c.Args().First()) }, }, } diff --git a/command.go b/command.go index 79af87e..7ef7b01 100644 --- a/command.go +++ b/command.go @@ -43,11 +43,12 @@ func (c Command) Run(ctx *Context) error { var err error if firstFlagIndex > -1 { - args := ctx.Args()[1:firstFlagIndex] - flags := ctx.Args()[firstFlagIndex:] - err = set.Parse(append(flags, args...)) + args := ctx.Args() + regularArgs := args[1:firstFlagIndex] + flagArgs := args[firstFlagIndex:] + err = set.Parse(append(flagArgs, regularArgs...)) } else { - err = set.Parse(ctx.Args()[1:]) + err = set.Parse(ctx.Args().Tail()) } if err != nil { diff --git a/context.go b/context.go index d944d70..695ebe3 100644 --- a/context.go +++ b/context.go @@ -15,6 +15,8 @@ type Context struct { globalSet *flag.FlagSet } +type Args []string + // Creates a new context. For use in when invoking an App or Command action. func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context { return &Context{app, set, globalSet} @@ -71,8 +73,9 @@ func (c *Context) GlobalIntSlice(name string) []int { } // Returns the command line arguments associated with the context. -func (c *Context) Args() []string { - return c.flagSet.Args() +func (c *Context) Args() Args { + args := Args(c.flagSet.Args()) + return args } func (c *Context) lookupInt(name string, set *flag.FlagSet) int { @@ -130,16 +133,29 @@ func (c *Context) lookupBool(name string, set *flag.FlagSet) bool { return false } -// Returns the nth argument, or just a blank string -func (c *Context) GetArg(n int) string { - args := c.Args() - if len(args) < n { - return args[n] +// Returns the nth argument, or else a blank string +func (a Args) Get(n int) string { + if len(a) > n { + return a[n] } return "" } -// Returns the first argument, or just a blank string, for convenience -func (c *Context) FirstArg() string { - return c.GetArg(0) +// Returns the first argument, or else a blank string +func (a Args) First() string { + return a.Get(0) +} + +// Return the rest of the arguments (not the first one) +// or else an empty string slice +func (a Args) Tail() []string { + if len(a) >= 2 { + return []string(a)[1:] + } + return []string{} +} + +// Checks if there are any arguments present +func (a Args) Present() bool { + return len(a) != 0 } diff --git a/help.go b/help.go index 9ac997b..e91497c 100644 --- a/help.go +++ b/help.go @@ -76,8 +76,8 @@ var helpCommand = Command{ Usage: "Shows a list of commands or help for one command", Action: func(c *Context) { args := c.Args() - if len(args) > 0 { - ShowCommandHelp(c, args[0]) + if args.Present() { + ShowCommandHelp(c, args.First()) } else { ShowAppHelp(c) }