Currently I want Python's argparse module to only print out '1 - 65535' rather than {1, 2, 3, ... 65535}, but the documentation doesn't seem to provide any method for this. Any suggestions?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can alter the way defaults are formatted by setting the
formatter_class
option.I'd subclass the HelpFormatter class to alter the way it formats your
choices
values. This class is officially an "implementation detail" but I doubt it'll change much with newer python versions.The
_metavar_formatter
method formats the{1, 2, ..., 65535}
string and your subclass could override that:Another option is to not use the
choices
argument for such a large range, and instead define a new argument type.This is just a callable, passed a string, that raises
argparse.ArgumentTypeError
,TypeError
orValueError
if the string cannot be converted to the target type, or the converted value otherwise:You can use this as the argument type like this:
and adjust your help message to indicate what values are acceptable.