Skip to content

Commit 95a2b02

Browse files
committed
refactor: ensure '-' can be pos arg and subcommand
1 parent 369d5c8 commit 95a2b02

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

cli_test.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,52 @@ func TestParsing(t *testing.T) {
284284
},
285285
},
286286
},
287-
// subcommands (with missing or uknown error checks)
287+
// ensure '-' can be a positional argument
288+
{
289+
name: "hyphensc",
290+
cmd: NewCmd("cmd").
291+
Opt(NewOpt("a")).
292+
Arg(NewArg("arg1")),
293+
variations: []testInputOutput{
294+
{
295+
Case: ttCase(),
296+
args: []string{"-aA", "-", "-bB"},
297+
expected: Command{
298+
Inputs: []Input{
299+
{ID: "a", From: ParsedFrom{Opt: "a"}, RawValue: "A", Value: "A"},
300+
{ID: "arg1", From: ParsedFrom{Arg: 1}, RawValue: "-", Value: "-"},
301+
},
302+
Surplus: []string{"-bB"},
303+
},
304+
},
305+
},
306+
},
307+
// ensure '-' can be a subcommand
308+
{
309+
name: "hyphensc",
310+
cmd: NewCmd("cmd").
311+
Opt(NewOpt("a")).
312+
Subcmd(NewCmd("-").
313+
Opt(NewOpt("b"))),
314+
variations: []testInputOutput{
315+
{
316+
Case: ttCase(),
317+
args: []string{"-aA", "-", "-bB"},
318+
expected: Command{
319+
Inputs: []Input{
320+
{ID: "a", From: ParsedFrom{Opt: "a"}, RawValue: "A", Value: "A"},
321+
},
322+
Subcmd: &Command{
323+
Name: "-",
324+
Inputs: []Input{
325+
{ID: "b", From: ParsedFrom{Opt: "b"}, RawValue: "B", Value: "B"},
326+
},
327+
},
328+
},
329+
},
330+
},
331+
},
332+
// subcommands (with missing or unknown error checks)
288333
{
289334
name: "subcommands",
290335
cmd: NewCmd("cmd").
@@ -301,6 +346,7 @@ func TestParsing(t *testing.T) {
301346
args: []string{"one", "--bb", "B"},
302347
expected: Command{
303348
Subcmd: &Command{
349+
Name: "one",
304350
Inputs: []Input{
305351
{ID: "bb", From: ParsedFrom{Opt: "bb"}, RawValue: "B", Value: "B"},
306352
},
@@ -312,6 +358,7 @@ func TestParsing(t *testing.T) {
312358
args: []string{"two", "--dd", "D"},
313359
expected: Command{
314360
Subcmd: &Command{
361+
Name: "two",
315362
Inputs: []Input{
316363
{ID: "dd", From: ParsedFrom{Opt: "dd"}, RawValue: "D", Value: "D"},
317364
},
@@ -497,17 +544,21 @@ func TestParsing(t *testing.T) {
497544
func cmpParsed(t *testing.T, tioInfo string, exp, got *Command) {
498545
t.Helper()
499546

547+
// command name
548+
if got.Name != exp.Name {
549+
t.Errorf("%s:\nexpected command name '%s', got '%s'", tioInfo, exp.Name, got.Name)
550+
}
500551
// inputs
501552
{
502553
gotNumInputs := len(got.Inputs)
503554
expNumInputs := len(exp.Inputs)
504555
if gotNumInputs != expNumInputs {
505-
t.Fatalf("%s: expected %d parsed options, got %d", tioInfo, expNumInputs, gotNumInputs)
556+
t.Fatalf("%s: expected %d parsed inputs, got %d", tioInfo, expNumInputs, gotNumInputs)
506557
}
507558
for i, gotOpt := range got.Inputs {
508559
expOpt := exp.Inputs[i]
509560
if !reflect.DeepEqual(gotOpt, expOpt) {
510-
t.Errorf("%s: parsed options[%d]:\nexpected %+#v\n got %+#v", tioInfo, i, expOpt, gotOpt)
561+
t.Errorf("%s: parsed inputs[%d]:\nexpected %+#v\n got %+#v", tioInfo, i, expOpt, gotOpt)
511562
}
512563
}
513564
}

0 commit comments

Comments
 (0)