mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
prevent incorrect flag message on bad subcommand parse
This commit is contained in:
parent
79bd93d369
commit
fddac5a640
2 changed files with 40 additions and 7 deletions
|
@ -417,6 +417,33 @@ func TestTrailingCommandFlags(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidSubCommandFlags(t *testing.T) {
|
||||||
|
sub := &Command{
|
||||||
|
Use: "sub",
|
||||||
|
Short: "Invalid sub command test sub",
|
||||||
|
Run: func(cmd *Command, args []string) {
|
||||||
|
},
|
||||||
|
}
|
||||||
|
sub.Flags().String("images", "", "images flag")
|
||||||
|
|
||||||
|
root := &Command{
|
||||||
|
Use: "root",
|
||||||
|
Short: "Invalid sub command test root",
|
||||||
|
Run: func(cmd *Command, args []string) {
|
||||||
|
},
|
||||||
|
}
|
||||||
|
root.AddCommand(sub)
|
||||||
|
|
||||||
|
result := simpleTester(root, "sub --images=foo --master=bar")
|
||||||
|
|
||||||
|
checkResultContains(t, result, "unknown flag: --master")
|
||||||
|
|
||||||
|
if strings.Contains(result.Output, "unknown flag: --images") {
|
||||||
|
t.Errorf("invalid --master flag shouldn't fail on images, Got: \n %s", result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestPersistentFlags(t *testing.T) {
|
func TestPersistentFlags(t *testing.T) {
|
||||||
fullSetupTest("echo -s something -p more here")
|
fullSetupTest("echo -s something -p more here")
|
||||||
|
|
||||||
|
|
20
command.go
20
command.go
|
@ -399,13 +399,13 @@ func (c *Command) Root() *Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute the command determined by args and the command tree
|
// execute the command determined by args and the command tree
|
||||||
func (c *Command) findAndExecute(args []string) (err error) {
|
func (c *Command) findAndExecute(args []string) (findErr error, err error) {
|
||||||
|
|
||||||
cmd, a, e := c.Find(args)
|
cmd, a, e := c.Find(args)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e, nil
|
||||||
}
|
}
|
||||||
return cmd.execute(a)
|
return nil, cmd.execute(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) execute(a []string) (err error) {
|
func (c *Command) execute(a []string) (err error) {
|
||||||
|
@ -486,19 +486,25 @@ func (c *Command) Execute() (err error) {
|
||||||
args = c.args
|
args = c.args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var findErr error
|
||||||
|
var rootCommandError error
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
// Only the executable is called and the root is runnable, run it
|
// Only the executable is called and the root is runnable, run it
|
||||||
if c.Runnable() {
|
if c.Runnable() {
|
||||||
err = c.execute([]string(nil))
|
rootCommandError = c.execute([]string(nil))
|
||||||
|
err = rootCommandError
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
c.Help()
|
c.Help()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = c.findAndExecute(args)
|
findErr, err = c.findAndExecute(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now handle the case where the root is runnable and only flags are provided
|
// if there was an error caused by running the root command or an inability to locate the requested command
|
||||||
if err != nil && c.Runnable() {
|
// try to reparse the flags and run using the root command.
|
||||||
|
if (findErr != nil || rootCommandError != nil) && c.Runnable() {
|
||||||
// This is pretty much a custom version of the *Command.execute method
|
// This is pretty much a custom version of the *Command.execute method
|
||||||
// with a few differences because it's the final command (no fall back)
|
// with a few differences because it's the final command (no fall back)
|
||||||
e := c.ParseFlags(args)
|
e := c.ParseFlags(args)
|
||||||
|
|
Loading…
Reference in a new issue