Python argparse Module


argparse module handles argument parsement out of global variable sys.argv. It can also generate help and error messages if the user gives invalid arguments.

argparse module located at "Lib/argparse.py".

Let's see a file named "test.py" with code:

import argparse
ps = argparse.ArgumentParser()
ps.add_argument('--elements',help='strings to be concatenated') 
args = ps.parse_args()

Run the file from command line:
C:\> python test.py --help
usage: test.py [-h] [--elements ELEMENTS]

optional arguments:
  -h, --help           show this help message and exit
  --elements ELEMENTS  strings to be concatenated