fix: sub command help
This commit is contained in:
parent
2bcc1b7fb4
commit
772b260b37
2 changed files with 130 additions and 3 deletions
|
@ -61,11 +61,13 @@ func execute(cmd *Command, args []string, root bool) error {
|
|||
|
||||
// Calls command by its name.
|
||||
if len(args) >= 2 && cmd.Name == args[1] {
|
||||
if len(args) < 3 || !contains(cmd.subCommands, args[2]) {
|
||||
if err := run(cmd, args[2:]); err != nil {
|
||||
return fmt.Errorf("command %s error: %v", cmd.Name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// No sub-command, calls the current command.
|
||||
if len(cmd.subCommands) == 0 {
|
||||
|
@ -78,6 +80,9 @@ func execute(cmd *Command, args []string, root bool) error {
|
|||
// Trying to find the sub-command.
|
||||
for _, subCmd := range cmd.subCommands {
|
||||
if len(args) >= 2 && subCmd.Name == args[1] {
|
||||
return execute(subCmd, args, false)
|
||||
}
|
||||
if len(args) >= 3 && subCmd.Name == args[2] {
|
||||
return execute(subCmd, args[1:], false)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -559,6 +559,88 @@ func Test_execute(t *testing.T) {
|
|||
},
|
||||
expected: expected{result: "root---foo=bar--fii=bir"},
|
||||
},
|
||||
{
|
||||
desc: "sub command help",
|
||||
args: []string{"", "test", "subtest", "--help"},
|
||||
command: func() *Command {
|
||||
rootCmd := &Command{
|
||||
Name: "test",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
subCmd := &Command{
|
||||
Name: "subtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
err := rootCmd.AddCommand(subCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubCmd := &Command{
|
||||
Name: "subsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
err = subCmd.AddCommand(subSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubSubCmd := &Command{
|
||||
Name: "subsubsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
Run: func([]string) error {
|
||||
called = "subsubsubtest"
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err = subSubCmd.AddCommand(subSubSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
return rootCmd
|
||||
},
|
||||
expected: expected{},
|
||||
},
|
||||
{
|
||||
desc: "sub sub command help",
|
||||
args: []string{"", "test", "subtest", "subsubtest", "--help"},
|
||||
command: func() *Command {
|
||||
rootCmd := &Command{
|
||||
Name: "test",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
subCmd := &Command{
|
||||
Name: "subtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
err := rootCmd.AddCommand(subCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubCmd := &Command{
|
||||
Name: "subsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
err = subCmd.AddCommand(subSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubSubCmd := &Command{
|
||||
Name: "subsubsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
Run: func([]string) error {
|
||||
called = "subsubsubtest"
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err = subSubCmd.AddCommand(subSubSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
return rootCmd
|
||||
},
|
||||
expected: expected{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
@ -756,3 +838,43 @@ Flags:
|
|||
|
||||
`, string(out))
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
rootCmd := &Command{
|
||||
Name: "test",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
subCmd := &Command{
|
||||
Name: "subtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
}
|
||||
|
||||
err := rootCmd.AddCommand(subCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubCmd := &Command{
|
||||
Name: "subsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
Run: func([]string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err = subCmd.AddCommand(subSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
subSubSubCmd := &Command{
|
||||
Name: "subsubsubtest",
|
||||
Resources: []ResourceLoader{&FlagLoader{}},
|
||||
Run: func([]string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err = subSubCmd.AddCommand(subSubSubCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = execute(rootCmd, []string{"", "test", "subtest", "subsubtest", "subsubsubtest", "--help"}, true)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue