Merge pull request #336 from muraty/master

Add NArg method to context
This commit is contained in:
Jesse Szwedko 2016-02-27 08:56:50 -08:00
commit a2943485b1
3 changed files with 16 additions and 3 deletions

View File

@ -153,7 +153,7 @@ app.Flags = []cli.Flag {
}
app.Action = func(c *cli.Context) {
name := "someone"
if len(c.Args()) > 0 {
if c.NArg() > 0 {
name = c.Args()[0]
}
if c.String("lang") == "spanish" {
@ -180,7 +180,7 @@ app.Flags = []cli.Flag {
}
app.Action = func(c *cli.Context) {
name := "someone"
if len(c.Args()) > 0 {
if c.NArg() > 0 {
name = c.Args()[0]
}
if language == "spanish" {
@ -308,7 +308,7 @@ app.Commands = []cli.Command{
},
BashComplete: func(c *cli.Context) {
// This will complete if no args are passed
if len(c.Args()) > 0 {
if c.NArg() > 0 {
return
}
for _, t := range tasks {

View File

@ -197,6 +197,11 @@ func (c *Context) Args() Args {
return args
}
// Returns the number of the command line arguments.
func (c *Context) NArg() int {
return len(c.Args())
}
// Returns the nth argument, or else a blank string
func (a Args) Get(n int) string {
if len(a) > n {

View File

@ -64,6 +64,14 @@ func TestContext_Args(t *testing.T) {
expect(t, c.Bool("myflag"), true)
}
func TestContext_NArg(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
c := NewContext(nil, set, nil)
set.Parse([]string{"--myflag", "bat", "baz"})
expect(t, c.NArg(), 2)
}
func TestContext_IsSet(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")