Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)
Fix #277 Fix #467
This commit is contained in:
committed by
Albert Nigmatzianov
parent
419e0f8d51
commit
83b1f03962
119
command.go
119
command.go
@ -125,6 +125,9 @@ type Command struct {
|
||||
// Must be > 0.
|
||||
SuggestionsMinimumDistance int
|
||||
|
||||
// TraverseChildren parses flags on all parents before executing child command.
|
||||
TraverseChildren bool
|
||||
|
||||
// name is the command name, usually the executable's name.
|
||||
name string
|
||||
// commands is the list of commands supported by this program.
|
||||
@ -475,13 +478,14 @@ func argsMinusFirstX(args []string, x string) []string {
|
||||
return args
|
||||
}
|
||||
|
||||
func isFlagArg(arg string) bool {
|
||||
return ((len(arg) >= 3 && arg[1] == '-') ||
|
||||
(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
|
||||
}
|
||||
|
||||
// Find the target command given the args and command tree
|
||||
// Meant to be run on the highest node. Only searches down.
|
||||
func (c *Command) Find(args []string) (*Command, []string, error) {
|
||||
if c == nil {
|
||||
return nil, nil, fmt.Errorf("Called find() on a nil Command")
|
||||
}
|
||||
|
||||
var innerfind func(*Command, []string) (*Command, []string)
|
||||
|
||||
innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
|
||||
@ -490,28 +494,11 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
||||
return c, innerArgs
|
||||
}
|
||||
nextSubCmd := argsWOflags[0]
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
||||
}
|
||||
if EnablePrefixMatching {
|
||||
if strings.HasPrefix(cmd.Name(), nextSubCmd) { // prefix match
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
for _, x := range cmd.Aliases {
|
||||
if strings.HasPrefix(x, nextSubCmd) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// only accept a single prefix match - multiple matches would be ambiguous
|
||||
if len(matches) == 1 {
|
||||
return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
|
||||
cmd := c.findNext(nextSubCmd)
|
||||
if cmd != nil {
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
||||
}
|
||||
|
||||
return c, innerArgs
|
||||
}
|
||||
|
||||
@ -539,6 +526,66 @@ func (c *Command) findSuggestions(arg string) string {
|
||||
return suggestionsString
|
||||
}
|
||||
|
||||
func (c *Command) findNext(next string) *Command {
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == next || cmd.HasAlias(next) {
|
||||
return cmd
|
||||
}
|
||||
if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
if len(matches) == 1 {
|
||||
return matches[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse the command tree to find the command, and parse args for
|
||||
// each parent.
|
||||
func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
||||
flags := []string{}
|
||||
inFlag := false
|
||||
|
||||
for i, arg := range args {
|
||||
switch {
|
||||
// A long flag with a space separated value
|
||||
case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
|
||||
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
||||
inFlag = !hasNoOptDefVal(arg[2:], c.Flags())
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// A short flag with a space separated value
|
||||
case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
|
||||
inFlag = true
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// The value for a flag
|
||||
case inFlag:
|
||||
inFlag = false
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// A flag without a value, or with an `=` separated value
|
||||
case isFlagArg(arg):
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
}
|
||||
|
||||
cmd := c.findNext(arg)
|
||||
if cmd == nil {
|
||||
return c, args, nil
|
||||
}
|
||||
|
||||
if err := c.ParseFlags(flags); err != nil {
|
||||
return nil, args, err
|
||||
}
|
||||
return cmd.Traverse(args[i+1:])
|
||||
}
|
||||
return c, args, nil
|
||||
}
|
||||
|
||||
// SuggestionsFor provides suggestions for the typedName.
|
||||
func (c *Command) SuggestionsFor(typedName string) []string {
|
||||
suggestions := []string{}
|
||||
@ -714,7 +761,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
args = c.args
|
||||
}
|
||||
|
||||
cmd, flags, err := c.Find(args)
|
||||
var flags []string
|
||||
if c.TraverseChildren {
|
||||
cmd, flags, err = c.Traverse(args)
|
||||
} else {
|
||||
cmd, flags, err = c.Find(args)
|
||||
}
|
||||
if err != nil {
|
||||
// If found parse to a subcommand and then failed, talk about the subcommand
|
||||
if cmd != nil {
|
||||
@ -726,6 +778,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// Always show help if requested, even if SilenceErrors is in
|
||||
@ -993,7 +1046,21 @@ func (c *Command) HasAlias(s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// NameAndAliases returns string containing name and all aliases
|
||||
// hasNameOrAliasPrefix returns true if the Name or any of aliases start
|
||||
// with prefix
|
||||
func (c *Command) hasNameOrAliasPrefix(prefix string) bool {
|
||||
if strings.HasPrefix(c.Name(), prefix) {
|
||||
return true
|
||||
}
|
||||
for _, alias := range c.Aliases {
|
||||
if strings.HasPrefix(alias, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NameAndAliases returns a list of the command name and all aliases
|
||||
func (c *Command) NameAndAliases() string {
|
||||
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
|
||||
}
|
||||
|
Reference in New Issue
Block a user