Skip to content

Commit b05dc53

Browse files
authored
fix: better hint on positional argument (#799)
1 parent 5bd3f6a commit b05dc53

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

internal/core/cobra_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func positionalArgHint(cmd *Command, hintValue string, otherArgs []string, posit
154154
// Suggest to use the other arguments.
155155
suggestedArgs = append(suggestedArgs, otherArgs...)
156156

157-
suggestedCommand := append([]string{"scw", cmd.getPath()}, suggestedArgs...)
157+
suggestedCommand := append([]string{"scw", cmd.GetCommandLine()}, suggestedArgs...)
158158
return "Try running: " + strings.Join(suggestedCommand, " ")
159159
}
160160

internal/core/cobra_utils_test.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func testGetCommands() *Commands {
2929
},
3030
},
3131
&Command{
32-
Namespace: "test-positional",
32+
Namespace: "test",
33+
Resource: "positional",
3334
ArgSpecs: ArgSpecs{
3435
{
3536
Name: "name-id",
@@ -45,7 +46,8 @@ func testGetCommands() *Commands {
4546
},
4647
},
4748
&Command{
48-
Namespace: "test-raw-args",
49+
Namespace: "test",
50+
Resource: "raw-args",
4951
ArgsType: reflect.TypeOf(args.RawArgs{}),
5052
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
5153
res := ""
@@ -91,15 +93,15 @@ func Test_handleUnmarshalErrors(t *testing.T) {
9193
func Test_RawArgs(t *testing.T) {
9294
t.Run("Simple", Test(&TestConfig{
9395
Commands: testGetCommands(),
94-
Cmd: "scw test-raw-args -- blabla",
96+
Cmd: "scw test raw-args -- blabla",
9597
Check: TestCheckCombine(
9698
TestCheckExitCode(0),
9799
TestCheckStdout("blabla\n"),
98100
),
99101
}))
100102
t.Run("Multiple", Test(&TestConfig{
101103
Commands: testGetCommands(),
102-
Cmd: "scw test-raw-args -- blabla foo bar",
104+
Cmd: "scw test raw-args -- blabla foo bar",
103105
Check: TestCheckCombine(
104106
TestCheckExitCode(0),
105107
TestCheckStdout("blabla foo bar\n"),
@@ -111,80 +113,80 @@ func Test_PositionalArg(t *testing.T) {
111113
t.Run("Error", func(t *testing.T) {
112114
t.Run("Missing1", Test(&TestConfig{
113115
Commands: testGetCommands(),
114-
Cmd: "scw test-positional",
116+
Cmd: "scw test positional",
115117
Check: TestCheckCombine(
116118
TestCheckExitCode(1),
117119
TestCheckError(&CliError{
118120
Err: fmt.Errorf("a positional argument is required for this command"),
119-
Hint: "Try running: scw test-positional <name-id>",
121+
Hint: "Try running: scw test positional <name-id>",
120122
}),
121123
),
122124
}))
123125

124126
t.Run("Missing2", Test(&TestConfig{
125127
Commands: testGetCommands(),
126-
Cmd: "scw test-positional tag=world",
128+
Cmd: "scw test positional tag=world",
127129
Check: TestCheckCombine(
128130
TestCheckExitCode(1),
129131
TestCheckError(&CliError{
130132
Err: fmt.Errorf("a positional argument is required for this command"),
131-
Hint: "Try running: scw test-positional <name-id> tag=world",
133+
Hint: "Try running: scw test positional <name-id> tag=world",
132134
}),
133135
),
134136
}))
135137

136138
t.Run("Invalid1", Test(&TestConfig{
137139
Commands: testGetCommands(),
138-
Cmd: "scw test-positional name-id=plop tag=world",
140+
Cmd: "scw test positional name-id=plop tag=world",
139141
Check: TestCheckCombine(
140142
TestCheckExitCode(1),
141143
TestCheckError(&CliError{
142144
Err: fmt.Errorf("a positional argument is required for this command"),
143-
Hint: "Try running: scw test-positional plop tag=world",
145+
Hint: "Try running: scw test positional plop tag=world",
144146
}),
145147
),
146148
}))
147149

148150
t.Run("Invalid2", Test(&TestConfig{
149151
Commands: testGetCommands(),
150-
Cmd: "scw test-positional tag=world name-id=plop",
152+
Cmd: "scw test positional tag=world name-id=plop",
151153
Check: TestCheckCombine(
152154
TestCheckExitCode(1),
153155
TestCheckError(&CliError{
154156
Err: fmt.Errorf("a positional argument is required for this command"),
155-
Hint: fmt.Sprintf("Try running: scw test-positional plop tag=world"),
157+
Hint: fmt.Sprintf("Try running: scw test positional plop tag=world"),
156158
}),
157159
),
158160
}))
159161

160162
t.Run("Invalid3", Test(&TestConfig{
161163
Commands: testGetCommands(),
162-
Cmd: "scw test-positional plop name-id=plop",
164+
Cmd: "scw test positional plop name-id=plop",
163165
Check: TestCheckCombine(
164166
TestCheckExitCode(1),
165167
TestCheckError(&CliError{
166168
Err: fmt.Errorf("a positional argument is required for this command"),
167-
Hint: fmt.Sprintf("Try running: scw test-positional plop"),
169+
Hint: fmt.Sprintf("Try running: scw test positional plop"),
168170
}),
169171
),
170172
}))
171173
})
172174

173175
t.Run("simple", Test(&TestConfig{
174176
Commands: testGetCommands(),
175-
Cmd: "scw test-positional plop",
177+
Cmd: "scw test positional plop",
176178
Check: TestCheckExitCode(0),
177179
}))
178180

179181
t.Run("full command", Test(&TestConfig{
180182
Commands: testGetCommands(),
181-
Cmd: "scw test-positional plop tag=world",
183+
Cmd: "scw test positional plop tag=world",
182184
Check: TestCheckExitCode(0),
183185
}))
184186

185187
t.Run("full command", Test(&TestConfig{
186188
Commands: testGetCommands(),
187-
Cmd: "scw test-positional -h",
189+
Cmd: "scw test positional -h",
188190
Check: TestCheckCombine(
189191
TestCheckExitCode(0),
190192
TestCheckGolden(),

internal/core/testdata/test-positional-arg-full-command#01.stderr.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
USAGE:
2-
scw test-positional <name-id> [arg=value ...]
2+
scw test positional <name-id> [arg=value ...]
33

44
ARGS:
55
name-id
66
[tag]
77

88
FLAGS:
9-
-h, --help help for test-positional
9+
-h, --help help for positional
1010

1111
GLOBAL FLAGS:
1212
-D, --debug Enable debug mode

0 commit comments

Comments
 (0)