fix: sub command help
This commit is contained in:
parent
2bcc1b7fb4
commit
772b260b37
2 changed files with 130 additions and 3 deletions
|
@ -61,10 +61,12 @@ func execute(cmd *Command, args []string, root bool) error {
|
||||||
|
|
||||||
// Calls command by its name.
|
// Calls command by its name.
|
||||||
if len(args) >= 2 && cmd.Name == args[1] {
|
if len(args) >= 2 && cmd.Name == args[1] {
|
||||||
if err := run(cmd, args[2:]); err != nil {
|
if len(args) < 3 || !contains(cmd.subCommands, args[2]) {
|
||||||
return fmt.Errorf("command %s error: %v", cmd.Name, err)
|
if err := run(cmd, args[2:]); err != nil {
|
||||||
|
return fmt.Errorf("command %s error: %v", cmd.Name, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No sub-command, calls the current command.
|
// No sub-command, calls the current command.
|
||||||
|
@ -78,6 +80,9 @@ func execute(cmd *Command, args []string, root bool) error {
|
||||||
// Trying to find the sub-command.
|
// Trying to find the sub-command.
|
||||||
for _, subCmd := range cmd.subCommands {
|
for _, subCmd := range cmd.subCommands {
|
||||||
if len(args) >= 2 && subCmd.Name == args[1] {
|
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)
|
return execute(subCmd, args[1:], false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -559,6 +559,88 @@ func Test_execute(t *testing.T) {
|
||||||
},
|
},
|
||||||
expected: expected{result: "root---foo=bar--fii=bir"},
|
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 {
|
for _, test := range testCases {
|
||||||
|
@ -756,3 +838,43 @@ Flags:
|
||||||
|
|
||||||
`, string(out))
|
`, 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