I use argparse to deal with input parameters, and it outputs the following with parser.print_help():
optional arguments: -h, --help show this help message and exit -t TEMPLATES, --templates TEMPLATES template names to make, should be defined as section name in conf, and have related file in templates/ folder -c CONFPATH, --confpath CONFPATH configuration path for template detail info
my code looks like the following:
import argparse parser = argparse.ArgumentParser(prog='base_maker', description='template maker') parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str) parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))
However, I want to add a detailed example about how to use -t/--template, like (added in the example part):
optional arguments: -h, --help show this help message and exit -t TEMPLATES, --templates TEMPLATES template names to make, should be defined as section name in conf, and have related file in templates/ folder -c CONFPATH, --confpath CONFPATH configuration path for template detail info example: python test.py -t template/test.py python test.py -t template/test -c conf/test.conf python test.py -t test.py
I don't know which attribute should be used to add "example" part, I check Print program usage example with argparse modulen, but it's unclear and without detailed example when I check epilog in the official doc.
Could anyone give me a example about how to achieve that? Thanks
To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
You can use the argparse module to write a command-line interface that accepts a positional argument. Positional arguments (as opposed to optional arguments, which we'll explore in a subsequent section), are generally used to specify required inputs to your program.
>>> parser = argparse. ArgumentParser(description='Process some integers. ') The ArgumentParser object will hold all the information necessary to parse the command line into Python data types.
You need to use the epilog and the formatter_class arguments to ArgumentParser if you want to have the help the example printed at the end (epilog) and to preserve the whitespace/formatting (formatter_class set to RawDescriptionHelpFormatter).
Example by modifying your example above:
import argparse example_text = '''example: python test.py -t template/test.py python test.py -t template/test -c conf/test.conf python test.py -t test.py''' parser = argparse.ArgumentParser(prog='base_maker', description='template maker', epilog=example_text, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str) parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))
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