Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: argparse taking a list of variable size

In Python, using argparse, I want an input argument to take a variable number of files, like:

$ myScript --aParameter file1 file2 file3 ... fileN

How can do it?

parser.add_argument( "--aParameter", nargs=????, type=str,
                        help="Provide a list of files to analyze",
                        default=None)
like image 878
Ricky Robinson Avatar asked Apr 29 '13 14:04

Ricky Robinson


People also ask

What does Nargs mean in Python?

nargs stands for Number Of Arguments.

What does Argparse ArgumentParser () do?

ArgumentParser() initializes the parser so that you can start to add custom arguments. To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required .

What is Metavar in Argparse Python?

Metavar: It provides a different name for optional argument in help messages.


1 Answers

Use the kwarg nargs='+'. That's pretty much all there is to it.

like image 174
wim Avatar answered Sep 24 '22 21:09

wim