mirror of
https://github.com/spf13/cobra
synced 2025-05-05 04:47:22 +00:00
Merge f5ce802946
into fce8d8aeb0
This commit is contained in:
commit
9d52fd05e3
2 changed files with 9 additions and 10 deletions
7
args.go
7
args.go
|
@ -23,16 +23,15 @@ type PositionalArgs func(cmd *Command, args []string) error
|
|||
|
||||
// Legacy arg validation has the following behaviour:
|
||||
// - root commands with no subcommands can take arbitrary arguments
|
||||
// - root commands with subcommands will do subcommand validity checking
|
||||
// - subcommands will always accept arbitrary arguments
|
||||
// - commands with subcommands cannot take any arguments
|
||||
func legacyArgs(cmd *Command, args []string) error {
|
||||
// no subcommand, always take args
|
||||
if !cmd.HasSubCommands() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// root command with subcommands, do subcommand checking.
|
||||
if !cmd.HasParent() && len(args) > 0 {
|
||||
// root command with subcommands, the args should be empty.
|
||||
if cmd.HasSubCommands() && len(args) > 0 {
|
||||
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
||||
}
|
||||
return nil
|
||||
|
|
12
command.go
12
command.go
|
@ -828,12 +828,6 @@ func (c *Command) execute(a []string) (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
if !c.Runnable() {
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
||||
c.preRun()
|
||||
|
||||
argWoFlags := c.Flags().Args()
|
||||
if c.DisableFlagParsing {
|
||||
argWoFlags = a
|
||||
|
@ -843,6 +837,12 @@ func (c *Command) execute(a []string) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
if !c.Runnable() {
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
||||
c.preRun()
|
||||
|
||||
for p := c; p != nil; p = p.Parent() {
|
||||
if p.PersistentPreRunE != nil {
|
||||
if err := p.PersistentPreRunE(c, argWoFlags); err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue