Skip to content

Commit

Permalink
Implement slightly wonky setup for checking against ...
Browse files Browse the repository at this point in the history
subcommand names of a default command (should it be set)
  • Loading branch information
jalavosus committed Jun 21, 2022
1 parent 32dec1d commit 77feee8
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions app.go
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}

0 comments on commit 77feee8

Please sign in to comment.