I'm using argparse to take in command line input and also to produce help text. I want to use ArgumentDefaultsHelpFormatter
as the formatter_class
, however this prevents me from also using RawDescriptionHelpFormatter
which would allow me to add custom formatting to my description or epilog.
Is there a sensible method of achieving this aside from writing code to produce text for default values myself? According to the argparse docs, all internals of ArgumentParser
are considered implementation details, not public API, so sub-classing isn't an attractive option.
nargs stands for Number Of Arguments.
Optional Arguments To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.
I just tried a multiple inheritance approach, and it works:
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): pass parser = argparse.ArgumentParser(description='test\ntest\ntest.', epilog='test\ntest\ntest.', formatter_class=CustomFormatter)
This may break if the internals of these classes change though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With