Add NArg method to context structure
This commit is contained in:
parent
5db74198de
commit
802f64479d
@ -153,7 +153,7 @@ app.Flags = []cli.Flag {
|
|||||||
}
|
}
|
||||||
app.Action = func(c *cli.Context) {
|
app.Action = func(c *cli.Context) {
|
||||||
name := "someone"
|
name := "someone"
|
||||||
if len(c.Args()) > 0 {
|
if c.NArg() > 0 {
|
||||||
name = c.Args()[0]
|
name = c.Args()[0]
|
||||||
}
|
}
|
||||||
if c.String("lang") == "spanish" {
|
if c.String("lang") == "spanish" {
|
||||||
@ -180,7 +180,7 @@ app.Flags = []cli.Flag {
|
|||||||
}
|
}
|
||||||
app.Action = func(c *cli.Context) {
|
app.Action = func(c *cli.Context) {
|
||||||
name := "someone"
|
name := "someone"
|
||||||
if len(c.Args()) > 0 {
|
if c.NArg() > 0 {
|
||||||
name = c.Args()[0]
|
name = c.Args()[0]
|
||||||
}
|
}
|
||||||
if language == "spanish" {
|
if language == "spanish" {
|
||||||
@ -308,7 +308,7 @@ app.Commands = []cli.Command{
|
|||||||
},
|
},
|
||||||
BashComplete: func(c *cli.Context) {
|
BashComplete: func(c *cli.Context) {
|
||||||
// This will complete if no args are passed
|
// This will complete if no args are passed
|
||||||
if len(c.Args()) > 0 {
|
if c.NArg() > 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, t := range tasks {
|
for _, t := range tasks {
|
||||||
|
@ -197,6 +197,11 @@ func (c *Context) Args() Args {
|
|||||||
return 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
|
// Returns the nth argument, or else a blank string
|
||||||
func (a Args) Get(n int) string {
|
func (a Args) Get(n int) string {
|
||||||
if len(a) > n {
|
if len(a) > n {
|
||||||
|
@ -64,6 +64,14 @@ func TestContext_Args(t *testing.T) {
|
|||||||
expect(t, c.Bool("myflag"), true)
|
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) {
|
func TestContext_IsSet(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
|
Loading…
Reference in New Issue
Block a user