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"
|
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 {
|
2013-07-19 15:34:01 +00:00
|
|
|
App *App
|
2013-07-19 01:00:52 +00:00
|
|
|
flagSet *flag.FlagSet
|
|
|
|
globalSet *flag.FlagSet
|
2013-12-18 18:09:16 +00:00
|
|
|
setFlags map[string]bool
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Creates a new context. For use in when invoking an App or Command action.
|
2013-07-19 15:34:01 +00:00
|
|
|
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
2013-12-18 18:09:16 +00:00
|
|
|
return &Context{app, set, globalSet, nil}
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 01:00:52 +00:00
|
|
|
// 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 {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupInt(name, c.flagSet)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 14:32:54 +00:00
|
|
|
// 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 {
|
|
|
|
return lookupFloat64(name, c.flagSet)
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:00:52 +00:00
|
|
|
// Looks up the value of a local bool flag, returns false if no bool flag exists
|
2013-07-19 00:29:06 +00:00
|
|
|
func (c *Context) Bool(name string) bool {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupBool(name, c.flagSet)
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:00:52 +00:00
|
|
|
// Looks up the value of a local string flag, returns "" if no string flag exists
|
|
|
|
func (c *Context) String(name string) string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupString(name, c.flagSet)
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:36:01 +00:00
|
|
|
// 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 {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupStringSlice(name, c.flagSet)
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:36:01 +00:00
|
|
|
// 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 {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupIntSlice(name, c.flagSet)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 01:00:52 +00:00
|
|
|
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
|
|
|
func (c *Context) GlobalInt(name string) int {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupInt(name, c.globalSet)
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
|
|
|
func (c *Context) GlobalBool(name string) bool {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupBool(name, c.globalSet)
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Looks up the value of a global string flag, returns "" if no string flag exists
|
|
|
|
func (c *Context) GlobalString(name string) string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupString(name, c.globalSet)
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:36:01 +00:00
|
|
|
// 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 {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupStringSlice(name, c.globalSet)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2013-11-18 23:35:23 +00:00
|
|
|
return lookupIntSlice(name, c.globalSet)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 18:09:16 +00:00
|
|
|
// Determines if the flag was actually set exists
|
|
|
|
func (c *Context) IsSet(name string) bool {
|
|
|
|
if c.setFlags == nil {
|
|
|
|
c.setFlags = make(map[string]bool)
|
|
|
|
c.flagSet.Visit(func(f *flag.Flag) {
|
|
|
|
c.setFlags[f.Name] = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return c.setFlags[name] == true
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:53:50 +00:00
|
|
|
type Args []string
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-11-28 15:53:50 +00:00
|
|
|
// Returns the nth argument, or else a blank string
|
|
|
|
func (a Args) Get(n int) string {
|
|
|
|
if len(a) > n {
|
|
|
|
return a[n]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
// or else an empty string slice
|
|
|
|
func (a Args) Tail() []string {
|
|
|
|
if len(a) >= 2 {
|
|
|
|
return []string(a)[1:]
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if there are any arguments present
|
|
|
|
func (a Args) Present() bool {
|
|
|
|
return len(a) != 0
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
val, err := strconv.Atoi(f.Value.String())
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
func lookupBoolT(name string, set *flag.FlagSet) bool {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
val, err := strconv.ParseBool(f.Value.String())
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-11-21 01:24:37 +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
|
|
|
}
|