-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
closes #2130 new command flag ErrorOnUnknownSubcommand #2167
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,10 @@ type Group struct { | |
Title string | ||
} | ||
|
||
// ErrUnknownSubcommand is returned by Command.Execute() when the subcommand was not found. | ||
// Hereto, the ErrorOnUnknownSubcommand flag must be set true. | ||
var ErrUnknownSubcommand = errors.New("subcommand is unknown") | ||
|
||
// Command is just that, a command for your application. | ||
// E.g. 'go run ...' - 'run' is the command. Cobra requires | ||
// you to define the usage and description as part of your command | ||
|
@@ -254,6 +258,17 @@ type Command struct { | |
// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. | ||
// Must be > 0. | ||
SuggestionsMinimumDistance int | ||
|
||
// ErrorOnUnknownSubcommand controls the behavior of subcommand handling. | ||
// When the flag is set true the behavior of Command.Execute() will change: | ||
// If a sub-subcommand is not found ErrUnknownSubcommand will be returned on calling | ||
// Command.Exec() instead of the old behavior where a nil error was sent. | ||
// If the flag is false (default) the old behavior is performed. | ||
// For this behavior the child subcommand must be nil. | ||
// Example: in root/service/run - there would be an existing subcommand `run` | ||
// in root/service/unknown - there would be an unknown subcommand `unknown` therefore returning an error | ||
// `service` must have a nil Command.Run() function for this. | ||
ErrorOnUnknownSubcommand bool | ||
} | ||
|
||
// Context returns underlying command context. If command was executed | ||
|
@@ -923,9 +938,12 @@ func (c *Command) execute(a []string) (err error) { | |
} | ||
|
||
if !c.Runnable() { | ||
return flag.ErrHelp | ||
if c.ErrorOnUnknownSubcommand { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you feel this feature should be enabled per command or globally? If globally, then you can add a global variable in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! You are right. I think the global behavior is more what I (and probably others) want! I will change that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @marckhouzam. Did you have a chance to review my changes? Would the behavior be so ok? |
||
return ErrUnknownSubcommand | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only return the error if there is at least one arg. You should be able to move up the following lines to get the number of args:
|
||
} else { | ||
return flag.ErrHelp | ||
} | ||
} | ||
|
||
c.preRun() | ||
|
||
defer c.postRun() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's format this command message like the one in
legacyArgs()
to have a consistent behaviour.fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))