feat: space separated option values - #73
Merged
Merged
Conversation
An option a command declares in its option definitions with a type other than "flag" now takes the next token as its value, so -o value and --opt value work next to --opt=value. Options with no definition keep returning true and leave the next token in the arguments. The tokens are kept after prepare() so the options can be parsed again in dispatch(), once the command and its definitions are known. A token that is a negative number, like -5, is now an argument instead of a set of boolean options, so it no longer needs the -- marker.
This file contains hidden or 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
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.
Closes #48.
-o valueand--opt valuenow carry a value, next to the--opt=valueform that already worked.How it works
The option definitions decide it: an option a command declares in
$optionDefinitionswith a type other thanflagtakes the next token as its value, when that token is not itself an option or the--marker. Options with no definition keep the old behaviour, so-abcstill expands to three boolean flags and nothing that works today changes.Since the definitions belong to the command, the parse happens twice:
prepare()parses as before and keeps the tokens, thendispatch()parses them again once the command is resolved. The console wide options are re-applied after that second pass, so--quietand--no-ansikeep working.In a short group only the last option can take a value, as in
-vf config.php, which is what getopt style parsers do.Negative numbers
The edge case from the issue is fixed too: a token that is numeric after the dash, like
-5or-1.5, is an argument now instead of a set of boolean options, so it no longer needs the--marker. It is also accepted as an option value, as in--offset -5.Tests
tests/OptionValueTest.phpcovers the short and long form, the group form, the negative numbers, and the cases that must not change: undefined options,flagoptions, an option followed by another option, and the--marker. Full suite is green (128 tests), PHPStan and the coding standard are clean.