Argparse
What is Argparse?
Argparse is a parser for command-line options, arguments and subcommands. This library makes it easy to write user-friendly command-line interfaces by:
- defines what arguments it requires
- and figuring out how to parse those out of sys.argv.
The argparse module automatically generates help and usage messages and issues errors when users give the program invalid arguments.
Getting Started
See agrparse_1.py
# import the library
import argparse
# initialize the parser
parser = argparse.ArgumentParser()
# parse arguments from sys.argv
parser.parse_args()
# run the code and ask for help
!python argparse_1.py --help
usage: argparse_1.py [-h]
optional arguments:
-h, --help show this help message and exit
Positional Arguments
See argparse_2.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name")
parser.add_argument("age")
parser.add_argument("city")
args = parser.parse_args()
print(args.name, args.age, args.city)
!python argparse_2.py --help
usage: argparse_2.py [-h] name age city
positional arguments:
name
age
city
optional arguments:
-h, --help show this help message and exit
!python argparse_2.py Ben 37 Pittsburgh
Ben 37 Pittsburgh
!python argparse_2.py Brian "??" Pittsburgh
Brian ?? Pittsburgh
Extending the help text
See argparse_3.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="the name of the person you want to find")
parser.add_argument("age", help="the age of the person you'd like to find")
parser.add_argument("city", help="the city you'd like to search")
args = parser.parse_args()
!python argparse_3.py --help
usage: argparse_3.py [-h] name age city
positional arguments:
name the name of the person you want to find
age the age of the person you'd like to find
city the city you'd like to search
optional arguments:
-h, --help show this help message and exit
Changing the default argument type
Argparse treats all arguments as strings by default. You can change the expected data type when you add each argument.
See argparse_4.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="the name of the person you want to find")
parser.add_argument("age", help="the age of the person you'd like to find", type=int)
parser.add_argument("city", help="the city you'd like to search")
args = parser.parse_args()
!python argparse_4.py Ben abc Pittsburgh
usage: argparse_4.py [-h] name age city
argparse_4.py: error: argument age: invalid int value: 'abc'
!python argparse_4.py Ben 37 Pittsburgh
Optional arguments
See argparse_5.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose",
help="increase output verbosity",
action="store_true")
args = parser.parse_args()
if args.verbose:
print("verbosity turned on")
An optional argument (or option) is (by default) given None as a value when its not being used.
- Using the –verbosity option, only two values are actually useful, True or False.
- The keyword “action” is being given the value “store_true” which means that if the option is specifed, then assign the value “True” to args.verbose
- Not specifying the option implies False.
!python argparse_5.py --help
usage: argparse_5.py [-h] [--verbose]
optional arguments:
-h, --help show this help message and exit
--verbose increase output verbosity
!python argparse_5.py --verbose
verbosity turned on
!python argparse_5.py
Short options
See argparse_6.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", help="the name of the person you want to find")
parser.add_argument("-a", "--age", help="the age of the person you'd like to find", type=int)
parser.add_argument("-c", "--city", help="the city you'd like to search")
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
args = parser.parse_args()
if args.verbose:
print(f"Searching for {args.name} {args.age} years of age in or around {args.city}")
else:
print(f"Searching for {args.name}")
!python argparse_6.py --help
usage: argparse_6.py [-h] [-n NAME] [-a AGE] [-c CITY] [-v]
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME the name of the person you want to find
-a AGE, --age AGE the age of the person you'd like to find
-c CITY, --city CITY the city you'd like to search
-v, --verbose increase output verbosity
!python argparse_6.py -n Ben -a 37 -c Pittsburgh
Searching for Ben
!python argparse_6.py --name Ben --age 37 --city Pittsburgh
Searching for Ben
!python argparse_6.py --name Ben --age 37 --city Pittsburgh --verbose
Searching for Ben 37 years of age in or around Pittsburgh