-
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
flag: Support flags after the first non-flag argument #36744
Comments
|
Change https://golang.org/cl/216378 mentions this issue: |
@ceseo I am proposing adding a new function |
The standard library's flag package presents a particular approach to command line flags. Other approaches are possible, and https://pkg.go.dev/search?q=flag turns up quite a few packages. |
I sorta expected this to be rejected... I submit that the number of flag packages and the number of times this has come up imply that while the flag design is intentional, it misses a use case that should be supported. But you gotta draw a line somewhere. The loop in the description above serves as an ok workaround if someone needs this in the future. |
@ianlancetaylor quick followup -- noting like you said that there are different approaches... would you have any interest in an additional example for the documentation, to demonstrate:
Just seeing if there is a way to improve the usability of flag consistent with the "A little copying is better than a little dependency" and "Documentation is for users" proverbs. |
Maybe? Changing the formatting of the help message could be a useful example. I'm less sure about parsing flags after arguments. But, maybe? |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, confirmed on master.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I tried to write a command line tool using
flag
where my users could appending flags to modify the command, or to get help. Ex:What did you expect to see?
I had hoped that this would be trivial to do, as it is a common pattern for option parsing.
What did you see instead?
I saw I needed to do some work to make this happen.
Per the docs and an example, flag.Parse stops at the first non-flag argument. That represents one common pattern of option parsing, which is fine, especially for subcommands. The other common pattern is what I was trying to do - recognize flags interspersed within arguments, all the way to the end.
I worked out the following loop to support this second pattern:
At the end all flags are parsed regardless of location and
args
is the remaining non-flag arguments (essentiallyflag.Args()
). I looked in the issue tracker and found having this loop might be of use to others as well.I propose adding that loop as a
ParseToEnd
function to compliment to the currentParse
behavior, so that both common patterns will be readily available.The text was updated successfully, but these errors were encountered: