|
|
|
@ -21,57 +21,62 @@ type Context struct {
|
|
|
|
|
parentContext *Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a new context. For use in when invoking an App or Command action.
|
|
|
|
|
// NewContext creates a new context. For use in when invoking an App or Command action.
|
|
|
|
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
|
|
|
|
return &Context{App: app, flagSet: set, parentContext: parentCtx}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local int flag, returns 0 if no int flag exists
|
|
|
|
|
// Int looks up the value of a local int flag, returns 0 if no int flag exists
|
|
|
|
|
func (c *Context) Int(name string) int {
|
|
|
|
|
return lookupInt(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local time.Duration flag, returns 0 if no time.Duration flag exists
|
|
|
|
|
// Duration looks up the value of a local time.Duration flag, returns 0 if no
|
|
|
|
|
// time.Duration flag exists
|
|
|
|
|
func (c *Context) Duration(name string) time.Duration {
|
|
|
|
|
return lookupDuration(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local float64 flag, returns 0 if no float64 flag exists
|
|
|
|
|
// Float64 looks up the value of a local float64 flag, returns 0 if no float64
|
|
|
|
|
// flag exists
|
|
|
|
|
func (c *Context) Float64(name string) float64 {
|
|
|
|
|
return lookupFloat64(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local bool flag, returns false if no bool flag exists
|
|
|
|
|
// Bool looks up the value of a local bool flag, returns false if no bool flag exists
|
|
|
|
|
func (c *Context) Bool(name string) bool {
|
|
|
|
|
return lookupBool(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local boolT flag, returns false if no bool flag exists
|
|
|
|
|
// BoolT looks up the value of a local boolT flag, returns false if no bool flag exists
|
|
|
|
|
func (c *Context) BoolT(name string) bool {
|
|
|
|
|
return lookupBoolT(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local string flag, returns "" if no string flag exists
|
|
|
|
|
// String looks up the value of a local string flag, returns "" if no string flag exists
|
|
|
|
|
func (c *Context) String(name string) string {
|
|
|
|
|
return lookupString(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local string slice flag, returns nil if no string slice flag exists
|
|
|
|
|
// StringSlice looks up the value of a local string slice flag, returns nil if no
|
|
|
|
|
// string slice flag exists
|
|
|
|
|
func (c *Context) StringSlice(name string) []string {
|
|
|
|
|
return lookupStringSlice(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local int slice flag, returns nil if no int slice flag exists
|
|
|
|
|
// IntSlice looks up the value of a local int slice flag, returns nil if no int
|
|
|
|
|
// slice flag exists
|
|
|
|
|
func (c *Context) IntSlice(name string) []int {
|
|
|
|
|
return lookupIntSlice(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a local generic flag, returns nil if no generic flag exists
|
|
|
|
|
// Generic looks up the value of a local generic flag, returns nil if no generic
|
|
|
|
|
// flag exists
|
|
|
|
|
func (c *Context) Generic(name string) interface{} {
|
|
|
|
|
return lookupGeneric(name, c.flagSet)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
|
|
|
|
// GlobalInt looks up the value of a global int flag, returns 0 if no int flag exists
|
|
|
|
|
func (c *Context) GlobalInt(name string) int {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupInt(name, fs)
|
|
|
|
@ -79,8 +84,8 @@ func (c *Context) GlobalInt(name string) int {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global float64 flag, returns float64(0) if no float64
|
|
|
|
|
// flag exists
|
|
|
|
|
// GlobalFloat64 looks up the value of a global float64 flag, returns float64(0)
|
|
|
|
|
// if no float64 flag exists
|
|
|
|
|
func (c *Context) GlobalFloat64(name string) float64 {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupFloat64(name, fs)
|
|
|
|
@ -88,7 +93,8 @@ func (c *Context) GlobalFloat64(name string) float64 {
|
|
|
|
|
return float64(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
|
|
|
|
// GlobalDuration looks up the value of a global time.Duration flag, returns 0
|
|
|
|
|
// if no time.Duration flag exists
|
|
|
|
|
func (c *Context) GlobalDuration(name string) time.Duration {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupDuration(name, fs)
|
|
|
|
@ -96,7 +102,8 @@ func (c *Context) GlobalDuration(name string) time.Duration {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
|
|
|
|
// GlobalBool looks up the value of a global bool flag, returns false if no bool
|
|
|
|
|
// flag exists
|
|
|
|
|
func (c *Context) GlobalBool(name string) bool {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupBool(name, fs)
|
|
|
|
@ -104,7 +111,8 @@ func (c *Context) GlobalBool(name string) bool {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global bool flag, returns true if no bool flag exists
|
|
|
|
|
// GlobalBoolT looks up the value of a global bool flag, returns true if no bool
|
|
|
|
|
// flag exists
|
|
|
|
|
func (c *Context) GlobalBoolT(name string) bool {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupBoolT(name, fs)
|
|
|
|
@ -112,7 +120,8 @@ func (c *Context) GlobalBoolT(name string) bool {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global string flag, returns "" if no string flag exists
|
|
|
|
|
// GlobalString looks up the value of a global string flag, returns "" if no
|
|
|
|
|
// string flag exists
|
|
|
|
|
func (c *Context) GlobalString(name string) string {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupString(name, fs)
|
|
|
|
@ -120,7 +129,8 @@ func (c *Context) GlobalString(name string) string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
|
|
|
|
|
// GlobalStringSlice looks up the value of a global string slice flag, returns
|
|
|
|
|
// nil if no string slice flag exists
|
|
|
|
|
func (c *Context) GlobalStringSlice(name string) []string {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupStringSlice(name, fs)
|
|
|
|
@ -128,7 +138,8 @@ func (c *Context) GlobalStringSlice(name string) []string {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
|
|
|
|
|
// GlobalIntSlice looks up the value of a global int slice flag, returns nil if
|
|
|
|
|
// no int slice flag exists
|
|
|
|
|
func (c *Context) GlobalIntSlice(name string) []int {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupIntSlice(name, fs)
|
|
|
|
@ -136,7 +147,8 @@ func (c *Context) GlobalIntSlice(name string) []int {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looks up the value of a global generic flag, returns nil if no generic flag exists
|
|
|
|
|
// GlobalGeneric looks up the value of a global generic flag, returns nil if no
|
|
|
|
|
// generic flag exists
|
|
|
|
|
func (c *Context) GlobalGeneric(name string) interface{} {
|
|
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
|
|
return lookupGeneric(name, fs)
|
|
|
|
@ -144,7 +156,7 @@ func (c *Context) GlobalGeneric(name string) interface{} {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the number of flags set
|
|
|
|
|
// NumFlags returns the number of flags set
|
|
|
|
|
func (c *Context) NumFlags() int {
|
|
|
|
|
return c.flagSet.NFlag()
|
|
|
|
|
}
|
|
|
|
@ -159,7 +171,7 @@ func (c *Context) GlobalSet(name, value string) error {
|
|
|
|
|
return globalContext(c).flagSet.Set(name, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determines if the flag was actually set
|
|
|
|
|
// IsSet determines if the flag was actually set
|
|
|
|
|
func (c *Context) IsSet(name string) bool {
|
|
|
|
|
if c.setFlags == nil {
|
|
|
|
|
c.setFlags = make(map[string]bool)
|
|
|
|
@ -170,7 +182,7 @@ func (c *Context) IsSet(name string) bool {
|
|
|
|
|
return c.setFlags[name] == true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determines if the global flag was actually set
|
|
|
|
|
// GlobalIsSet determines if the global flag was actually set
|
|
|
|
|
func (c *Context) GlobalIsSet(name string) bool {
|
|
|
|
|
if c.globalSetFlags == nil {
|
|
|
|
|
c.globalSetFlags = make(map[string]bool)
|
|
|
|
@ -187,7 +199,7 @@ func (c *Context) GlobalIsSet(name string) bool {
|
|
|
|
|
return c.globalSetFlags[name]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a slice of flag names used in this context.
|
|
|
|
|
// FlagNames returns a slice of flag names used in this context.
|
|
|
|
|
func (c *Context) FlagNames() (names []string) {
|
|
|
|
|
for _, flag := range c.Command.Flags {
|
|
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
|
|
@ -199,7 +211,7 @@ func (c *Context) FlagNames() (names []string) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a slice of global flag names used by the app.
|
|
|
|
|
// GlobalFlagNames returns a slice of global flag names used by the app.
|
|
|
|
|
func (c *Context) GlobalFlagNames() (names []string) {
|
|
|
|
|
for _, flag := range c.App.Flags {
|
|
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
|
|
@ -211,25 +223,26 @@ func (c *Context) GlobalFlagNames() (names []string) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the parent context, if any
|
|
|
|
|
// Parent returns the parent context, if any
|
|
|
|
|
func (c *Context) Parent() *Context {
|
|
|
|
|
return c.parentContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Args contains apps console arguments
|
|
|
|
|
type Args []string
|
|
|
|
|
|
|
|
|
|
// Returns the command line arguments associated with the context.
|
|
|
|
|
// Args returns the command line arguments associated with the context.
|
|
|
|
|
func (c *Context) Args() Args {
|
|
|
|
|
args := Args(c.flagSet.Args())
|
|
|
|
|
return args
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the number of the command line arguments.
|
|
|
|
|
// NArg 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
|
|
|
|
|
// Get returns the nth argument, or else a blank string
|
|
|
|
|
func (a Args) Get(n int) string {
|
|
|
|
|
if len(a) > n {
|
|
|
|
|
return a[n]
|
|
|
|
@ -237,12 +250,12 @@ func (a Args) Get(n int) string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the first argument, or else a blank string
|
|
|
|
|
// First 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)
|
|
|
|
|
// Tail returns the rest of the arguments (not the first one)
|
|
|
|
|
// or else an empty string slice
|
|
|
|
|
func (a Args) Tail() []string {
|
|
|
|
|
if len(a) >= 2 {
|
|
|
|
@ -251,12 +264,12 @@ func (a Args) Tail() []string {
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks if there are any arguments present
|
|
|
|
|
// Present checks if there are any arguments present
|
|
|
|
|
func (a Args) Present() bool {
|
|
|
|
|
return len(a) != 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Swaps arguments at the given indexes
|
|
|
|
|
// Swap swaps arguments at the given indexes
|
|
|
|
|
func (a Args) Swap(from, to int) error {
|
|
|
|
|
if from >= len(a) || to >= len(a) {
|
|
|
|
|
return errors.New("index out of range")
|
|
|
|
|