@@ -344,14 +344,26 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
344
344
if a .validCommandName (name ) {
345
345
c = a .Command (name )
346
346
} 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
+ }
352
363
}
353
364
}
354
- if isFlagName {
365
+
366
+ if isFlagName || (hasDefault && (defaultHasSubcommands && isDefaultSubcommand )) {
355
367
argsWithDefault := a .argsWithDefaultCommand (args )
356
368
if ! reflect .DeepEqual (args , argsWithDefault ) {
357
369
c = a .Command (argsWithDefault .First ())
@@ -661,3 +673,15 @@ func HandleAction(action interface{}, cCtx *Context) (err error) {
661
673
662
674
return errInvalidActionType
663
675
}
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