Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better hint on positional argument #799

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
}

Expand Down
36 changes: 19 additions & 17 deletions internal/core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func testGetCommands() *Commands {
},
},
&Command{
Namespace: "test-positional",
Namespace: "test",
Resource: "positional",
ArgSpecs: ArgSpecs{
{
Name: "name-id",
Expand All @@ -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 := ""
Expand Down Expand Up @@ -91,15 +93,15 @@ 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"),
),
}))
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"),
Expand All @@ -111,80 +113,80 @@ 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 <name-id>",
Hint: "Try running: scw test positional <name-id>",
}),
),
}))

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 <name-id> tag=world",
Hint: "Try running: scw test positional <name-id> 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"),
}),
),
}))
})

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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
USAGE:
scw test-positional <name-id> [arg=value ...]
scw test positional <name-id> [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
Expand Down