Skip to content

Commit

Permalink
add AcceptMultiPositionalArgs to Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Mia-Cross committed Apr 8, 2024
1 parent 95ed909 commit 8067653
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 128 deletions.
2 changes: 1 addition & 1 deletion internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
results := MultiResults(nil)
rawArgs = rawArgs.RemoveAllPositional()

if cmd.Verb == "set" {
if cmd.AcceptMultiplePositionalArgs {
argNameWithIndex := fmt.Sprintf("%s.%d", positionalArgSpec.Name, 0)
rawArgsWithPositional := rawArgs.Add(argNameWithIndex, positionalArgs[0])
for i := 1; i < len(positionalArgs); i++ {
Expand Down
18 changes: 10 additions & 8 deletions internal/core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type testDate struct {
Date *time.Time
}

type testSetType struct {
type testAcceptMultiPositionalArgsType struct {
NameIDs []string
Tag string
}
Expand Down Expand Up @@ -61,8 +61,7 @@ func testGetCommands() *core.Commands {
},
&core.Command{
Namespace: "test",
Resource: "positional",
Verb: "set",
Resource: "multi-positional",
ArgSpecs: core.ArgSpecs{
{
Name: "name-ids",
Expand All @@ -72,8 +71,9 @@ func testGetCommands() *core.Commands {
Name: "tag",
},
},
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testSetType{}),
AcceptMultiplePositionalArgs: true,
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testAcceptMultiPositionalArgsType{}),
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
return argsI, nil
},
Expand Down Expand Up @@ -270,10 +270,12 @@ func Test_PositionalArg(t *testing.T) {
core.TestCheckGolden(),
),
}))
}

func Test_MultiPositionalArg(t *testing.T) {
t.Run("set verb with one positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test positional set pos1 tag=tag1",
Cmd: "scw test multi-positional pos1 tag=tag1",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
core.TestCheckGolden(),
Expand All @@ -282,7 +284,7 @@ func Test_PositionalArg(t *testing.T) {

t.Run("set verb with multi positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test positional set pos1 pos2 pos3 tag=tag1",
Cmd: "scw test multi-positional pos1 pos2 pos3 tag=tag1",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
core.TestCheckGolden(),
Expand All @@ -291,7 +293,7 @@ func Test_PositionalArg(t *testing.T) {

t.Run("set verb with no positional", core.Test(&core.TestConfig{
Commands: testGetCommands(),
Cmd: "scw test positional set tag=tag1",
Cmd: "scw test multi-positional tag=tag1",
Check: core.TestCheckCombine(
core.TestCheckExitCode(1),
core.TestCheckGolden(),
Expand Down
3 changes: 3 additions & 0 deletions internal/core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Command struct {
// ArgSpecs defines specifications for arguments.
ArgSpecs ArgSpecs

// AcceptMultiplePositionalArgs defines whether the command can accept multiple positional arguments.
AcceptMultiplePositionalArgs bool

// View defines the View for this command.
// It is used to create the different options for the different Marshalers.
View *View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
A positional argument is required for this command

Hint:
Try running: scw test positional set <name-ids> tag=tag1
Try running: scw test multi-positional <name-ids> tag=tag1
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "a positional argument is required for this command",
"error": {},
"hint": "Try running: scw test positional set \u003cname-ids\u003e tag=tag1"
"hint": "Try running: scw test multi-positional \u003cname-ids\u003e tag=tag1"
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
USAGE:
scw test positional <command> <name-id ...> [arg=value ...]
scw test positional <name-id ...> [arg=value ...]

ARGS:
name-id
[tag]

AVAILABLE COMMANDS:
set

FLAGS:
-h, --help help for positional

Expand All @@ -18,5 +15,3 @@ GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use

Use "scw test positional [command] --help" for more information about a command.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 8067653

Please sign in to comment.