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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can alter the way defaults are formatted by setting the
formatter_classoption.I’d subclass the HelpFormatter class to alter the way it formats your
choicesvalues. This class is officially an “implementation detail” but I doubt it’ll change much with newer python versions.The
_metavar_formattermethod formats the{1, 2, ..., 65535}string and your subclass could override that:Another option is to not use the
choicesargument for such a large range, and instead define a new argument type.This is just a callable, passed a string, that raises
argparse.ArgumentTypeError,TypeErrororValueErrorif 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.