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

Documentation for back-quoted flag usage #2125

Open
calebstewart opened this issue Mar 16, 2024 · 4 comments · May be fixed by spf13/cobra.dev#23
Open

Documentation for back-quoted flag usage #2125

calebstewart opened this issue Mar 16, 2024 · 4 comments · May be fixed by spf13/cobra.dev#23

Comments

@calebstewart
Copy link

calebstewart commented Mar 16, 2024

This may be better suited in the spf13/pflag project, but my expectation is that more people use Cobra than pflag directly. I was recently using Cobra to parse a custom repeated argument in the form --option KEY=VALUE . When using .StringArray(), the usage looks like this by default:

Flags:
  --option stringArray     My usage information

This isn't terrible, but "stringArray" is not helpful in this context. I went digging in the documentation and code and ended up finding the UnquoteUsage function. It seems that providing a usage like:

cmd.Flags().StringArray("option", nil, "Extra `KEY=VALUE` options")

Will result in the following usage:

Flags:
  --option KEY=VALUE    Extra KEY=VALUE options

This is exactly what I wanted, however there's no documentation anywhere that I could find.

If this is better opened over in pflag, let me know, and I can reopen over there. If I've missed where this functionality is documented, then no worries. I just wanted to surface it to help someone else in the future. :)

@marckhouzam
Copy link
Collaborator

Believe it or not I also actually found out about this a few weeks ago! So I agree that it would be nice to document in cobra. Also, shell completion which automatically provides descriptions of flags does not respect this format.

@calebstewart I encourage you to submit a PR for the documentation. And if you want, a PR for shell completion would also be appreciated

@cubxxw
Copy link
Contributor

cubxxw commented Apr 8, 2024

Come on!

@nirs
Copy link
Contributor

nirs commented May 3, 2024

I learned about this today reviewing #2105 :-)

Shouldn't this be documented in the pflags package?

@calebstewart
Copy link
Author

I apologize for losing track of this issue. It seems from the discussion, and after reading back over everything that it makes the most sense to add this documentation to spf13/pflag and spf13/cobra.dev (to get it to the website docs). That is what I did above. I'm open to any tweaks to that documentation, but I at least took a stab at it.

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 a pull request may close this issue.

4 participants