-
Notifications
You must be signed in to change notification settings - Fork 479
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
Fix #600 - better handling of --help and --version options #608
Open
rmunn
wants to merge
15
commits into
commandlineparser:develop
Choose a base branch
from
rmunn:feature/autohelp-rewrite
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This will let us parse -vvv and turn that into "Verbose=3".
Most tests pass; a couple of tests were either not needed, or were testing semantics that change with the new implementation (e.g., spaces in option values are allowed now).
Pattern matching is a particularly good fit here, as each case is data-driven and the flow is easy to read top-to-bottom.
The short and long option code can move into separate functions, which makes the big switch statement much easier to read. Also remove one TODO comment that's not relevant to the current feature.
8e3e1e8
to
33fc02f
Compare
I've rebased this PR on top of the new version of #607. It's ready to merge now. |
Double dash (--) should end option sequences, but should have no effect on value sequences. These unit tests reflect that design.
Now things like -vvv for triple-verbose can be done in user code.
Rebasing again since #607 has had some changes, in particular it now has a working implementation (and a unit test) for the FlagCounter attribute added to Options. |
Next we'll implement the feature.
These default to false and determine whether the automatically-added "--help" and "--version" parameters will have "-h" and "-V" shortnames. The shortnames, like the longnames, are NOT configurable.
The --help option should be recognized anywhere it appears in the command line, according to the GNU getopt standard, and if it appears, it should override any other processing of arguments -- which includes returning no other parsing errors. Ditto for --version.
The tests were already written, but the AutoHelpShortName and AutoVersionShortName parts were commented out because otherwise the tests wouldn't compile. Now the tests compile, and pass.
Ensure that UnknownOptionError is still generated for --help option when AutoHelp is off.
Ensure that UnknownOptionError is still generated for --version option when AutoVersion is off.
33fc02f
to
aa680a2
Compare
This was referenced Aug 19, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds on #607 to fix #600. Now that string values are handled by the tokenizer exactly how getopt handles them, we can also handle the special
--
,--help
and--version
options exactly how getopt handles them. In particular, we can respond to--help
no matter where it appears in the argument list (the previous behavior would only respond to--help
if it was the first parameter, and had some hacks that tried to ensure that it would be responded to elsewhere, but those hacks were incomplete and buggy, creating issues like #596 among others).Note that because I wrote this PR on top of #607, merging this PR will automatically merge #607 as well. I did it this way because proper tokenization of
--help
(and NOT handling it if it was a string value to some other option) is essential to how this PR is supposed to work. Without #607, many of the tests I wrote for the AutoHelp and AutoVersion options would fail.