mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
apply fix to subcommands, also
This commit is contained in:
parent
140c5d89f1
commit
c236cde1e2
2 changed files with 18 additions and 1 deletions
17
args.go
17
args.go
|
@ -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 {
|
||||
|
|
|
@ -769,7 +769,7 @@ func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
|||
if c.Runnable() {
|
||||
return c, args, nil
|
||||
}
|
||||
return c, args, legacyArgs(c, args)
|
||||
return c, args, legacySubCommandArgs(c, args)
|
||||
}
|
||||
|
||||
if err := c.ParseFlags(flags); err != nil {
|
||||
|
|
Loading…
Reference in a new issue