from IPython.core.display import Image
Image(url='http://labrosa.ee.columbia.edu/crucialpython/logo.png', width=600)
Notebooks are great, but sometimes you need a program that runs from the command-line. Sometimes, your program even has options! Flags! Variable-length argument lists!
What ever shall we do?
If this was C, we'd most likely use getopt(3)
to set up an argument parser. This provides the familiar style of -v
and --verbose
command-line arguments that we all know and love.
The Python analog of getopt
is ArgParse: a module that takes the pain out of constructing argument parsers. Today, we'll look through some of its basic functionality.
# First, we import the argparse module
import argparse
# Let's define a silly function to add and multiply numbers
def my_function(lower_bound, upper_bound, multiply=None, verbose=False):
value = 0
for x in range(lower_bound, upper_bound):
if verbose:
print 'value <- %3d + %3d' % (value, x)
value = value + x
if multiply:
for x in multiply:
if verbose:
print 'value <- %3d * %3d' % (value, x)
value = value * x
return value
# Let's define a function to process a list of string arguments
def process_arguments(args):
# First, construct the parser
parser = argparse.ArgumentParser(description="Adds a set of numbers, and then multiplies by an optional list")
# Let's add our first argument
parser.add_argument('-l', # Short switch
'--lower-bound', # Long switch
dest='lower_bound', # Destination in our option dict
type=int, # Type of argument we expect
default=0, # Default value
help='Lower bound for the summation' # Descriptive text
)
# And another
parser.add_argument('-u',
'--upper-bound',
dest='upper_bound',
type=int,
default=10,
help='Upper bound for the summation')
# How about optional positional arguments?
parser.add_argument(dest='multiply',
nargs='*',
type=float,
default=None,
help='One or numbers to multiply with the sum')
# A binary switch?
parser.add_argument('-v',
'--verbose',
dest='verbose',
action='store_true',
default=False,
help='verbose output')
# Finally, apply the parser to the argument list
options = parser.parse_args(args)
# And return it as a dict
return vars(options)
# A first test, use the help function:
print process_arguments(['-h'])
# If we call with no arguments
print process_arguments([])
# Or lots of arguments
print process_arguments(['-l', '3', '--upper-bound', '8', '--verbose', '-1', '2', '7'])
# To actually use this on the command line, get sys.argv[1:]
import sys
options = process_arguments(sys.argv[1:])