Add some useful argument types #1522
Merged
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.
Hi! I've made a collection of a few utility argument types that I've found useful over the years. I think it would be useful to have them in
cmd2
itself so users can find them easily and not re-invent this wheel.integer
Usage
Failures
Limitations
Because the base can be specified, Python does not allow a leading zero. This avoids confusion where a user might expect the "C" behavior of that specifying an octal value, or expect the "normal" behavior of ignoring the leading zero.
The default
int
constructor only uses decimal, so allows the leading zero.Notes
It is tempting to allow a "B" after the suffixes, since my typical use case has been specifying sizes in bytes, but I suspect there would be other similar special cases.
hexadecimal
Usage
Failures
Note
We could avoid having a standalone function for this, and instead the user could use:
Unfortunately, the error text then becomes
Similarly for other one-liner strategies using partial evaluation.
Range
Usage
Failures
Notes
type=int, choices=range(5)
works well, but it becomes unwieldy for large ranges of valid values.range
built-in), and is compact.IntSet
Allows the user to specify multiple values from within a range, in a flexible format.
Usage
Errors
Notes
yield
/yield from
to return an iterable without constructing a list. Unfortunately, then the value checks are only performed when the iteration is done by the user code, not when the argument is being parsed. So we would need another function/generator to do the yielding, returned by this function after the parsing. It's not that much extra code, but it seems like an unlikely corner case that the user would be specifying a range so large that this matters.set
, and there is no uniqueness check on the values. But I don't really likeIntList
any more thanIntSet
.__repr__
, we could aim for error text likeError: argument value: invalid set of values from [0..99] value:'100'
but I was trying to stay a little closer to the spirit of__repr__
P.S.
Thanks for all the work on cmd2! I've used it several times over the years for internal tools, and users are always so impressed with the polish (esp. help and tab completion). I've glued in
rich
in the past and have been looking forward to 3.0, I think that'll be great.