Skip to content

Commit c798958

Browse files
fix(arg-spec): revert multi positional arg definition (#4627)
Signed-off-by: Olivier Cano <[email protected]>
1 parent cca5fbf commit c798958

File tree

2 files changed

+5
-80
lines changed

2 files changed

+5
-80
lines changed

core/arg_specs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ var AllLocalities = "all"
1414

1515
type ArgSpecs []*ArgSpec
1616

17-
// GetPositionalArg returns the last positional argument from the arg specs.
17+
// GetPositionalArg if exist returns the positional argument from the arg specs.
18+
// Panics when more than one positional arg is found.
1819
func (s ArgSpecs) GetPositionalArg() *ArgSpec {
1920
var positionalArg *ArgSpec
2021
for _, argSpec := range s {
2122
if argSpec.Positional {
23+
if positionalArg != nil {
24+
panic(fmt.Errorf("more than one positional parameter detected: %s and %s are flagged as positional arg", positionalArg.Name, argSpec.Name))
25+
}
2226
positionalArg = argSpec
2327
}
2428
}

core/cobra_utils_test.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ type testType struct {
1717
Tag string
1818
}
1919

20-
type testTypeManyTags struct {
21-
NameID string
22-
Tags []string
23-
}
24-
2520
type testDate struct {
2621
Date *time.Time
2722
}
@@ -83,45 +78,6 @@ func testGetCommands() *core.Commands {
8378
return argsI, nil
8479
},
8580
},
86-
&core.Command{
87-
Namespace: "test",
88-
Resource: "many-positional",
89-
ArgSpecs: core.ArgSpecs{
90-
{
91-
Name: "name-id",
92-
Positional: true,
93-
},
94-
{
95-
Name: "tag",
96-
Positional: true,
97-
},
98-
},
99-
AllowAnonymousClient: true,
100-
ArgsType: reflect.TypeOf(testType{}),
101-
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
102-
return argsI, nil
103-
},
104-
},
105-
&core.Command{
106-
Namespace: "test",
107-
Resource: "many-multi-positional",
108-
ArgSpecs: core.ArgSpecs{
109-
{
110-
Name: "name-id",
111-
Positional: true,
112-
},
113-
{
114-
Name: "tags",
115-
Positional: true,
116-
},
117-
},
118-
AcceptMultiplePositionalArgs: true,
119-
AllowAnonymousClient: true,
120-
ArgsType: reflect.TypeOf(testTypeManyTags{}),
121-
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
122-
return argsI, nil
123-
},
124-
},
12581
&core.Command{
12682
Namespace: "test",
12783
Resource: "raw-args",
@@ -298,41 +254,6 @@ func Test_PositionalArg(t *testing.T) {
298254
core.TestCheckGolden(),
299255
),
300256
}))
301-
302-
t.Run("many positional", core.Test(&core.TestConfig{
303-
Commands: testGetCommands(),
304-
Cmd: "scw test many-positional tag1 name-id=plop",
305-
Check: core.TestCheckExitCode(0),
306-
}))
307-
308-
t.Run("many positional", core.Test(&core.TestConfig{
309-
Commands: testGetCommands(),
310-
Cmd: "scw test many-positional tag1 name-id=plop",
311-
Check: core.TestCheckCombine(
312-
core.TestCheckExitCode(0),
313-
func(t *testing.T, ctx *core.CheckFuncCtx) {
314-
t.Helper()
315-
res := ctx.Result.(*testType)
316-
assert.Equal(t, "plop", res.NameID)
317-
assert.Equal(t, "tag1", res.Tag)
318-
},
319-
),
320-
}))
321-
322-
t.Run("many multi-positional", core.Test(&core.TestConfig{
323-
Commands: testGetCommands(),
324-
Cmd: "scw test many-multi-positional pos1 pos2 name-id=plop",
325-
Check: core.TestCheckCombine(
326-
core.TestCheckExitCode(0),
327-
func(t *testing.T, ctx *core.CheckFuncCtx) {
328-
t.Helper()
329-
res := ctx.Result.(*testTypeManyTags)
330-
assert.Equal(t, "plop", res.NameID)
331-
assert.Equal(t, "pos1", res.Tags[0])
332-
assert.Equal(t, "pos2", res.Tags[1])
333-
},
334-
),
335-
}))
336257
}
337258

338259
func Test_MultiPositionalArg(t *testing.T) {

0 commit comments

Comments
 (0)