Implement slightly wonky setup for checking against ...

subcommand names of a default command (should it be set)
This commit is contained in:
James Alavosus 2022-05-06 00:06:05 -04:00
parent 32dec1ddaa
commit 77feee843d
No known key found for this signature in database
GPG Key ID: 1B900ABF298E2A89

36
app.go
View File

@ -344,14 +344,26 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
if a.validCommandName(name) { if a.validCommandName(name) {
c = a.Command(name) c = a.Command(name)
} else { } else {
isFlagName := false hasDefault := a.DefaultCommand != ""
for _, flagName := range cCtx.FlagNames() { isFlagName := checkStringSliceIncludes(name, cCtx.FlagNames())
if name == flagName {
isFlagName = true var (
break 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) argsWithDefault := a.argsWithDefaultCommand(args)
if !reflect.DeepEqual(args, argsWithDefault) { if !reflect.DeepEqual(args, argsWithDefault) {
c = a.Command(argsWithDefault.First()) c = a.Command(argsWithDefault.First())
@ -661,3 +673,15 @@ func HandleAction(action interface{}, cCtx *Context) (err error) {
return errInvalidActionType return errInvalidActionType
} }
func checkStringSliceIncludes(want string, sSlice []string) bool {
found := false
for _, s := range sSlice {
if want == s {
found = true
break
}
}
return found
}