Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python,argparse: how to have nargs=2 with type=str and type=int

Tags:

People also ask

What does Nargs do in Argparse?

Number of Arguments If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.

How do you make an argument optional in Argparse?

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

What is action =' Store_true in Python?

The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.


I spent some times on the argparse documentation, but I'm still struggling with this module for one option in my program:

parser.add_argument("-r", "--rmsd", dest="rmsd", nargs=2,     help="extract the poses that are close from a ref according RMSD",     metavar=("ref","rmsd")) 

I'd like to the first argument to be a string (type str) and mandatory, while the second argument should have type int, and if no value is given have a default one (let's say default=50). I know how to do that when there is only one argument expected, but I have no idea how to proceed when nargs=2... Is that even possible?