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"
|
2013-07-18 00:00:02 +00:00
|
|
|
"strconv"
|
2013-11-18 23:37:59 +00:00
|
|
|
"strings"
|
2014-08-02 21:32:32 +00:00
|
|
|
"time"
|
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
|
2016-05-25 16:05:14 +00:00
|
|
|
// can be used to retrieve context-specific args and
|
2013-07-18 00:00:02 +00:00
|
|
|
// parsed command-line options.
|
|
|
|
type Context struct {
|
2016-05-17 07:16:26 +00:00
|
|
|
App *App
|
2016-05-22 19:20:52 +00:00
|
|
|
Command *Command
|
2016-05-17 07:16:26 +00:00
|
|
|
|
|
|
|
flagSet *flag.FlagSet
|
|
|
|
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
|
|
|
// Int looks up the value of a local int flag, returns 0 if no int flag exists
|
2013-07-19 00:29:06 +00:00
|
|
|
func (c *Context) Int(name string) int {
|
2016-05-16 14:18:15 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupInt(name, fs)
|
|
|
|
}
|
|
|
|
return 0
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:41:24 +00:00
|
|
|
// Int64 looks up the value of a local int flag, returns 0 if no int flag exists
|
|
|
|
func (c *Context) Int64(name string) int64 {
|
2016-06-17 13:11:36 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupInt64(name, fs)
|
|
|
|
}
|
|
|
|
return 0
|
2016-06-11 22:41:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:13:32 +00:00
|
|
|
// Uint looks up the value of a local int flag, returns 0 if no int flag exists
|
|
|
|
func (c *Context) Uint(name string) uint {
|
2016-06-17 13:11:36 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupUint(name, fs)
|
|
|
|
}
|
|
|
|
return 0
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64 looks up the value of a local int flag, returns 0 if no int flag exists
|
|
|
|
func (c *Context) Uint64(name string) uint64 {
|
2016-06-17 13:11:36 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupUint64(name, fs)
|
|
|
|
}
|
|
|
|
return 0
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Duration looks up the value of a local time.Duration flag, returns 0 if no
|
|
|
|
// time.Duration flag exists
|
2014-08-02 21:32:32 +00:00
|
|
|
func (c *Context) Duration(name string) time.Duration {
|
2016-05-16 14:18:15 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupDuration(name, fs)
|
|
|
|
}
|
|
|
|
return 0
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Float64 looks up the value of a local float64 flag, returns 0 if no float64
|
|
|
|
// flag exists
|
2013-12-03 13:42:09 +00:00
|
|
|
func (c *Context) Float64(name string) float64 {
|
2016-05-16 14:18:15 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2016-04-28 17:03:25 +00:00
|
|
|
return lookupFloat64(name, fs)
|
|
|
|
}
|
2015-05-18 15:39:48 +00:00
|
|
|
return 0
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 04:54:33 +00:00
|
|
|
// Int64Slice looks up the value of a local int slice flag, returns nil if no int
|
|
|
|
// slice flag exists
|
|
|
|
func (c *Context) Int64Slice(name string) []int64 {
|
2016-06-17 13:11:36 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupInt64Slice(name, fs)
|
2015-05-18 15:39:48 +00:00
|
|
|
}
|
2016-06-17 13:11:36 +00:00
|
|
|
return nil
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// Bool looks up the value of a local bool flag, returns false if no bool flag exists
|
|
|
|
func (c *Context) Bool(name string) bool {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2015-05-18 15:39:48 +00:00
|
|
|
return lookupBool(name, fs)
|
|
|
|
}
|
|
|
|
return false
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// String looks up the value of a local string flag, returns "" if no string flag exists
|
|
|
|
func (c *Context) String(name string) string {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2015-05-18 15:39:48 +00:00
|
|
|
return lookupString(name, fs)
|
|
|
|
}
|
|
|
|
return ""
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// 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 {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2015-05-18 15:39:48 +00:00
|
|
|
return lookupStringSlice(name, fs)
|
|
|
|
}
|
|
|
|
return nil
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// 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 {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2015-05-18 15:39:48 +00:00
|
|
|
return lookupIntSlice(name, fs)
|
|
|
|
}
|
|
|
|
return nil
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// Generic looks up the value of a local generic flag, returns nil if no generic
|
|
|
|
// flag exists
|
|
|
|
func (c *Context) Generic(name string) interface{} {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
2015-05-18 15:39:48 +00:00
|
|
|
return lookupGeneric(name, fs)
|
|
|
|
}
|
|
|
|
return nil
|
2014-04-15 14:16:47 +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-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 {
|
2016-05-16 14:18:15 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
isSet := false
|
2016-05-17 07:16:26 +00:00
|
|
|
fs.Visit(func(f *flag.Flag) {
|
2016-05-16 14:18:15 +00:00
|
|
|
if f.Name == name {
|
|
|
|
isSet = true
|
|
|
|
}
|
2013-12-18 18:09:16 +00:00
|
|
|
})
|
2016-05-16 14:18:15 +00:00
|
|
|
return isSet
|
2013-12-18 18:09:16 +00:00
|
|
|
}
|
2016-05-16 14:18:15 +00:00
|
|
|
return false
|
2013-12-18 18:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// LocalFlagNames returns a slice of flag names used in this context.
|
|
|
|
func (c *Context) LocalFlagNames() []string {
|
|
|
|
names := []string{}
|
|
|
|
c.flagSet.Visit(makeFlagNameVisitor(&names))
|
|
|
|
return names
|
2014-11-11 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// FlagNames returns a slice of flag names used by the this context and all of
|
|
|
|
// its parent contexts.
|
|
|
|
func (c *Context) FlagNames() []string {
|
|
|
|
names := []string{}
|
|
|
|
for _, ctx := range c.Lineage() {
|
|
|
|
ctx.flagSet.Visit(makeFlagNameVisitor(&names))
|
2014-10-02 22:16:29 +00:00
|
|
|
}
|
2016-05-16 14:18:15 +00:00
|
|
|
return names
|
2014-10-02 22:16:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
// Lineage returns *this* context and all of its ancestor contexts in order from
|
|
|
|
// child to parent
|
|
|
|
func (c *Context) Lineage() []*Context {
|
|
|
|
lineage := []*Context{}
|
|
|
|
|
2016-05-17 07:16:26 +00:00
|
|
|
for cur := c; cur != nil; cur = cur.parentContext {
|
2016-05-16 14:18:15 +00:00
|
|
|
lineage = append(lineage, cur)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineage
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Args returns the command line arguments associated with the context.
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *Context) Args() Args {
|
|
|
|
ret := args(c.flagSet.Args())
|
|
|
|
return &ret
|
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 {
|
2016-05-22 19:20:52 +00:00
|
|
|
return c.Args().Len()
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
|
|
for _, c := range ctx.Lineage() {
|
|
|
|
if f := c.flagSet.Lookup(name); f != nil {
|
|
|
|
return c.flagSet
|
2015-05-18 15:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:18:15 +00:00
|
|
|
|
2015-05-18 15:39:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func lookupInt(name string, set *flag.FlagSet) int {
|
2013-07-19 01:00:52 +00:00
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
2016-06-16 15:13:32 +00:00
|
|
|
val, err := strconv.ParseInt(f.Value.String(), 0, 64)
|
2013-07-18 00:00:02 +00:00
|
|
|
if err != nil {
|
2013-07-19 01:00:52 +00:00
|
|
|
return 0
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2016-06-16 15:13:32 +00:00
|
|
|
return int(val)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
2013-07-22 13:35:56 +00:00
|
|
|
return 0
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:41:24 +00:00
|
|
|
func lookupInt64(name string, set *flag.FlagSet) int64 {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
2016-06-16 15:13:32 +00:00
|
|
|
val, err := strconv.ParseInt(f.Value.String(), 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupUint(name string, set *flag.FlagSet) uint {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := strconv.ParseUint(f.Value.String(), 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return uint(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupUint64(name string, set *flag.FlagSet) uint64 {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := strconv.ParseUint(f.Value.String(), 0, 64)
|
2013-07-18 00:00:02 +00:00
|
|
|
if err != nil {
|
2013-07-19 01:00:52 +00:00
|
|
|
return 0
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
2013-07-22 13:35:56 +00:00
|
|
|
return 0
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 21:32:32 +00:00
|
|
|
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := time.ParseDuration(f.Value.String())
|
|
|
|
if err == nil {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
func lookupFloat64(name string, set *flag.FlagSet) float64 {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := strconv.ParseFloat(f.Value.String(), 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func lookupString(name string, set *flag.FlagSet) string {
|
2013-07-19 01:00:52 +00:00
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
return f.Value.String()
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
2013-07-22 13:35:56 +00:00
|
|
|
return ""
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func lookupStringSlice(name string, set *flag.FlagSet) []string {
|
2013-09-24 01:41:31 +00:00
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
2013-09-24 19:36:01 +00:00
|
|
|
return (f.Value.(*StringSlice)).Value()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func lookupIntSlice(name string, set *flag.FlagSet) []int {
|
2013-09-24 19:36:01 +00:00
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
return (f.Value.(*IntSlice)).Value()
|
|
|
|
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 04:54:33 +00:00
|
|
|
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
return (f.Value.(*Int64Slice)).Value()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
2014-04-15 14:57:11 +00:00
|
|
|
return f.Value
|
2014-04-15 14:16:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func lookupBool(name string, set *flag.FlagSet) bool {
|
2013-07-19 01:00:52 +00:00
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := strconv.ParseBool(f.Value.String())
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
2013-07-22 13:35:56 +00:00
|
|
|
return false
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
|
2014-03-19 02:26:57 +00:00
|
|
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
|
|
|
switch ff.Value.(type) {
|
2016-04-29 06:30:49 +00:00
|
|
|
case Serializeder:
|
|
|
|
set.Set(name, ff.Value.(Serializeder).Serialized())
|
2014-03-19 02:26:57 +00:00
|
|
|
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 {
|
2016-05-22 01:29:45 +00:00
|
|
|
parts := f.Names()
|
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
|
|
|
}
|
2016-05-16 14:18:15 +00:00
|
|
|
|
|
|
|
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
|
|
|
|
return func(f *flag.Flag) {
|
2016-05-17 07:46:16 +00:00
|
|
|
nameParts := strings.Split(f.Name, ",")
|
|
|
|
name := strings.TrimSpace(nameParts[0])
|
|
|
|
|
|
|
|
for _, part := range nameParts {
|
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
if len(part) > len(name) {
|
|
|
|
name = part
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if name != "" {
|
2016-05-16 14:18:15 +00:00
|
|
|
(*names) = append(*names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|