I'm trying to pass a list of arguments with argparse but the only way that I've found involves rewriting the option for each argument that I want to pass:
What I currently use:
main.py -t arg1 -a arg2
and I would like:
main.py -t arg1 arg2 ...
Here is my code:
parser.add_argument("-t", action='append', dest='table', default=[], help="")
Being aware, you asked for argparse solution, I would like to present alternative solution using package
docopt
Install it first:
Write the code:
Run it to show usage instrucitons:
Call it with your parameters:
Btw. if you do not need the
-a
option, you shall directly allow passing the arguments. It makes usage simpler to the user.Use
nargs
:For example, if
nargs
is set to'+'
So, your code would look like
That way
-t
arguments will be gathered intolist
automatically (you don't have to explicitly specify theaction
).