From 19b1b1ee6ac6471c61b7686cedb13190d5b98498 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Sat, 9 Nov 2024 22:48:18 +0100 Subject: [PATCH] don't inteprete commas in string slice flags Affects: --replace --find --exclude --exclude-dir --- app/app.go | 3 ++- app/app_test/app_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 5e9ff86..8fa8165 100644 --- a/app/app.go +++ b/app/app.go @@ -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 }, diff --git a/app/app_test/app_test.go b/app/app_test/app_test.go index d7f2adc..df68c56 100644 --- a/app/app_test/app_test.go +++ b/app/app_test/app_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/stretchr/testify/assert" "github.com/urfave/cli/v2" "github.com/ayoisaiah/f2/v2/app" @@ -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) + } +}