Skip to content

command: apply SliceFlagSeparator to env-var values in PostParse#2313

Open
alliasgher wants to merge 3 commits intourfave:mainfrom
alliasgher:fix-slice-separator-env-var
Open

command: apply SliceFlagSeparator to env-var values in PostParse#2313
alliasgher wants to merge 3 commits intourfave:mainfrom
alliasgher:fix-slice-separator-env-var

Conversation

@alliasgher
Copy link
Copy Markdown

Summary

SliceFlagSeparator (and DisableSliceFlagSeparator) were not applied to flag values sourced from environment variables.

setMultiValueParsingConfig — which propagates the command's separator settings to the flag's underlying value — was only called inside cmd.set(). That path is taken when parsing CLI flags. When values come from environment variables they flow through flag.PostParse(), which called f.Set() without first configuring the separator. As a result the flag's value always used the default "," separator regardless of what SliceFlagSeparator was set to.

Before

export A="a;b;c"
./myapp  # SliceFlagSeparator: ";"
# cmd.StringSlice("a") → ["a;b;c"]  ← NOT split

After

export A="a;b;c"
./myapp  # SliceFlagSeparator: ";"
# cmd.StringSlice("a") → ["a", "b", "c"]  ← correctly split

Fix

Add a cmd.setMultiValueParsingConfig(flag) call in the PostParse loop (in command_run.go) before flag.PostParse() so the separator is configured regardless of whether the value came from the CLI or an env var.

Fixes #2262

Checklist

  • Test added (TestCommandSliceFlagSeparatorFromEnvVar)
  • Full test suite passes

When FlagStringer returns a string without a tab character,
strings.Index returns -1 and out[-1:] panics with
"slice bounds out of range [-1:]".

Guard against this by clamping the index to 0 when no tab is
found. In that case the entire FlagStringer output is used as
the suffix portion of the formatted string, matching the intent
of callers who use a custom stringer without the usual tab layout.

Fixes urfave#2303

Signed-off-by: alliasgher <alliasgher123@gmail.com>
When HandleExitCoder received an ExitCoder whose Error() returned an
empty string (e.g. cli.Exit("", code)), it still called
fmt.Fprintln(ErrWriter, err) which wrote a bare newline to stderr.

Guard the print with a check on the error message so that an empty
message produces no output at all.

Fixes urfave#2263

Signed-off-by: alliasgher <alliasgher123@gmail.com>
setMultiValueParsingConfig was only called from cmd.set(), which is
invoked when parsing command-line flags. When flag values came from an
environment variable they were processed in flag.PostParse() which did
not have the separator configured on the value, so the default ","
separator was used regardless of the command's SliceFlagSeparator.

Call setMultiValueParsingConfig for each flag in the PostParse loop so
the separator is configured before any env-var lookup.

Fixes urfave#2262

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split separator in StringSliceFlag ignored when using environment variable as source

1 participant