Custom type for arguments
This commit is contained in:
parent
fbbda7a902
commit
1bea6dcbf0
4
app.go
4
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)
|
||||
|
@ -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}
|
||||
|
@ -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())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -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 {
|
||||
|
36
context.go
36
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user