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

backslashes in default string values get doubled up #1396

Closed
06kellyjac opened this issue May 11, 2021 · 6 comments
Closed

backslashes in default string values get doubled up #1396

06kellyjac opened this issue May 11, 2021 · 6 comments
Labels
area/docs-generation Generation of docs via Cobra good-first-issue Issues that a new contributor could make a PR for kind/bug A bug in cobra; unintended behavior lifecycle/needs-pr Ready for a PR from the community

Comments

@06kellyjac
Copy link

If you have a string with backslashes (correctly escaped to be a literal \) if it's the cobra default value it outputs with extra slashes, presumably for escaping.

flags.StringVar(&opts.something, "the-flag", "ping\\pong", "The description")
outputs with an extra slash:
--the-flag string The description (default "ping\\pong")

fmt.Sprintf("my string: %s", "ping\\pong")
outputs the correct amount of slashes:
my string: ping\pong

Is there a reason for the extra slashes? or is this unintended?

I tried looking for old issues but I couldn't find anything

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@06kellyjac
Copy link
Author

Still important

@johnSchnake
Copy link
Collaborator

Reproduced and agree it should print the single slash.

@johnSchnake johnSchnake added kind/bug A bug in cobra; unintended behavior good-first-issue Issues that a new contributor could make a PR for lifecycle/needs-pr Ready for a PR from the community area/docs-generation Generation of docs via Cobra and removed kind/stale labels Mar 16, 2022
@johnSchnake
Copy link
Collaborator

Just spent another minute looking at this and that I think it is caused from the pflag library here https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/flag.go#L733

The %q is what causes it to be a go-escaped string rather than just using "%v"; go fmt package states that %q for strings as:

%q	a double-quoted string safely escaped with Go syntax

So I think we need to put a PR up for that. I'll create an issue there to track it.

@johnSchnake
Copy link
Collaborator

PR: spf13/pflag#347
Going to close this issue since I've created the other issue/PR and there isn't anything to track here.

@rohitramu
Copy link

I'm still seeing this issue. Looks like spf13/pflag#347 still hasn't been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs-generation Generation of docs via Cobra good-first-issue Issues that a new contributor could make a PR for kind/bug A bug in cobra; unintended behavior lifecycle/needs-pr Ready for a PR from the community
Projects
None yet
Development

No branches or pull requests

3 participants