Skip to content

fix(arg-spec): revert multi positional arg definition #4627

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

Merged
merged 1 commit into from
Apr 1, 2025
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
6 changes: 5 additions & 1 deletion core/arg_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ var AllLocalities = "all"

type ArgSpecs []*ArgSpec

// GetPositionalArg returns the last positional argument from the arg specs.
// GetPositionalArg if exist returns the positional argument from the arg specs.
// Panics when more than one positional arg is found.
func (s ArgSpecs) GetPositionalArg() *ArgSpec {
var positionalArg *ArgSpec
for _, argSpec := range s {
if argSpec.Positional {
if positionalArg != nil {
panic(fmt.Errorf("more than one positional parameter detected: %s and %s are flagged as positional arg", positionalArg.Name, argSpec.Name))
}
positionalArg = argSpec
}
}
Expand Down
79 changes: 0 additions & 79 deletions core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ type testType struct {
Tag string
}

type testTypeManyTags struct {
NameID string
Tags []string
}

type testDate struct {
Date *time.Time
}
Expand Down Expand Up @@ -83,45 +78,6 @@ func testGetCommands() *core.Commands {
return argsI, nil
},
},
&core.Command{
Namespace: "test",
Resource: "many-positional",
ArgSpecs: core.ArgSpecs{
{
Name: "name-id",
Positional: true,
},
{
Name: "tag",
Positional: true,
},
},
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testType{}),
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
return argsI, nil
},
},
&core.Command{
Namespace: "test",
Resource: "many-multi-positional",
ArgSpecs: core.ArgSpecs{
{
Name: "name-id",
Positional: true,
},
{
Name: "tags",
Positional: true,
},
},
AcceptMultiplePositionalArgs: true,
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testTypeManyTags{}),
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
return argsI, nil
},
},
&core.Command{
Namespace: "test",
Resource: "raw-args",
Expand Down Expand Up @@ -298,41 +254,6 @@ func Test_PositionalArg(t *testing.T) {
core.TestCheckGolden(),
),
}))

t.Run("many positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test many-positional tag1 name-id=plop",
Check: core.TestCheckExitCode(0),
}))

t.Run("many positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test many-positional tag1 name-id=plop",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
res := ctx.Result.(*testType)
assert.Equal(t, "plop", res.NameID)
assert.Equal(t, "tag1", res.Tag)
},
),
}))

t.Run("many multi-positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test many-multi-positional pos1 pos2 name-id=plop",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
res := ctx.Result.(*testTypeManyTags)
assert.Equal(t, "plop", res.NameID)
assert.Equal(t, "pos1", res.Tags[0])
assert.Equal(t, "pos2", res.Tags[1])
},
),
}))
}

func Test_MultiPositionalArg(t *testing.T) {
Expand Down
Loading