2021-01-31 23:10:16 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
2022-10-01 14:20:01 +00:00
|
|
|
"fmt"
|
2021-01-31 23:10:16 +00:00
|
|
|
"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 {
|
|
|
|
context.Context
|
|
|
|
App *App
|
|
|
|
Command *Command
|
|
|
|
shellComplete bool
|
|
|
|
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 {
|
|
|
|
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
|
|
|
if parentCtx != nil {
|
|
|
|
c.Context = parentCtx.Context
|
|
|
|
c.shellComplete = parentCtx.shellComplete
|
|
|
|
if parentCtx.flagSet == nil {
|
|
|
|
parentCtx.flagSet = &flag.FlagSet{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Command = &Command{}
|
|
|
|
|
|
|
|
if c.Context == nil {
|
|
|
|
c.Context = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumFlags returns the number of flags set
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) NumFlags() int {
|
|
|
|
return cCtx.flagSet.NFlag()
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a context flag to a value.
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) Set(name, value string) error {
|
2022-10-01 14:20:01 +00:00
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
|
|
|
return fs.Set(name, value)
|
2022-09-02 18:42:41 +00:00
|
|
|
}
|
2022-10-01 14:20:01 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("no such flag -%s", name)
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet determines if the flag was actually set
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) IsSet(name string) bool {
|
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
2022-03-16 11:55:03 +00:00
|
|
|
isSet := false
|
|
|
|
fs.Visit(func(f *flag.Flag) {
|
|
|
|
if f.Name == name {
|
|
|
|
isSet = true
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
2022-03-16 11:55:03 +00:00
|
|
|
})
|
|
|
|
if isSet {
|
|
|
|
return true
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
f := cCtx.lookupFlag(name)
|
2021-01-31 23:10:16 +00:00
|
|
|
if f == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.IsSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalFlagNames returns a slice of flag names used in this context.
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) LocalFlagNames() []string {
|
2021-01-31 23:10:16 +00:00
|
|
|
var names []string
|
2022-04-26 12:24:23 +00:00
|
|
|
cCtx.flagSet.Visit(makeFlagNameVisitor(&names))
|
2021-01-31 23:10:16 +00:00
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagNames returns a slice of flag names used by the this context and all of
|
|
|
|
// its parent contexts.
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) FlagNames() []string {
|
2021-01-31 23:10:16 +00:00
|
|
|
var names []string
|
2022-04-26 12:24:23 +00:00
|
|
|
for _, pCtx := range cCtx.Lineage() {
|
|
|
|
pCtx.flagSet.Visit(makeFlagNameVisitor(&names))
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lineage returns *this* context and all of its ancestor contexts in order from
|
|
|
|
// child to parent
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) Lineage() []*Context {
|
2021-01-31 23:10:16 +00:00
|
|
|
var lineage []*Context
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
for cur := cCtx; cur != nil; cur = cur.parentContext {
|
2021-01-31 23:10:16 +00:00
|
|
|
lineage = append(lineage, cur)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineage
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:44:44 +00:00
|
|
|
// Count returns the num of occurences of this flag
|
|
|
|
func (cCtx *Context) Count(name string) int {
|
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
|
|
|
if cf, ok := fs.Lookup(name).Value.(Countable); ok {
|
|
|
|
return cf.Count()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:10:16 +00:00
|
|
|
// Value returns the value of the flag corresponding to `name`
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) Value(name string) interface{} {
|
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
2022-03-16 11:55:03 +00:00
|
|
|
return fs.Lookup(name).Value.(flag.Getter).Get()
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Args returns the command line arguments associated with the context.
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) Args() Args {
|
|
|
|
ret := args(cCtx.flagSet.Args())
|
2021-01-31 23:10:16 +00:00
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// NArg returns the number of the command line arguments.
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) NArg() int {
|
|
|
|
return cCtx.Args().Len()
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) lookupFlag(name string) Flag {
|
|
|
|
for _, c := range cCtx.Lineage() {
|
2021-01-31 23:10:16 +00:00
|
|
|
if c.Command == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range c.Command.Flags {
|
|
|
|
for _, n := range f.Names() {
|
|
|
|
if n == name {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
if cCtx.App != nil {
|
|
|
|
for _, f := range cCtx.App.Flags {
|
2021-01-31 23:10:16 +00:00
|
|
|
for _, n := range f.Names() {
|
|
|
|
if n == name {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) lookupFlagSet(name string) *flag.FlagSet {
|
|
|
|
for _, c := range cCtx.Lineage() {
|
2022-03-16 11:55:03 +00:00
|
|
|
if c.flagSet == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-31 23:10:16 +00:00
|
|
|
if f := c.flagSet.Lookup(name); f != nil {
|
|
|
|
return c.flagSet
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 18:42:41 +00:00
|
|
|
cCtx.onInvalidFlag(name)
|
2021-01-31 23:10:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
|
2021-01-31 23:10:16 +00:00
|
|
|
var missingFlags []string
|
|
|
|
for _, f := range flags {
|
|
|
|
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
|
|
|
|
var flagPresent bool
|
|
|
|
var flagName string
|
|
|
|
|
|
|
|
for _, key := range f.Names() {
|
2022-09-02 18:42:41 +00:00
|
|
|
flagName = key
|
2021-01-31 23:10:16 +00:00
|
|
|
|
2022-04-26 12:24:23 +00:00
|
|
|
if cCtx.IsSet(strings.TrimSpace(key)) {
|
2021-01-31 23:10:16 +00:00
|
|
|
flagPresent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !flagPresent && flagName != "" {
|
|
|
|
missingFlags = append(missingFlags, flagName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missingFlags) != 0 {
|
|
|
|
return &errRequiredFlags{missingFlags: missingFlags}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-16 11:55:03 +00:00
|
|
|
|
2022-09-02 18:42:41 +00:00
|
|
|
func (cCtx *Context) onInvalidFlag(name string) {
|
|
|
|
for cCtx != nil {
|
|
|
|
if cCtx.App != nil && cCtx.App.InvalidFlagAccessHandler != nil {
|
|
|
|
cCtx.App.InvalidFlagAccessHandler(cCtx, name)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cCtx = cCtx.parentContext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 11:55:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|