Implement slightly wonky setup for checking against ...
subcommand names of a default command (should it be set)
This commit is contained in:
parent
32dec1ddaa
commit
77feee843d
36
app.go
36
app.go
@ -344,14 +344,26 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
|
||||
if a.validCommandName(name) {
|
||||
c = a.Command(name)
|
||||
} else {
|
||||
isFlagName := false
|
||||
for _, flagName := range cCtx.FlagNames() {
|
||||
if name == flagName {
|
||||
isFlagName = true
|
||||
break
|
||||
hasDefault := a.DefaultCommand != ""
|
||||
isFlagName := checkStringSliceIncludes(name, cCtx.FlagNames())
|
||||
|
||||
var (
|
||||
isDefaultSubcommand = false
|
||||
defaultHasSubcommands = false
|
||||
)
|
||||
|
||||
if hasDefault {
|
||||
dc := a.Command(a.DefaultCommand)
|
||||
defaultHasSubcommands = len(dc.Subcommands) > 0
|
||||
for _, dcSub := range dc.Subcommands {
|
||||
if checkStringSliceIncludes(name, dcSub.Names()) {
|
||||
isDefaultSubcommand = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if isFlagName {
|
||||
|
||||
if isFlagName || (hasDefault && (defaultHasSubcommands && isDefaultSubcommand)) {
|
||||
argsWithDefault := a.argsWithDefaultCommand(args)
|
||||
if !reflect.DeepEqual(args, argsWithDefault) {
|
||||
c = a.Command(argsWithDefault.First())
|
||||
@ -661,3 +673,15 @@ func HandleAction(action interface{}, cCtx *Context) (err error) {
|
||||
|
||||
return errInvalidActionType
|
||||
}
|
||||
|
||||
func checkStringSliceIncludes(want string, sSlice []string) bool {
|
||||
found := false
|
||||
for _, s := range sSlice {
|
||||
if want == s {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return found
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user