spf13 / pflag

Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
BSD 3-Clause "New" or "Revised" License
2.42k stars 347 forks source link

Consistent documented parsing for stringtostring #297

Open xloem opened 3 years ago

xloem commented 3 years ago

I'm having some trouble passing an element of a map to stringtostring, where the value may or may not contain equal signs, quotes, and/or commas. It seems there is no normative passing, quoting, escaping mechanism.

The documentation says the items won't be split on commas, but if they contain an equal sign, they are. Then to have the commas and quotes preserved, they must be quoted: but this won't be correct if they don't contain an equal sign, no?

Originally misfiled as https://github.com/ogier/pflag/issues/39 .

I ended up working around this by loading values from files.

cornfeedhobo commented 3 years ago

@xloem could you write this as a failing test for me? I'm not sure I fully follow.

talset commented 2 years ago

@cornfeedhobo

I think I have the same issue and it seems linked to = and new line. Here are more details to reproduce

Im using GetStringToString("field")

When running my application, If I give --field ssh_prv="$(echo -e 'foobar\nbarbla')" I correctly get "ssh_prv": (string) (len=13) "foobar\nbarbla"

If I had a = sign on one of the lines --field ssh_prv="$(echo -e 'foobar\nbar=bla')" I only got the first line as value "ssh_prv": (string) (len=6) "foobar"

I guess it comes from https://github.com/spf13/pflag/blob/master/string_to_string.go#L35 It try to read the value as CSV, but having multiple lines does not help

Should we consider adding something like

if strings.Contains(val,"\n"){
    ss = append(ss, strings.Trim(val, `"`))
} else {
    r := csv.NewReader(strings.NewReader(val))
    ....
}

?

lllamnyp commented 8 months ago

@xloem could you write this as a failing test for me? I'm not sure I fully follow.

See this line of code.

Consider the result if

vals := map[string]string{"a=b": "c=d"} // not to be confused with {"a": "b", "c": "d"}

or similar.