diff --git a/command_run.go b/command_run.go index 676a14c676..1e9649300f 100644 --- a/command_run.go +++ b/command_run.go @@ -214,6 +214,10 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context for _, flag := range cmd.allFlags() { isSet := flag.IsSet() + // Propagate the command's slice/map separator config before PostParse + // so that env-var values are split with the same separator as CLI + // values (see https://github.com/urfave/cli/issues/2262). + cmd.setMultiValueParsingConfig(flag) if err := flag.PostParse(); err != nil { return ctx, err } diff --git a/command_test.go b/command_test.go index dafee0bee6..e0dfc2ad91 100644 --- a/command_test.go +++ b/command_test.go @@ -4590,6 +4590,28 @@ func TestCommandSliceFlagSeparator(t *testing.T) { r.Equal([]string{"ff", "dd", "gg", "t,u"}, cmd.Value("foo")) } +// TestCommandSliceFlagSeparatorFromEnvVar is a regression test for +// https://github.com/urfave/cli/issues/2262. +// SliceFlagSeparator must be respected when flag values come from env vars, +// not only when they are supplied on the command line. +func TestCommandSliceFlagSeparatorFromEnvVar(t *testing.T) { + t.Setenv("FOO", "ff;dd;gg") + + cmd := &Command{ + SliceFlagSeparator: ";", + Flags: []Flag{ + &StringSliceFlag{ + Name: "foo", + Sources: EnvVars("FOO"), + }, + }, + } + + r := require.New(t) + r.NoError(cmd.Run(buildTestContext(t), []string{"app"})) + r.Equal([]string{"ff", "dd", "gg"}, cmd.Value("foo")) +} + func TestCommandMapKeyValueFlagSeparator(t *testing.T) { cmd := &Command{ MapFlagKeyValueSeparator: ":",