Skip to content

Commit

Permalink
don't inteprete commas in string slice flags
Browse files Browse the repository at this point in the history
Affects:

--replace
--find
--exclude
--exclude-dir
  • Loading branch information
ayoisaiah committed Nov 9, 2024
1 parent 7f5d251 commit 19b1b1e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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)
}
}

0 comments on commit 19b1b1e

Please sign in to comment.