Skip to content

Commit df51685

Browse files
committed
fix: pass a bare dash through byte for byte
Positional arguments reach the action unchanged, but the bare-dash branch appended the trimmed copy, so " - " arrived as "-". Append rargs[0] there as well, and compare against "-" directly rather than checking the length. Fold TestCommand_BareDashKeepsFollowingArgs into the table test, now TestCommand_BareDash, and add cases for a dash with surrounding whitespace and for an undefined flag after "-". Before this fix that flag was silently dropped; it is now reported as an error. Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 828fb80 commit df51685

2 files changed

Lines changed: 30 additions & 29 deletions

File tree

‎command_parse.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
122122
}
123123

124124
numMinuses := 1
125-
// this is same as firstArg == "-"
126-
if len(firstArg) == 1 {
127-
posArgs = append(posArgs, firstArg)
125+
if firstArg == "-" {
126+
posArgs = append(posArgs, rargs[0])
128127
continue
129128
}
130129

‎command_test.go‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,44 +1156,39 @@ func TestCommand_CommandWithDash(t *testing.T) {
11561156
require.Equal(t, "-", args.Get(1))
11571157
}
11581158

1159-
func TestCommand_BareDashKeepsFollowingArgs(t *testing.T) {
1160-
var args Args
1161-
1162-
cmd := &Command{
1163-
Commands: []*Command{
1164-
{
1165-
Name: "cmd",
1166-
Action: func(_ context.Context, cmd *Command) error {
1167-
args = cmd.Args()
1168-
return nil
1169-
},
1170-
},
1171-
},
1172-
}
1173-
1174-
require.NoError(t, cmd.Run(buildTestContext(t), []string{"", "cmd", "-", "foo", "bar"}))
1175-
require.NotNil(t, args)
1176-
require.Equal(t, []string{"-", "foo", "bar"}, args.Slice())
1177-
}
1178-
1179-
func TestCommand_BareDashThenFlags(t *testing.T) {
1159+
func TestCommand_BareDash(t *testing.T) {
11801160
cases := []struct {
11811161
name string
11821162
argv []string
11831163
option string
11841164
args []string
1165+
err string
11851166
}{
1167+
{
1168+
name: "arguments after a bare dash are kept",
1169+
argv: []string{"", "cmd", "-", "foo", "bar"},
1170+
args: []string{"-", "foo", "bar"},
1171+
},
11861172
{
11871173
name: "flags after a bare dash still parse",
11881174
argv: []string{"", "cmd", "-", "--option", "my-option", "foo"},
11891175
option: "my-option",
11901176
args: []string{"-", "foo"},
11911177
},
11921178
{
1193-
name: "terminator after a bare dash still stops flag parsing",
1194-
argv: []string{"", "cmd", "-", "--", "--option", "leftover"},
1195-
option: "",
1196-
args: []string{"-", "--option", "leftover"},
1179+
name: "terminator after a bare dash still stops flag parsing",
1180+
argv: []string{"", "cmd", "-", "--", "--option", "leftover"},
1181+
args: []string{"-", "--option", "leftover"},
1182+
},
1183+
{
1184+
name: "a bare dash with surrounding whitespace is passed through unchanged",
1185+
argv: []string{"", "cmd", " - ", "foo"},
1186+
args: []string{" - ", "foo"},
1187+
},
1188+
{
1189+
name: "an undefined flag after a bare dash is an error",
1190+
argv: []string{"", "cmd", "-", "--undefined"},
1191+
err: "flag provided but not defined: -undefined",
11971192
},
11981193
}
11991194

@@ -1203,6 +1198,8 @@ func TestCommand_BareDashThenFlags(t *testing.T) {
12031198
var args Args
12041199

12051200
cmd := &Command{
1201+
Writer: io.Discard,
1202+
ErrWriter: io.Discard,
12061203
Commands: []*Command{
12071204
{
12081205
Name: "cmd",
@@ -1218,7 +1215,12 @@ func TestCommand_BareDashThenFlags(t *testing.T) {
12181215
},
12191216
}
12201217

1221-
require.NoError(t, cmd.Run(buildTestContext(t), tc.argv))
1218+
err := cmd.Run(buildTestContext(t), tc.argv)
1219+
if tc.err != "" {
1220+
require.EqualError(t, err, tc.err)
1221+
return
1222+
}
1223+
require.NoError(t, err)
12221224
require.Equal(t, tc.option, parsedOption)
12231225
require.NotNil(t, args)
12241226
require.Equal(t, tc.args, args.Slice())

0 commit comments

Comments
 (0)