-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Inconsistent/wrong list arg parsing #239
Comments
Thanks for these James. Here are my thoughts: First casego run parsingbug.go --some-list list1 sub input At the moment, list arguments without equals signs consume all remaining tokens until encountering one that starts with a hyphen. So in your first example, yes do_something | xargs ./myprogram --some-list then it could be confusing if there are certain tokens (e.g. the string "sub" in your example) that could be generated by "do_something" and would change the way the arguments are parsed. Now it's already the case that tokens that start with a hyphen do change the way the arguments are parsed (they terminate the consumption of tokens), but that at least doesn't depend on the particularities of the args struct being parsed into, but rather is consistent no matter what your args struct looks like. If you want to put a list of tokens on the command line and then continue with more tokens afterwards, then it seems to me that it would be best to use the equals sign, as in Second casego run parsingbug.go --some-list=list1 sub input In the case that you use an equals sign with a list argument, as in Third casego run parsingbug.go --some-list list1 --some-list list2 sub input In this case, go-arg encounters two values for ./myprogram --value 1 --value 2 --value 3 The above will end up with "3" parsed into the struct field corresponding to Fourth casego run parsingbug.go --some-list list1 --some-list list2 --some-list=list3 sub input The reason you're getting the behavior that you are in this case is due to the same "overwrite" behavior that is behind the previous case. The library finds three separate values for Options for v2What if anything should we do to avoid this complexity in v2? One possibility would be to remove support for space-separated list arguments. This would reduce complexity a lot but space-separated list arguments are widely used on the command line and they are nice. A second possibility would be to throw an error if the same option appears twice on the command line. We could have a struct tag that explicitly re-enables this behavior if someone really wants it. This could make the whole situation less confusing. We could add documentation about all of these cases so that at least people can get a good explanation of how it all works. We could also make it so that subcommands terminate the consumption of tokens going into a list argument, but I still don't like this option. It seems to me the best approach for v2 would be to throw an error when an option appears twice, and then give some really good documentation about how subcommands, equals signs, and list arguments interact together, with a nice table of examples. |
Thank you for your dedicated and thoughtful reply.
Assuming I'm parsing this correctly, and you mean things like
I think this would be a great idea. Either that or allowing PS: if you're curious, here is my WIP branch porting to go-arg. It will likely get merged as-is, unless of course you'd like to comment. purpleidea/mgmt@54456ff Of interesting note, here is a workaround I added to help users who might accidentally hit this issue: purpleidea/mgmt@54456ff#diff-e59db0c8589637e59150ffb46afed207139f3273d37db6fa95260914254450d7R82 The same workaround for a more complicated sub-command is shown here: purpleidea/mgmt@54456ff#diff-d8b7e73d77fa79e1493c5173dbf6914d4048b87c83ae9edbb97ca3bd5593bc47R124 Thanks again for the nice library. You've got me motivated that CLI libraries are cool. If I was using regexp for anything I'd want to use your clever regexp library too! Cheers |
It seems the parsing of lists is done differently than what is common in some other parsing libraries like urfave/cli. I detected this because some tests in https://github.com/purpleidea/mgmt/ started failing after a direct port to this lib. I investigated the behaviour and I believe what we are doing isn't optimal or correct. Minimally I think it's inconsistent or surprising.
Overall the library is great though! I'm happy to find such issues and report.
TL;DR: extra positional args are "chomped" up when we have a
[]string
arg or similar. For example:Note how
sub
is part of the list:If we add the equals symbol this works around this:
Strange things happen with two list args. Where did list1 go?
Three specifiers gives something perhaps inconsistent if the last one has an equals:
The below code for this works all by itself:
I'll be happy to test and then convert this into a PR of test cases if someone wants to prepare a patch.
Cheers!
The text was updated successfully, but these errors were encountered: