feat: interpret demand() numbers as relative to executing command #582
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.
Fixes #451.
Previously, min and max values passed to
.demand()
were interpreted as the total number of non-option arguments, which included the arguments representing the command(s) itself.With this PR, the min and max values are now interpreted as the additional number of non-option arguments, relative to the currently executing command.
Here's an example:
Before this PR, the command would not work as expected (i.e. would not accept any flagless args) because the
lint
command itself would satisfy the min and max on the command line.After this PR, the command works by requiring a single flagless arg on the command line besides the
lint
command itself.When
.demand()
is used outside of a command builder, the functionality remains the same.Note that this is a BREAKING CHANGE since users relying on the previous (now deemed incorrect) functionality will need to change the values they pass to
.demand()
within command builders, decrementing them by the "nested/depth count" of the command.Also note that this does not change the contents of
argv._
within the command handler;argv._
still contains the command key(s) it took to execute the command(s). (Note the use ofargv._[1]
in the above example.)We could possibly change this (which would be another breaking change), but I'd rather leave it this way to potentially support command handlers that are reusable across multiple commands (allowing the handler to slightly change its functionality based on the command keys present in
argv._
).