Skip to content

Commit 77feee8

Browse files
committed
Implement slightly wonky setup for checking against ...
subcommand names of a default command (should it be set)
1 parent 32dec1d commit 77feee8

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

app.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,26 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
344344
if a.validCommandName(name) {
345345
c = a.Command(name)
346346
} else {
347-
isFlagName := false
348-
for _, flagName := range cCtx.FlagNames() {
349-
if name == flagName {
350-
isFlagName = true
351-
break
347+
hasDefault := a.DefaultCommand != ""
348+
isFlagName := checkStringSliceIncludes(name, cCtx.FlagNames())
349+
350+
var (
351+
isDefaultSubcommand = false
352+
defaultHasSubcommands = false
353+
)
354+
355+
if hasDefault {
356+
dc := a.Command(a.DefaultCommand)
357+
defaultHasSubcommands = len(dc.Subcommands) > 0
358+
for _, dcSub := range dc.Subcommands {
359+
if checkStringSliceIncludes(name, dcSub.Names()) {
360+
isDefaultSubcommand = true
361+
break
362+
}
352363
}
353364
}
354-
if isFlagName {
365+
366+
if isFlagName || (hasDefault && (defaultHasSubcommands && isDefaultSubcommand)) {
355367
argsWithDefault := a.argsWithDefaultCommand(args)
356368
if !reflect.DeepEqual(args, argsWithDefault) {
357369
c = a.Command(argsWithDefault.First())
@@ -661,3 +673,15 @@ func HandleAction(action interface{}, cCtx *Context) (err error) {
661673

662674
return errInvalidActionType
663675
}
676+
677+
func checkStringSliceIncludes(want string, sSlice []string) bool {
678+
found := false
679+
for _, s := range sSlice {
680+
if want == s {
681+
found = true
682+
break
683+
}
684+
}
685+
686+
return found
687+
}

0 commit comments

Comments
 (0)