Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent help output from escaping default values as go strings #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preempting a similar issue here potentially.

}
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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preempting a similar issue here potentially.

}

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