Parsing Command Line arguments

Parsing Command Line arguments allow both the user and other programs to run the Python file passing it data as it starts. Learn more here.

Most command line tools rely on arguments passed to the program upon its execution. Instead of prompting for input, these programs expect data or specific flags (which become booleans) to be set. This allows both the user and other programs to run the Python file passing it data as it starts. This section explains and demonstrates the implementation and usage of command line arguments in Python.

Parsing Command Line arguments: Hello world in argparse

The following program says hello to the user. It takes one positional argument, the name of the user, and can also be told the greeting.

import argparse
parser = argparse.ArgumentParser()
help='name of user'
parser.add_argument('-g', '--greeting',
help='optional alternate greeting'
args = parser.parse_args()
print("{greeting}, {name}!".format(
$ python --help
usage: [-h] [-g GREETING] name

positional arguments:

name of user

optional arguments:
-h, --help show this help message and exit -g GREETING, --greeting GREETING
optional alternate greeting
python world Hello, world!
python John -g Howdy Howdy, John!

For more details please read the argparse documentation.

Using command line arguments with argv

Whenever a Python script is invoked from the command line, the user may supply additional command line arguments which will be passed on to the script. These arguments will be available to the programmer from the system variable sys.argv (“argv” is a traditional name used in most programming languages, and it means “argument vector”). – Python® Notes for Professionals

By convention, the first element in the sys.argv list is the name of the Python script itself, while the rest of the elements are the tokens passed by the user when invoking the script. import sys print(sys.argv)
python => ['']
python fizz => ['', 'fizz']
$ python fizz buzz
=> ['', 'fizz', 'buzz']

Here’s another example of how to use argv. We first strip off the initial element of sys.argv because it contains the script’s name. Then we combine the rest of the arguments into a single sentence, and finally print that sentence prepending the name of the currently logged-in user (so that it emulates a chat program).

import getpass
import sys
words = sys.argv[1:]
sentence = " ".join(words)
print("[%s] %s" % (getpass.getuser(), sentence))

The algorithm commonly used when “manually” parsing a number of non-positional arguments is to iterate over the sys.argv list. One way is to go over the list and pop each element of it:

reverse and copy sys.argv argv = reversed(sys.argv)
extract the first element arg = argv.pop()
stop iterating when there's no more args to pop() while len(argv) > 0:
if arg in ('-f', '--foo'):
print('seen foo!') elif arg in ('-b', '--bar'):
print('seen bar!')
elif arg in ('-a', '--with-arg'): arg = arg.pop()
print('seen value: {}'.format(arg))
get the next value arg = argv.pop()

Parsing Command Line arguments: Setting mutually exclusive arguments with argparse

If you want two or more arguments to be mutually exclusive. You can use the function argparse.ArgumentParser.add_mutually_exclusive_group(). In the example below, either foo or bar can exist but not both at the same time.

import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group() – Python® Notes for Professionals 403

group.add_argument("-f", "--foo")
group.add_argument("-b", "--bar")
args = parser.parse_args()
print "foo = ",
print "bar = ",

If you try to run the script specifying both –foo and –bar arguments, the script will complain with the below message.

error: argument -b/--bar: not allowed with argument -f/--foo

Parsing Command Line arguments: Basic example with docopt

docopt turns command-line argument parsing on its head. Instead of parsing the arguments, you just write the usage string for your program, and docopt parses the usage string and uses it to extract the command line arguments.

Usage: [-a] [-b]
-a Print all the things.
-b Get more bees into the path.
from docopt import docopt
if name == "main":
args = docopt(doc)
import pprint; pprint.pprint(args)
Sample runs:
python Usage: [-a] [-b]
python something {'-a': False,
'-b': False,
'': 'something'}
python something -a {'-a': True,
'-b': False,
'': 'something'}
python -b something -a {'-a': True,
'-b': True,
'': 'something'}

Custom parser error message with argparse

You can create parser error messages according to your script needs. This is through the argparse.ArgumentParser.error function. The below example shows the script printing a usage and an error message to stderr when –foo is given but not –bar.

import argparse – Python® Notes for Professionals 404

parser = argparse.ArgumentParser()
parser.add_argument("-f", "--foo")
parser.add_argument("-b", "--bar")
args = parser.parse_args()
if and is None:
parser.error("--foo requires --bar. You did not specify bar.")
print "foo =",
print "bar =",
Assuming your script name is, and we run: python --foo ds_in_fridge

The script will complain with the following:

usage: [-h] [-f FOO] [-b BAR] error: --foo requires --bar. You did not specify bar.

Conceptual grouping of arguments with argparse.add_argument_group()

When you create an argparse ArgumentParser() and run your program with ‘-h’ you get an automated usage message explaining what arguments you can run your software with. By default, positional arguments and conditional arguments are separated into two categories, for example, here is a small script ( and the output when you run python -h.

import argparse

parser = argparse.ArgumentParser(description=’Simple example’)

parser.add_argument(‘name’, help=’Who to greet’, default=’World’)





args = parser.parse_args()

usage: [-h] [--bar_this BAR_THIS] [--bar_that BAR_THAT]
[--foo_this FOO_THIS] [--foo_that FOO_THAT]
Simple example
positional arguments:
name Who to greet
optional arguments:
-h, --help show this help message and exit
--bar_this BAR_THIS
--bar_that BAR_THAT
--foo_this FOO_THIS
--foo_that FOO_THAT

There are some situations where you want to separate your arguments into further conceptual sections to assist your user. For example, you may wish to have all the input options in one group, and all the output formatting options in another. The above example can be adjusted to separate the –foo_* args from the –bar_* args like so.

import argparse
parser = argparse.ArgumentParser(description='Simple example')
parser.add_argument('name', help='Who to greet', default='World')

Create two argument groups

foo_group = parser.add_argument_group(title='Foo options')
bar_group = parser.add_argument_group(title='Bar options')

Add arguments to those groups

args = parser.parse_args()

Which produces this output when python -h is run:

usage: [-h] [--bar_this BAR_THIS] [--bar_that BAR_THAT]
[--foo_this FOO_THIS] [--foo_that FOO_THAT]
Simple example
positional arguments:
name Who to greet
optional arguments:
-h, --help show this help message and exit
Foo options:
--bar_this BAR_THIS
--bar_that BAR_THAT
Bar options:
--foo_this FOO_THIS
--foo_that FOO_THAT

Parsing Command Line arguments: Advanced example with docopt and docopt_dispatch

As with docopt, with [docopt_dispatch] you craft your –help in the doc variable of your entry-point module.

There, you call dispatch with the doc string as argument, so it can run the parser over it.

That being done, instead of handling manually the arguments (which usually ends up in a high cyclomatic if/else structure), you leave it to dispatch giving only how you want to handle the set of arguments.

This is what the dispatch.on decorator is for: you give it the argument or sequence of arguments that should trigger the function, and that function will be executed with the matching values as parameters.

"""Run something in development or production mode.
Usage: --development --production items add items delete
from docopt_dispatch import dispatch
def development(host, port, **kwargs):
print('in *development* mode')
def development(host, port, **kwargs):
print('in *production* mode')
@dispatch.on('items', 'add')
def items_add(item, **kwargs):
print('adding item…')
@dispatch.on('items', 'delete')
def items_delete(item, **kwargs):
print('deleting item…')
if name == 'main':

Learn More


Please enter your comment!
Please enter your name here