Skip to content

Commit

Permalink
Prevent help output from escaping default values as go strings
Browse files Browse the repository at this point in the history
When creating the help text for a flag, the default value shouldn't be
be excaped. It isn't clear to an end user that we would be escaping those values.

Instead we should be printing the actual value and letting the users decide
when/how to add escaping based on how they're utilzing it.

Fixes spf13#346

Signed-off-by: John Schnake <jschnake@vmware.com>
  • Loading branch information
johnSchnake committed Mar 16, 2022
1 parent d5e0c06 commit 1833377
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (f *FlagSet) ArgsLenAtDash() int {
func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error {
flag := f.Lookup(name)
if flag == nil {
return fmt.Errorf("flag %q does not exist", name)
return fmt.Errorf(`flag "%v" does not exist`, name)
}
if usageMessage == "" {
return fmt.Errorf("deprecated message for flag %q must be set", name)
Expand Down Expand Up @@ -475,7 +475,7 @@ func (f *FlagSet) Set(name, value string) error {
} else {
flagName = fmt.Sprintf("--%s", flag.Name)
}
return fmt.Errorf("invalid argument %q for %q flag: %v", value, flagName, err)
return fmt.Errorf(`invalid argument "%v" for "%v" flag: %v`, value, flagName, err)
}

if !flag.Changed {
Expand Down Expand Up @@ -730,7 +730,7 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
line += usage
if !flag.defaultIsZeroValue() {
if flag.Value.Type() == "string" {
line += fmt.Sprintf(" (default %q)", flag.DefValue)
line += fmt.Sprintf(` (default "%v")`, flag.DefValue)
} else {
line += fmt.Sprintf(" (default %s)", flag.DefValue)
}
Expand Down
2 changes: 2 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ const defaultOutput = ` --A for bootstrapping, allo
--custom custom custom Value implementation
--customP custom a VarP with default (default 10)
--maxT timeout set timeout for dial
--slash string a string with a slash (default "a\slash")
-v, --verbose count verbosity
`

Expand Down Expand Up @@ -1228,6 +1229,7 @@ func TestPrintDefaults(t *testing.T) {
fs.StringSlice("StringSlice", []string{}, "string slice with zero default")
fs.StringArray("StringArray", []string{}, "string array with zero default")
fs.CountP("verbose", "v", "verbosity")
fs.String("slash", "a\\slash", "a string with a slash")

var cv customValue
fs.Var(&cv, "custom", "custom Value implementation")
Expand Down

0 comments on commit 1833377

Please sign in to comment.