Merge branch 'master' of github.com:xyproto/cli into xyproto-master

Conflicts:
	app.go
	context.go
This commit is contained in:
Jeremy Saenz
2013-11-28 07:51:31 -08:00
7 changed files with 94 additions and 35 deletions

View File

@@ -17,6 +17,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}
@@ -73,8 +75,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 lookupInt(name string, set *flag.FlagSet) int {
@@ -162,3 +165,30 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
}
return nil
}
// 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 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
}