Python's argparse to show program's version with prog and version string formatting Python's argparse to show program's version with prog and version string formatting python python

Python's argparse to show program's version with prog and version string formatting


Yes, that's the accepted way. From http://docs.python.org/dev/library/argparse.html#action:

>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')

You should of course be embedding the version number in your package in a standard way: Standard way to embed version into python package?

If you're following that method, you have a __version__ variable:

from _version import __version__parser.add_argument('--version', action='version',                    version='%(prog)s {version}'.format(version=__version__))

For example, that's the method demonstrated at https://pypi.python.org/pypi/commando/0.3.2a:

parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)