diff --git a/command_parse.go b/command_parse.go index 939ad90b85..6b149750ac 100644 --- a/command_parse.go +++ b/command_parse.go @@ -114,7 +114,9 @@ func (cmd *Command) parseFlags(args Args) (Args, error) { return &stringSliceArgs{posArgs}, nil } - posArgs = append(posArgs, firstArg) + // firstArg is a trimmed copy that classifies the argument; the + // argument itself is what the action receives, byte for byte. + posArgs = append(posArgs, rargs[0]) continue } diff --git a/command_test.go b/command_test.go index 843c3a47b3..823eacff1f 100644 --- a/command_test.go +++ b/command_test.go @@ -1156,6 +1156,22 @@ func TestCommand_CommandWithDash(t *testing.T) { require.Equal(t, "-", args.Get(1)) } +func TestCommand_PositionalArgsKeepWhitespace(t *testing.T) { + var args Args + + cmd := &Command{ + Name: "prog", + Action: func(_ context.Context, cmd *Command) error { + args = cmd.Args() + return nil + }, + } + + require.NoError(t, cmd.Run(buildTestContext(t), []string{"prog", " padded ", "\ttabbed\t"})) + require.NotNil(t, args) + require.Equal(t, []string{" padded ", "\ttabbed\t"}, args.Slice()) +} + func TestCommand_CommandWithNoFlagBeforeTerminator(t *testing.T) { var args Args