|
@@ -21,57 +21,62 @@ type Context struct {
|
|
|
parentContext *Context
|
|
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 {
|
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
|
|
return &Context{App: app, flagSet: set, parentContext: parentCtx}
|
|
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 {
|
|
func (c *Context) Int(name string) int {
|
|
|
return lookupInt(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) Duration(name string) time.Duration {
|
|
|
return lookupDuration(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) Float64(name string) float64 {
|
|
|
return lookupFloat64(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) Bool(name string) bool {
|
|
|
return lookupBool(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) BoolT(name string) bool {
|
|
|
return lookupBoolT(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) String(name string) string {
|
|
|
return lookupString(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) StringSlice(name string) []string {
|
|
|
return lookupStringSlice(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) IntSlice(name string) []int {
|
|
|
return lookupIntSlice(name, c.flagSet)
|
|
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{} {
|
|
func (c *Context) Generic(name string) interface{} {
|
|
|
return lookupGeneric(name, c.flagSet)
|
|
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 {
|
|
func (c *Context) GlobalInt(name string) int {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupInt(name, fs)
|
|
return lookupInt(name, fs)
|
|
@@ -79,7 +84,17 @@ func (c *Context) GlobalInt(name string) int {
|
|
|
return 0
|
|
return 0
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration 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)
|
|
|
|
|
+ }
|
|
|
|
|
+ return float64(0)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 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 {
|
|
func (c *Context) GlobalDuration(name string) time.Duration {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupDuration(name, fs)
|
|
return lookupDuration(name, fs)
|
|
@@ -87,7 +102,8 @@ func (c *Context) GlobalDuration(name string) time.Duration {
|
|
|
return 0
|
|
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 {
|
|
func (c *Context) GlobalBool(name string) bool {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupBool(name, fs)
|
|
return lookupBool(name, fs)
|
|
@@ -95,7 +111,17 @@ func (c *Context) GlobalBool(name string) bool {
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Looks up the value of a global string flag, returns "" if no string 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)
|
|
|
|
|
+ }
|
|
|
|
|
+ return false
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GlobalString looks up the value of a global string flag, returns "" if no
|
|
|
|
|
+// string flag exists
|
|
|
func (c *Context) GlobalString(name string) string {
|
|
func (c *Context) GlobalString(name string) string {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupString(name, fs)
|
|
return lookupString(name, fs)
|
|
@@ -103,7 +129,8 @@ func (c *Context) GlobalString(name string) string {
|
|
|
return ""
|
|
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 {
|
|
func (c *Context) GlobalStringSlice(name string) []string {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupStringSlice(name, fs)
|
|
return lookupStringSlice(name, fs)
|
|
@@ -111,7 +138,8 @@ func (c *Context) GlobalStringSlice(name string) []string {
|
|
|
return nil
|
|
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 {
|
|
func (c *Context) GlobalIntSlice(name string) []int {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupIntSlice(name, fs)
|
|
return lookupIntSlice(name, fs)
|
|
@@ -119,7 +147,8 @@ func (c *Context) GlobalIntSlice(name string) []int {
|
|
|
return nil
|
|
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{} {
|
|
func (c *Context) GlobalGeneric(name string) interface{} {
|
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
|
return lookupGeneric(name, fs)
|
|
return lookupGeneric(name, fs)
|
|
@@ -127,12 +156,22 @@ func (c *Context) GlobalGeneric(name string) interface{} {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Returns the number of flags set
|
|
|
|
|
|
|
+// NumFlags returns the number of flags set
|
|
|
func (c *Context) NumFlags() int {
|
|
func (c *Context) NumFlags() int {
|
|
|
return c.flagSet.NFlag()
|
|
return c.flagSet.NFlag()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Determines if the flag was actually set
|
|
|
|
|
|
|
+// Set sets a context flag to a value.
|
|
|
|
|
+func (c *Context) Set(name, value string) error {
|
|
|
|
|
+ return c.flagSet.Set(name, value)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GlobalSet sets a context flag to a value on the global flagset
|
|
|
|
|
+func (c *Context) GlobalSet(name, value string) error {
|
|
|
|
|
+ return globalContext(c).flagSet.Set(name, value)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// IsSet determines if the flag was actually set
|
|
|
func (c *Context) IsSet(name string) bool {
|
|
func (c *Context) IsSet(name string) bool {
|
|
|
if c.setFlags == nil {
|
|
if c.setFlags == nil {
|
|
|
c.setFlags = make(map[string]bool)
|
|
c.setFlags = make(map[string]bool)
|
|
@@ -143,7 +182,7 @@ func (c *Context) IsSet(name string) bool {
|
|
|
return c.setFlags[name] == true
|
|
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 {
|
|
func (c *Context) GlobalIsSet(name string) bool {
|
|
|
if c.globalSetFlags == nil {
|
|
if c.globalSetFlags == nil {
|
|
|
c.globalSetFlags = make(map[string]bool)
|
|
c.globalSetFlags = make(map[string]bool)
|
|
@@ -160,7 +199,7 @@ func (c *Context) GlobalIsSet(name string) bool {
|
|
|
return c.globalSetFlags[name]
|
|
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) {
|
|
func (c *Context) FlagNames() (names []string) {
|
|
|
for _, flag := range c.Command.Flags {
|
|
for _, flag := range c.Command.Flags {
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
@@ -172,7 +211,7 @@ func (c *Context) FlagNames() (names []string) {
|
|
|
return
|
|
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) {
|
|
func (c *Context) GlobalFlagNames() (names []string) {
|
|
|
for _, flag := range c.App.Flags {
|
|
for _, flag := range c.App.Flags {
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
|
@@ -184,20 +223,26 @@ func (c *Context) GlobalFlagNames() (names []string) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Returns the parent context, if any
|
|
|
|
|
|
|
+// Parent returns the parent context, if any
|
|
|
func (c *Context) Parent() *Context {
|
|
func (c *Context) Parent() *Context {
|
|
|
return c.parentContext
|
|
return c.parentContext
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Args contains apps console arguments
|
|
|
type Args []string
|
|
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 {
|
|
func (c *Context) Args() Args {
|
|
|
args := Args(c.flagSet.Args())
|
|
args := Args(c.flagSet.Args())
|
|
|
return args
|
|
return args
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Returns the nth argument, or else a blank string
|
|
|
|
|
|
|
+// NArg returns the number of the command line arguments.
|
|
|
|
|
+func (c *Context) NArg() int {
|
|
|
|
|
+ return len(c.Args())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Get 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 {
|
|
|
return a[n]
|
|
return a[n]
|
|
@@ -205,12 +250,12 @@ func (a Args) Get(n int) string {
|
|
|
return ""
|
|
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 {
|
|
func (a Args) First() string {
|
|
|
return a.Get(0)
|
|
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
|
|
// or else an empty string slice
|
|
|
func (a Args) Tail() []string {
|
|
func (a Args) Tail() []string {
|
|
|
if len(a) >= 2 {
|
|
if len(a) >= 2 {
|
|
@@ -219,12 +264,12 @@ func (a Args) Tail() []string {
|
|
|
return []string{}
|
|
return []string{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Checks if there are any arguments present
|
|
|
|
|
|
|
+// Present checks if there are any arguments present
|
|
|
func (a Args) Present() bool {
|
|
func (a Args) Present() bool {
|
|
|
return len(a) != 0
|
|
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 {
|
|
func (a Args) Swap(from, to int) error {
|
|
|
if from >= len(a) || to >= len(a) {
|
|
if from >= len(a) || to >= len(a) {
|
|
|
return errors.New("index out of range")
|
|
return errors.New("index out of range")
|
|
@@ -233,6 +278,19 @@ func (a Args) Swap(from, to int) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func globalContext(ctx *Context) *Context {
|
|
|
|
|
+ if ctx == nil {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for {
|
|
|
|
|
+ if ctx.parentContext == nil {
|
|
|
|
|
+ return ctx
|
|
|
|
|
+ }
|
|
|
|
|
+ ctx = ctx.parentContext
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
|
if ctx.parentContext != nil {
|
|
if ctx.parentContext != nil {
|
|
|
ctx = ctx.parentContext
|
|
ctx = ctx.parentContext
|