-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: flag: function to add long and short version of a flag #40138
Comments
This can be done using the var frobVal int
func init() {
flag.IntVar(&frobVal, "frob", 0, "set the frob value")
flag.IntVar(&frobVal, "f", 0, "set the frob value")
} |
there are 2 problems with that. First, most of that code is the exact same. Second, using If there was a function like the one I'm proposing, the output would be more like: |
OK, we can think about it. Do you have a suggested API? |
I don't have any real ideas for function names, but the should probably be the same as current functions (with a normal and Var form) but with 2 that's just my idea, and I'd be open to hearing any better ideas anyone has. |
I think something like var frobVal int
func init() {
flag.IntVar(&frobVal, "f", 0, "set the frob value")
flag.Synonym("f", "frob") // displayed in this order in usage message
} |
See https://github.com/rsc/getopt for an example of what you can build on top of the flag package as-is. |
Yeah, this seem like it would be a better way to do this, as it wouldn't require doubling the amount of functions in the package. |
I think long and short flags can be added easily with backwards compatibility by extending the name argument of the flags. Currently, the name argument can be anything, but in practice it's just a word, since having a whitespace in the name is unusable. flag.IntVar(&frobVal, "frob f", 0, "set the frob value") This way, you won't get a second entry in flag.PrintDefaults, and no new API surface will need to be introduced. |
I do not think its worth it trying to squeeze it into the current function signature. I do not see any current restrictions on the name string in the flags package. Are we sure non-ASCII whitespace is currently not usable in Flag names? |
Setting up a flag with a whitespace in it currently produces the following help text:
However, if you try to use what the help text displays
Instead, one would have to invoke it as |
While this likely wouldn't break anything in practice, it would technically be a breaking change, and the benefit of slightly less typing doesn't justify the cost.
Also, the flag could be specified with |
This is a duplicate of #35761. Please see the discussion there about why |
@rsc what do you mean by "this part" and "the other part"? |
@lolbinarycat |
Sorry for the confusion, "this part" was supposed to be "part". |
Based on the discussion above (in particular it being a duplicate of #35761 and half-impossible), this seems like a likely decline. |
No change in consensus, so declined. |
Many programs have command line options that can be specified in either a long or short form.
The most common example would be
--help
and-h
(which are automaticaly included withflag
).As the package currently exists, there is no real good way to define other flags that have this behavior.
The text was updated successfully, but these errors were encountered: