Print command line arguments with argparse?

becko picture becko · Jan 25, 2016 · Viewed 34.8k times · Source

I am using argparse to parse command line arguments.

To aid debugging, I would like to print a line with the arguments which with the Python script was called. Is there a simple way to do this within argparse?

Answer

poke picture poke · Jan 25, 2016

ArgumentParser.parse_args by default takes the arguments simply from sys.argv. So if you don’t change that behavior (by passing in something else to parse_args), you can simply print sys.argv to get all arguments passed to the Python script:

import sys
print(sys.argv)

Alternatively, you could also just print the namespace that parse_args returns; that way you get all values in the way the argument parser interpreted them:

args = parser.parse_args()
print(args)