apply fix to subcommands, also

This commit is contained in:
Uwe Krueger 2022-08-07 13:44:30 +02:00
parent 140c5d89f1
commit c236cde1e2
2 changed files with 18 additions and 1 deletions

17
args.go
View file

@ -38,6 +38,23 @@ func legacyArgs(cmd *Command, args []string) error {
return nil 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. // NoArgs returns an error if any args are included.
func NoArgs(cmd *Command, args []string) error { func NoArgs(cmd *Command, args []string) error {
if len(args) > 0 { if len(args) > 0 {

View file

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