1
0
Fork 0
mirror of https://github.com/spf13/cobra synced 2025-04-04 22:09:11 +00:00
This commit is contained in:
Uwe Krueger 2025-03-21 21:52:25 -04:00 committed by GitHub
commit bbaf2495fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

17
args.go
View file

@ -38,6 +38,23 @@ func legacyArgs(cmd *Command, args []string) error {
return nil
}
// Legacy sub command arg validation has the following behaviour:
// - commands with no subcommands can take arbitrary arguments
// - commands with subcommands will do subcommand validity checking
// - subcommands will always accept arbitrary arguments
func legacySubCommandArgs(cmd *Command, args []string) error {
// no subcommand, always take args
if !cmd.HasSubCommands() {
return nil
}
// command with subcommands, do subcommand checking.
if len(args) > 0 {
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
}
return nil
}
// NoArgs returns an error if any args are included.
func NoArgs(cmd *Command, args []string) error {
if len(args) > 0 {

View file

@ -848,7 +848,10 @@ func (c *Command) Traverse(args []string) (*Command, []string, error) {
cmd := c.findNext(arg)
if cmd == nil {
return c, args, nil
if c.Runnable() {
return c, args, nil
}
return c, args, legacySubCommandArgs(c, args)
}
if err := c.ParseFlags(flags); err != nil {