Skip to content
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

Avoid interpreting commas in string slice flags #88

Merged
merged 1 commit into from
Nov 9, 2024
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
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ offers several options for fine-grained control over the renaming process.`,
flagTargetDir,
flagVerbose,
},
UseShortOptionHandling: true,
UseShortOptionHandling: true,
DisableSliceFlagSeparator: true,
OnUsageError: func(_ *cli.Context, err error, _ bool) error {
return err
},
Expand Down
43 changes: 43 additions & 0 deletions app/app_test/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"

"github.com/ayoisaiah/f2/v2/app"
Expand Down Expand Up @@ -207,3 +208,45 @@ func TestDefaultEnv(t *testing.T) {
})
}
}

func TestStringSliceFlag(t *testing.T) {
cases := []*testutil.TestCase{
{
Name: "commas should not be interpreted as a separator",
Args: []string{
"f2_test",
"--replace",
"Windows, Linux Episode {%d}{ext}",
},
Want: []string{"Windows, Linux Episode {%d}{ext}"},
},
{
Name: "multiple flags should add a separate value to the slice",
Args: []string{
"f2_test",
"--replace",
"Windows",
"--replace",
"Linux Episode {%d}{ext}",
},
Want: []string{"Windows", "Linux Episode {%d}{ext}"},
},
}

for _, tc := range cases {
var stdout bytes.Buffer

renamer, err := app.Get(os.Stdin, &stdout)
if err != nil {
t.Fatal(err)
}

renamer.Action = func(ctx *cli.Context) error {
assert.Equal(t, tc.Want, ctx.StringSlice("replace"))

return nil
}

_ = renamer.Run(tc.Args)
}
}