Using argument parser for Python scripts

It’s often easy to run your Python scripts from the command line. Sometimes, your scripts may require a form of input to run- here’s a two-minute recipe to use the ArgumentParser module in Python to allow you to enter these inputs like the following:

$ python script.py --input <input>
# can use input in script

And then using these inputs in your program. Easy/quick documentation for these things can be hard to come by, so I’m making some notes here for reference.

  1. First, install the argparse module- this is built in for most standard versions of Python, but run this to be sure you have it.
$ pip install argparse
  1. Say we wanted to use argparse to input a string value of someone’s name, for our Python script hello.py that says hello and happy birthday to an input person for an input age:
name = 'Monty'
print ('Happy ' + str(age) + 'th' + ' Birthday ' + name + '!')
# Ex: Happy 20th Birthday John!  for name = John, age = 20. 
  1. First, we import the argparse model and instantiate an object:
import argparse 


ap = argparse.ArgumentParser(description='Greet the user')
# Optional: put in a description for your script 
  1. Next, we add the argument, ‘name’, to our argparse object with the add_argument function with the following parameters:
ap.add_argument("-n", "--name", type=str, default= "Shamikh",
        help= "Name of person to greet. ex: Shamikh")

ap.add_argument("-a", "--age", type=int, default= 19,
        help= "Age of person to greet. ex: 19")

If the user types

python script.py --help 

They will see:

usage: script.py [-h] [-n NAME] [-a AGE]

Script to greet and wish happy birthday.

optional arguments:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  Name of person to greet. ex: Monty
  -a AGE, --age AGE     Age of person to greet. ex: 19

Sweet! Now how do we use these values into our program? If we use the parse_args() function, the entered inputs can be loaded into a python dictionary.

args = vars(ap.parse_args())

# we can now access the inputs with their reference: 
name = args['name']
age = args['age']
print ('Happy ' + str(age) + 'th' + ' Birthday ' + name + '!')

So, for a test run (here’s the full code):

import argparse 


ap = argparse.ArgumentParser(description = 'Script to greet and wish happy birthday.')

ap.add_argument("-n", "--name", type=str, default= "Shamikh",
        help= "Name of person to greet. ex: Shamikh")

ap.add_argument("-a", "--age", type=int, default= 19,
        help= "Age of person to greet. ex: 19")

args = vars(ap.parse_args())

print ('Happy ' + str(args['age']) + 'th' + ' Birthday ' + args['name'] + '!')

And here’s the output of running the script in Terminal:

$ python script.py -name Monty -age 100
Happy 100th Birthday Monty!