I’d like to be able to specify different verbose level, by adding more -v options to the command line. For example:
$ myprogram.py
$ myprogram.py -v
$ myprogram.py -vv
$ myprogram.py -v -v -v
would lead to verbose=0, verbose=1, verbose=2, and verbose=3 respectively. How can I achieve that using argparse?
Optionally, it could be great to also be able to specify it like
$ myprogram -v 2
You could do this with
nargs='?'(to accept 0 or 1 arguments after the-vflag) and a custom action (to process the 0 or 1 arguments):Running
script.py -v -vfrom the command line yieldsUncomment the print statement to see better what the
VActionis doing.