159 lines
3.5 KiB
Go
159 lines
3.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"strings"
|
|
)
|
|
|
|
// 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 {
|
|
App *App
|
|
Command *Command
|
|
|
|
flagSet *flag.FlagSet
|
|
parentContext *Context
|
|
}
|
|
|
|
// 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}
|
|
}
|
|
|
|
// NumFlags returns the number of flags set
|
|
func (c *Context) NumFlags() int {
|
|
return c.flagSet.NFlag()
|
|
}
|
|
|
|
// Set sets a context flag to a value.
|
|
func (c *Context) Set(name, value string) error {
|
|
return c.flagSet.Set(name, value)
|
|
}
|
|
|
|
// IsSet determines if the flag was actually set
|
|
func (c *Context) IsSet(name string) bool {
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
isSet := false
|
|
fs.Visit(func(f *flag.Flag) {
|
|
if f.Name == name {
|
|
isSet = true
|
|
}
|
|
})
|
|
return isSet
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
return names
|
|
}
|
|
|
|
// Lineage returns *this* context and all of its ancestor contexts in order from
|
|
// child to parent
|
|
func (c *Context) Lineage() []*Context {
|
|
lineage := []*Context{}
|
|
|
|
for cur := c; cur != nil; cur = cur.parentContext {
|
|
lineage = append(lineage, cur)
|
|
}
|
|
|
|
return lineage
|
|
}
|
|
|
|
// Args returns the command line arguments associated with the context.
|
|
func (c *Context) Args() Args {
|
|
ret := args(c.flagSet.Args())
|
|
return &ret
|
|
}
|
|
|
|
// NArg returns the number of the command line arguments.
|
|
func (c *Context) NArg() int {
|
|
return c.Args().Len()
|
|
}
|
|
|
|
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
for _, c := range ctx.Lineage() {
|
|
if f := c.flagSet.Lookup(name); f != nil {
|
|
return c.flagSet
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
|
switch ff.Value.(type) {
|
|
case Serializeder:
|
|
set.Set(name, ff.Value.(Serializeder).Serialized())
|
|
default:
|
|
set.Set(name, ff.Value.String())
|
|
}
|
|
}
|
|
|
|
func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|
visited := make(map[string]bool)
|
|
set.Visit(func(f *flag.Flag) {
|
|
visited[f.Name] = true
|
|
})
|
|
for _, f := range flags {
|
|
parts := f.Names()
|
|
if len(parts) == 1 {
|
|
continue
|
|
}
|
|
var ff *flag.Flag
|
|
for _, name := range parts {
|
|
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)
|
|
}
|
|
}
|
|
if ff == nil {
|
|
continue
|
|
}
|
|
for _, name := range parts {
|
|
name = strings.Trim(name, " ")
|
|
if !visited[name] {
|
|
copyFlag(name, ff, set)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
|
|
return func(f *flag.Flag) {
|
|
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 != "" {
|
|
(*names) = append(*names, name)
|
|
}
|
|
}
|
|
}
|