mirror of
https://github.com/spf13/cobra
synced 2025-04-04 22:09:11 +00:00
Merge c236cde1e2
into ceb39aba25
This commit is contained in:
commit
bbaf2495fd
2 changed files with 21 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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue