Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse required=True but --version functionality?

Tags:

In all my scripts I use the standard flags --help and --version, however I cannot seem to figure out how to make a --version with parser.add_argument(..., required=True).

import sys, os, argparse  parser = argparse.ArgumentParser(description='How to get --version to work?')  parser.add_argument('--version', action='store_true',                      help='print version information') parser.add_argument('-H', '--hostname', dest='hostname', required=True,                      help='Host name, IP Address') parser.add_argument('-d', '--database', dest='database', required=True,                     help='Check database with indicated name') parser.add_argument('-u', '--username', dest='username', required=True,                      help='connect using the indicated username') parser.add_argument('-p', '--password', dest='password', required=True,                      help='use the password to authenticate the connection')  args = parser.parse_args()  if args.version == True:     print 'Version information here'  $ ./arg.py  --version  usage: arg.py [-h] [--version] -H HOSTNAME -d DATABASE -u USERNAME -p PASSWORD  arg.py: error: argument -H/--hostname is required 

Yes, I want --hostname and others required, but I always want --version to work appropriately like --help (and -h).

$ ./arg.py  --help    usage: arg.py [-h] [--version] -H HOSTNAME -d DATABASE -u USERNAME -p PASSWORD  How to get --version to work?  optional arguments:   -h, --help            show this help message and exit   --version             print version information   -H HOSTNAME, --hostname HOSTNAME                         Host name, IP Address   -d DATABASE, --database DATABASE                         Check database with indicated name   -u USERNAME, --username USERNAME                         connect using the indicated username   -p PASSWORD, --password PASSWORD                         use the password to authenticate the connection 

Any help on getting --version to work?

like image 284
Bob Tanner Avatar asked Aug 25 '12 16:08

Bob Tanner


People also ask

What is required in Argparse?

The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. required is a parameter of the ArugmentParser object's function add_argument() . By default, the arguments of type -f or --foo are optional and can be omitted.

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.

How do you pass a Boolean in a command-line argument in Python?

The boolean value is always assigned, so that it can be used in logical statements without checking beforehand: import argparse parser = argparse. ArgumentParser(description="Parse bool") parser. add_argument("--do-something", default=False, action="store_true", help="Flag to do something") args = parser.


1 Answers

There is a special version action keyword argument to add_argument (As documented here: argparse#action).
Try this (copied from working code):

parser.add_argument('-V', '--version',                      action='version',                                         version='%(prog)s (version 0.1)') 
like image 66
Codie CodeMonkey Avatar answered Jan 03 '23 12:01

Codie CodeMonkey