Computer Science Atlas
Snippets

Python 3: Accept Positional Command Line Arguments

June 16, 2021
 
Table of Contents

Python 3.2 or Higher Required

This article describes how to use the argparse module, which comes built into Python versions 3.2 and up.

Single Positional Argument

Let's say we want to write a program called hello.py which takes a name as a positional command line argument and says hello to that name. We want the user to be able to run the program by typing something like the following in a command line terminal:

python3 hello.py Wolfgang
$ python3 hello.py Wolfgang
Hello, Wolfgang!

How would we write hello.py?

The argparse module allows us to accept command line arguments. Here's how we can write hello.py using argparse:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('name')
args = parser.parse_args()

name = args.name
print( f'Hello, { name }!' )
1
2
3
4
5
6
7
8
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('name')
args = parser.parse_args()

name = args.name
print( f'Hello, { name }!' )

If the user runs hello.py without any name argument, then argparse displays a help message automatically for us:

python3 hello.py
$ python3 hello.py
usage: hello.py [-h] name
hello.py: error: the following arguments are required: name

The user can also run with the -h flag to display a more detailed help message:

python3 hello.py -h
$ python3 hello.py -h
usage: hello.py [-h] name

positional arguments:
  name

optional arguments:
  -h, --help  show this help message and exit

Multiple Positional Arguments

We can add additional arguments using the add_argument() function. For example, to allow the user of our example hello.py program to specify a first, middle and last name, we can write hello.py like this:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('first_name')
parser.add_argument('middle_name')
parser.add_argument('last_name')
args = parser.parse_args()

first_name  = args.first_name
middle_name = args.middle_name
last_name   = args.last_name

print( f'Hello, { first_name } { middle_name } { last_name }!' )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('first_name')
parser.add_argument('middle_name')
parser.add_argument('last_name')
args = parser.parse_args()

first_name  = args.first_name
middle_name = args.middle_name
last_name   = args.last_name

print( f'Hello, { first_name } { middle_name } { last_name }!' )

Now our program produces the following output:

python3 hello.py Wolfgang Amadeus Mozart
$ python3 hello.py Wolfgang Amadeus Mozart
Hello, Wolfgang Amadeus Mozart!

If the user only supplies a first and middle name, then argparse automatically displays the following help message:

python3 hello.py Wolfgang Amadeus
$ python3 hello.py Wolfgang Amadeus
usage: hello.py [-h] first_name middle_name last_name
hello.py: error: the following arguments are required: last_name

References