From 7951f8681b4ef3223a1b1f29e6d5fa723b1f8e40 Mon Sep 17 00:00:00 2001 From: loicbourgois Date: Tue, 24 Mar 2020 15:40:19 +0100 Subject: [PATCH] fix: better hint on positional argument --- internal/core/cobra_utils.go | 2 +- internal/core/cobra_utils_test.go | 36 ++++++++++--------- ...sitional-arg-full-command#01.stderr.golden | 4 +-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/internal/core/cobra_utils.go b/internal/core/cobra_utils.go index d5f0f70028..eb8da60cf9 100644 --- a/internal/core/cobra_utils.go +++ b/internal/core/cobra_utils.go @@ -154,7 +154,7 @@ func positionalArgHint(cmd *Command, hintValue string, otherArgs []string, posit // Suggest to use the other arguments. suggestedArgs = append(suggestedArgs, otherArgs...) - suggestedCommand := append([]string{"scw", cmd.getPath()}, suggestedArgs...) + suggestedCommand := append([]string{"scw", cmd.GetCommandLine()}, suggestedArgs...) return "Try running: " + strings.Join(suggestedCommand, " ") } diff --git a/internal/core/cobra_utils_test.go b/internal/core/cobra_utils_test.go index 75274f5b2e..7763fbc026 100644 --- a/internal/core/cobra_utils_test.go +++ b/internal/core/cobra_utils_test.go @@ -29,7 +29,8 @@ func testGetCommands() *Commands { }, }, &Command{ - Namespace: "test-positional", + Namespace: "test", + Resource: "positional", ArgSpecs: ArgSpecs{ { Name: "name-id", @@ -45,7 +46,8 @@ func testGetCommands() *Commands { }, }, &Command{ - Namespace: "test-raw-args", + Namespace: "test", + Resource: "raw-args", ArgsType: reflect.TypeOf(args.RawArgs{}), Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { res := "" @@ -91,7 +93,7 @@ func Test_handleUnmarshalErrors(t *testing.T) { func Test_RawArgs(t *testing.T) { t.Run("Simple", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-raw-args -- blabla", + Cmd: "scw test raw-args -- blabla", Check: TestCheckCombine( TestCheckExitCode(0), TestCheckStdout("blabla\n"), @@ -99,7 +101,7 @@ func Test_RawArgs(t *testing.T) { })) t.Run("Multiple", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-raw-args -- blabla foo bar", + Cmd: "scw test raw-args -- blabla foo bar", Check: TestCheckCombine( TestCheckExitCode(0), TestCheckStdout("blabla foo bar\n"), @@ -111,60 +113,60 @@ func Test_PositionalArg(t *testing.T) { t.Run("Error", func(t *testing.T) { t.Run("Missing1", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional", + Cmd: "scw test positional", Check: TestCheckCombine( TestCheckExitCode(1), TestCheckError(&CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: "Try running: scw test-positional ", + Hint: "Try running: scw test positional ", }), ), })) t.Run("Missing2", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional tag=world", + Cmd: "scw test positional tag=world", Check: TestCheckCombine( TestCheckExitCode(1), TestCheckError(&CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: "Try running: scw test-positional tag=world", + Hint: "Try running: scw test positional tag=world", }), ), })) t.Run("Invalid1", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional name-id=plop tag=world", + Cmd: "scw test positional name-id=plop tag=world", Check: TestCheckCombine( TestCheckExitCode(1), TestCheckError(&CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: "Try running: scw test-positional plop tag=world", + Hint: "Try running: scw test positional plop tag=world", }), ), })) t.Run("Invalid2", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional tag=world name-id=plop", + Cmd: "scw test positional tag=world name-id=plop", Check: TestCheckCombine( TestCheckExitCode(1), TestCheckError(&CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: fmt.Sprintf("Try running: scw test-positional plop tag=world"), + Hint: fmt.Sprintf("Try running: scw test positional plop tag=world"), }), ), })) t.Run("Invalid3", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional plop name-id=plop", + Cmd: "scw test positional plop name-id=plop", Check: TestCheckCombine( TestCheckExitCode(1), TestCheckError(&CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: fmt.Sprintf("Try running: scw test-positional plop"), + Hint: fmt.Sprintf("Try running: scw test positional plop"), }), ), })) @@ -172,19 +174,19 @@ func Test_PositionalArg(t *testing.T) { t.Run("simple", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional plop", + Cmd: "scw test positional plop", Check: TestCheckExitCode(0), })) t.Run("full command", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional plop tag=world", + Cmd: "scw test positional plop tag=world", Check: TestCheckExitCode(0), })) t.Run("full command", Test(&TestConfig{ Commands: testGetCommands(), - Cmd: "scw test-positional -h", + Cmd: "scw test positional -h", Check: TestCheckCombine( TestCheckExitCode(0), TestCheckGolden(), diff --git a/internal/core/testdata/test-positional-arg-full-command#01.stderr.golden b/internal/core/testdata/test-positional-arg-full-command#01.stderr.golden index 22e3d3399b..04e5dbdd91 100644 --- a/internal/core/testdata/test-positional-arg-full-command#01.stderr.golden +++ b/internal/core/testdata/test-positional-arg-full-command#01.stderr.golden @@ -1,12 +1,12 @@ USAGE: - scw test-positional [arg=value ...] + scw test positional [arg=value ...] ARGS: name-id [tag] FLAGS: - -h, --help help for test-positional + -h, --help help for positional GLOBAL FLAGS: -D, --debug Enable debug mode