Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse add example usage

Tags:

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

like image 212
linpingta Avatar asked Nov 04 '16 09:11

linpingta


People also ask

How do you add an optional argument in Argparse?

To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.

Where is Argparse used?

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.

What does Argparse ArgumentParser () do?

>>> 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.


1 Answers

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')) 
like image 65
c3st7n Avatar answered Oct 05 '22 15:10

c3st7n