Make slice wrapping pattern more consistent

and move *Args out into its own file
This commit is contained in:
Dan Buch
2016-05-23 20:15:35 -04:00
parent 59cdedb334
commit 61710ff108
7 changed files with 113 additions and 101 deletions

View File

@@ -157,59 +157,6 @@ func (c *Context) NArg() int {
return c.Args().Len()
}
// Args wraps a string slice with some convenience methods
type Args struct {
slice []string
}
// Get returns the nth argument, or else a blank string
func (a *Args) Get(n int) string {
if len(a.slice) > n {
return a.slice[n]
}
return ""
}
// First returns the first argument, or else a blank string
func (a *Args) First() string {
return a.Get(0)
}
// Tail returns the rest of the arguments (not the first one)
// or else an empty string slice
func (a *Args) Tail() []string {
if a.Len() >= 2 {
return a.slice[1:]
}
return []string{}
}
// Len returns the length of the wrapped slice
func (a *Args) Len() int {
return len(a.slice)
}
// Present checks if there are any arguments present
func (a *Args) Present() bool {
return a.Len() != 0
}
// Swap swaps arguments at the given indexes
func (a *Args) Swap(from, to int) error {
if from >= a.Len() || to >= a.Len() {
return errors.New("index out of range")
}
a.slice[from], a.slice[to] = a.slice[to], a.slice[from]
return nil
}
// Slice returns a copy of the internal slice
func (a *Args) Slice() []string {
ret := make([]string, len(a.slice))
copy(ret, a.slice)
return ret
}
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
for _, c := range ctx.Lineage() {
if f := c.flagSet.Lookup(name); f != nil {