mirror of
https://github.com/spf13/cobra
synced 2024-11-16 10:47:09 +00:00
fix(args): Correct legacyArgs
logical errors
Close #1156 Signed-off-by: Dong Gang <dong.gang@daocloud.io>
This commit is contained in:
parent
675ae5f5a9
commit
01ce53c87a
1 changed files with 3 additions and 4 deletions
7
args.go
7
args.go
|
@ -9,16 +9,15 @@ type PositionalArgs func(cmd *Command, args []string) error
|
||||||
|
|
||||||
// Legacy arg validation has the following behaviour:
|
// Legacy arg validation has the following behaviour:
|
||||||
// - root commands with no subcommands can take arbitrary arguments
|
// - root commands with no subcommands can take arbitrary arguments
|
||||||
// - root commands with subcommands will do subcommand validity checking
|
// - commands with subcommands cannot take any arguments
|
||||||
// - subcommands will always accept arbitrary arguments
|
|
||||||
func legacyArgs(cmd *Command, args []string) error {
|
func legacyArgs(cmd *Command, args []string) error {
|
||||||
// no subcommand, always take args
|
// no subcommand, always take args
|
||||||
if !cmd.HasSubCommands() {
|
if !cmd.HasSubCommands() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// root command with subcommands, do subcommand checking.
|
// root command with subcommands, the args should be empty.
|
||||||
if !cmd.HasParent() && len(args) > 0 {
|
if cmd.HasSubCommands() && len(args) > 0 {
|
||||||
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue