-
Notifications
You must be signed in to change notification settings - Fork 1.1k
argparse: Add support for custom argument types. #1064
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
Conversation
|
I'd suggest to make this as much CPython-compatible as possible. It has this particular behavior: https://docs.python.org/3/library/argparse.html#type
So in CPython the resulting behavior is this: |
5c007f4 to
13541ff
Compare
Thanks for spotting that! I've made it more CPython-compatible, at the expense of an increased footprint by 100 more bytes. The case you mentioned should be handled, along with the exception handling limitations of the custom type parser (ie. let anything that's not Right now the main differences between this and CPython should be the lack of type registration, and |
Yeah should be fine. Also because those would lead to an error immediately, whereas the type/default thingie is harder to discover and could lead to surprising results. |
6936a30 to
2859d02
Compare
python-stdlib/argparse/argparse.py
Outdated
| except Exception as e: | ||
| if isinstance(e, (ArgumentTypeError, TypeError, ValueError)): | ||
| raise _ArgError("invalid value for %s: %s (%s)" % (optname, arg, str(e))) | ||
| raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about writing this as more idiomatic Python:
except (ArgumentTypeError, TypeError, ValueError) as e:
raise _ArgError(...)I think that's equivalent? It also saves 12 bytes in the resulting .mpy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this syntax wasn't supported by MicroPython, good to know I was wrong all along :)
It is indeed equivalent. Thanks for pointing this out, I've changed the code to reflect that.
2859d02 to
df811c0
Compare
This commit adds support for optional custom argument type validation to
argparse.ArgumentParser, allowing for shorter argument validation code
for both simple builtins and complex types.
For example, assuming that a particular command line argument must be an
integer, using "parser.add_argument('-a', type=int)" will make sure that
any value passed to that argument that cannot be converted into an
integer will trigger an argument validation error.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
df811c0 to
7473d1d
Compare
| @@ -1,4 +1,4 @@ | |||
| metadata(version="0.4.0") | |||
| metadata(version="0.5.0") | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I bumped the minor version, because this change is adding a feature.
This PR adds support for optional custom argument type validation to argparse.ArgumentParser, allowing for shorter argument validation code for both simple builtins and complex types, as supported by CPython.
CPython also provides the
argparse.FileTypeclass to simplify argument file/directory handling, but that was not added to the MicroPython module. Its behaviour is rather easy to manually replicate if needed anyway.This change increases the size of the
argparsemodule by 291 bytes, and that's mostly due to the extra error handling and related text strings. Probably those new strings can be shortened, but given that this module isn't usually included in production code, the size increase shouldn't hurt too much I guess.The unit tests unfortunately do not cover the case of a parameter failing validation, as the module will automatically issue a
sys.exitcall on error, after printing the relevant exception message. If there are cleaner ways to test that without hijackingsys.exit, let me know so I can update the tests accordingly.