2013-07-18 00:00:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2013-11-20 08:05:18 +00:00
|
|
|
"errors"
|
2013-11-21 01:25:13 +00:00
|
|
|
"flag"
|
2016-07-31 21:14:46 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
2013-11-18 23:37:59 +00:00
|
|
|
"strings"
|
2013-07-18 00:00:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Context is a type that is passed through to
|
|
|
|
// each Handler action in a cli application. Context
|
|
|
|
// can be used to retrieve context-specific Args and
|
|
|
|
// parsed command-line options.
|
|
|
|
type Context struct {
|
2016-07-31 21:14:46 +00:00
|
|
|
App *App
|
|
|
|
Command Command
|
|
|
|
flagSet *flag.FlagSet
|
|
|
|
setFlags map[string]bool
|
|
|
|
parentContext *Context
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// NewContext creates a new context. For use in when invoking an App or Command action.
|
2015-05-18 15:39:48 +00:00
|
|
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
|
|
|
return &Context{App: app, flagSet: set, parentContext: parentCtx}
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// NumFlags returns the number of flags set
|
2015-02-26 21:51:01 +00:00
|
|
|
func (c *Context) NumFlags() int {
|
|
|
|
return c.flagSet.NFlag()
|
|
|
|
}
|
|
|
|
|
2015-06-08 20:21:26 +00:00
|
|
|
// Set sets a context flag to a value.
|
|
|
|
func (c *Context) Set(name, value string) error {
|
|
|
|
return c.flagSet.Set(name, value)
|
|
|
|
}
|
|
|
|
|
2016-04-30 15:46:47 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// IsSet determines if the flag was actually set
|
2013-12-18 18:09:16 +00:00
|
|
|
func (c *Context) IsSet(name string) bool {
|
|
|
|
if c.setFlags == nil {
|
|
|
|
c.setFlags = make(map[string]bool)
|
2016-07-31 21:14:46 +00:00
|
|
|
|
2013-12-18 18:09:16 +00:00
|
|
|
c.flagSet.Visit(func(f *flag.Flag) {
|
|
|
|
c.setFlags[f.Name] = true
|
|
|
|
})
|
2016-07-31 21:14:46 +00:00
|
|
|
|
|
|
|
c.flagSet.VisitAll(func(f *flag.Flag) {
|
|
|
|
if _, ok := c.setFlags[f.Name]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.setFlags[f.Name] = false
|
|
|
|
})
|
|
|
|
|
|
|
|
// XXX hack to support IsSet for flags with EnvVar
|
|
|
|
//
|
|
|
|
// There isn't an easy way to do this with the current implementation since
|
|
|
|
// whether a flag was set via an environment variable is very difficult to
|
|
|
|
// determine here. Instead, we intend to introduce a backwards incompatible
|
|
|
|
// change in version 2 to add `IsSet` to the Flag interface to push the
|
|
|
|
// responsibility closer to where the information required to determine
|
|
|
|
// whether a flag is set by non-standard means such as environment
|
|
|
|
// variables is avaliable.
|
|
|
|
//
|
|
|
|
// See https://github.com/urfave/cli/issues/294 for additional discussion
|
|
|
|
flags := c.Command.Flags
|
|
|
|
if c.Command.Name == "" { // cannot == Command{} since it contains slice types
|
|
|
|
if c.App != nil {
|
|
|
|
flags = c.App.Flags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range flags {
|
|
|
|
eachName(f.GetName(), func(name string) {
|
|
|
|
if isSet, ok := c.setFlags[name]; isSet || !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:09:57 +00:00
|
|
|
envVarValue := reflect.ValueOf(f).FieldByName("EnvVar")
|
|
|
|
if !envVarValue.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
2016-07-31 21:14:46 +00:00
|
|
|
|
2016-08-01 03:09:57 +00:00
|
|
|
eachName(envVarValue.String(), func(envVar string) {
|
2016-07-31 21:14:46 +00:00
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
c.setFlags[name] = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2013-12-18 18:09:16 +00:00
|
|
|
}
|
2016-07-31 21:14:46 +00:00
|
|
|
|
|
|
|
return c.setFlags[name]
|
2013-12-18 18:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// GlobalIsSet determines if the global flag was actually set
|
2014-11-11 20:48:07 +00:00
|
|
|
func (c *Context) GlobalIsSet(name string) bool {
|
2016-07-31 21:14:46 +00:00
|
|
|
ctx := c
|
|
|
|
if ctx.parentContext != nil {
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
|
|
|
|
for ; ctx != nil; ctx = ctx.parentContext {
|
|
|
|
if ctx.IsSet(name) {
|
|
|
|
return true
|
2015-05-18 15:39:48 +00:00
|
|
|
}
|
2014-11-11 20:48:07 +00:00
|
|
|
}
|
2016-07-31 21:14:46 +00:00
|
|
|
return false
|
2014-11-11 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// FlagNames returns a slice of flag names used in this context.
|
2014-06-14 03:09:28 +00:00
|
|
|
func (c *Context) FlagNames() (names []string) {
|
|
|
|
for _, flag := range c.Command.Flags {
|
2015-12-15 17:29:03 +00:00
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
2014-06-14 03:09:28 +00:00
|
|
|
if name == "help" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// GlobalFlagNames returns a slice of global flag names used by the app.
|
2014-10-02 22:16:29 +00:00
|
|
|
func (c *Context) GlobalFlagNames() (names []string) {
|
|
|
|
for _, flag := range c.App.Flags {
|
2015-12-15 17:29:03 +00:00
|
|
|
name := strings.Split(flag.GetName(), ",")[0]
|
2014-10-02 22:16:29 +00:00
|
|
|
if name == "help" || name == "version" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Parent returns the parent context, if any
|
2015-06-19 21:06:04 +00:00
|
|
|
func (c *Context) Parent() *Context {
|
|
|
|
return c.parentContext
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Args contains apps console arguments
|
2013-11-28 15:53:50 +00:00
|
|
|
type Args []string
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Args returns the command line arguments associated with the context.
|
2013-11-24 13:40:21 +00:00
|
|
|
func (c *Context) Args() Args {
|
|
|
|
args := Args(c.flagSet.Args())
|
|
|
|
return args
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// NArg returns the number of the command line arguments.
|
2016-02-21 13:57:11 +00:00
|
|
|
func (c *Context) NArg() int {
|
|
|
|
return len(c.Args())
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Get returns the nth argument, or else a blank string
|
2013-11-28 15:53:50 +00:00
|
|
|
func (a Args) Get(n int) string {
|
|
|
|
if len(a) > n {
|
|
|
|
return a[n]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// First returns the first argument, or else a blank string
|
2013-11-28 15:53:50 +00:00
|
|
|
func (a Args) First() string {
|
|
|
|
return a.Get(0)
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Tail returns the rest of the arguments (not the first one)
|
2013-11-28 15:53:50 +00:00
|
|
|
// or else an empty string slice
|
|
|
|
func (a Args) Tail() []string {
|
|
|
|
if len(a) >= 2 {
|
|
|
|
return []string(a)[1:]
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Present checks if there are any arguments present
|
2013-11-28 15:53:50 +00:00
|
|
|
func (a Args) Present() bool {
|
|
|
|
return len(a) != 0
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Swap swaps arguments at the given indexes
|
2014-07-13 14:18:45 +00:00
|
|
|
func (a Args) Swap(from, to int) error {
|
|
|
|
if from >= len(a) || to >= len(a) {
|
|
|
|
return errors.New("index out of range")
|
|
|
|
}
|
|
|
|
a[from], a[to] = a[to], a[from]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-30 15:46:47 +00:00
|
|
|
func globalContext(ctx *Context) *Context {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if ctx.parentContext == nil {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:20:27 +00:00
|
|
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
|
|
if ctx.parentContext != nil {
|
|
|
|
ctx = ctx.parentContext
|
|
|
|
}
|
|
|
|
for ; ctx != nil; ctx = ctx.parentContext {
|
2015-05-18 15:39:48 +00:00
|
|
|
if f := ctx.flagSet.Lookup(name); f != nil {
|
|
|
|
return ctx.flagSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:26:57 +00:00
|
|
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
|
|
|
switch ff.Value.(type) {
|
|
|
|
case *StringSlice:
|
|
|
|
default:
|
|
|
|
set.Set(name, ff.Value.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|
|
|
visited := make(map[string]bool)
|
|
|
|
set.Visit(func(f *flag.Flag) {
|
|
|
|
visited[f.Name] = true
|
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
for _, f := range flags {
|
2015-12-15 17:29:03 +00:00
|
|
|
parts := strings.Split(f.GetName(), ",")
|
2013-11-18 23:35:23 +00:00
|
|
|
if len(parts) == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ff *flag.Flag
|
|
|
|
for _, name := range parts {
|
2013-11-20 08:05:18 +00:00
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
if visited[name] {
|
|
|
|
if ff != nil {
|
|
|
|
return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name)
|
|
|
|
}
|
|
|
|
ff = set.Lookup(name)
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ff == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, name := range parts {
|
2013-11-20 08:05:18 +00:00
|
|
|
name = strings.Trim(name, " ")
|
2014-03-19 02:26:57 +00:00
|
|
|
if !visited[name] {
|
|
|
|
copyFlag(name, ff, set)
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-20 08:05:18 +00:00
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|