Search notes:

Python standard library: argparse

The argparse library is used to parse command line arguments given to a Python script.

Simple example

The following simple script (simple.py) tries to demonstrate how argparse might be used.
The script defines 4 possible arguments -flag, -number, -text and -req.
action = 'store_true' makes -flag a boolean like argument which does not require an explicit value to be set. When simple.py is invoked, either -flag is present or not.
-number is specified to accept only an int. If the script is invoked without using this argument, the corresponding value will be None.
-text accepts a string (str). If this argument is not specified, the corresponding value will be the specified default value Hello world.
-req also accepts an int. Its required = True also mandates that this argument must be used when called.
argparse also adds the implicit argument -help which, when used, prints the command line arguments.

simple.py

This is the source code for this example
import argparse

argParser = argparse.ArgumentParser()

argParser.add_argument('-flag'   , action = 'store_true', help = 'Specify a true/false option'                          )
argParser.add_argument('-number' , type   =  int        , help = 'What is the number'                                   )
argParser.add_argument('-text'   , type   =  str        , help = 'What is the text'           , default  = 'Hello world')
argParser.add_argument('-req'    , type   =  int        , help = 'A required argument'        , required =  True        )

parsedArgs = argParser.parse_args()

if parsedArgs.flag:
   print('You specified the flag')
else:
   print('You did not specify the flag')

print('The number is ' + str(parsedArgs.number))
print('The text   is ' +     parsedArgs.text   )
print('req        is ' + str(parsedArgs.req   ))

Some invocations

Using -h to summarize the possible arguments. The […] braces indiciate optional arguments. Thus, it is visually clear that -req is a required argument. -flag is shown to accept no parameter while -number and -text accept exactly one parameter:
/home/rene> ./simple.py -h
usage: simple.py [-h] [-flag] [-number NUMBER] [-text TEXT] -req REQ

optional arguments:
  -h, --help      show this help message and exit
  -flag           Specify a true/false option
  -number NUMBER  What is the number
  -text TEXT      What is the text
  -req REQ        A required argument
/home/rene> ./simple.py -num 18
usage: simple.py [-h] [-flag] [-number NUMBER] [-text TEXT] -req REQ
simple.py: error: the following arguments are required: -req
No value specified for -text, defaults to Hello world:
/home/rene> ./simple.py -num 18 -req 99
You did not specify the flag
The number is 18
The text   is Hello world
req        is 99
No value specified for -number, defaults to None
/home/rene> ./simple.py -flag  -req 99
You specified the flag
The number is None
The text   is Hello world
req        is 99
Using the wrong data type for an argument:
/home/rene> ./simple.py -flag  -num forty-two -req 99
usage: simple.py [-h] [-flag] [-number NUMBER] [-text TEXT] -req REQ
simple.py: error: argument -number: invalid int value: 'forty-two'
You specified the flag

See also

sys.argv
Related libraries are
standard library

Index