argparse add example usage

Question:

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

Asked By: linpingta

||

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'))
Answered By: c3st7n
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.