CSc 250: Lecture Notes: Command Line Arguments

Introduction

Recall that in bash we could access command line arguments typed on the command line after the name of the script. The bash script could access and use these arguments within the script. The example that was used to inroduce this concept earlier in the semester was:

1 #!/bin/bash
2  
3 echo "The script being run is named: ${0}"
4 echo "Arguments provided:            $#"
5  
6 echo "     The first argument is:    ${1}"
7 echo "     The second argument is:   ${2}"
8 echo "     The third argument is:    ${3}"

We used this concept heavily in the first few homework assignments.

Command line arguments can also be given to a python script, and the arguments can be accessed from within the script. However, the arguments are not stored in individually-numbered variables, like they are in bash. In python, all of the arguments are stored in a list. Specifically, they are stored in a list named argv that is insode of the sys module (argv is short for “argument vector”).

To access the name of the script/program, we must access the first element of the sys.argv list:

sys.argv[0]

To get the length of this list, do the usual:

len(sys.argv)

However, this number includes the element that represents the name of the script. To get the number of arguments other than the script name, do:

len(sys.argv) - 1

And then, to get each other argument individually, do:

sys.argv[1]
sys.argv[2]
sys.argv[3]
...

A Full Example

Below is the contents of a python script named args.py:

import sys
print('This program was given ' + str(len(sys.argv)) + ' command-line arguments')
index = 1
for arg in sys.argv:
    print('Argument ' + str(index) + ' is ' + arg)
    index += 1

Here are a few runs of it from the bash shell:

$ python3 args.py A B C
This program was given 4 command-line arguments
Argument 1 is args.py
Argument 2 is A
Argument 3 is B
Argument 4 is C
$
$ python3 args.py Hello World
This program was given 3 command-line arguments
Argument 1 is args.py
Argument 2 is Hello
Argument 3 is World
$

The number of arguments is off, because len(sys.argv) includes the script name, and the loop that prints out the arguments also includes the script name (args.py). Here is a modification that improves the script to not include the script name in these parts:

import sys
print('This program was given ' + str(len(sys.argv) - 1) + ' command-line arguments')
index = 1
while index < len(sys.argv):
    print('Argument ' + str(index) + ' is ' + sys.argv[index])
    index += 1

Notice the changes:

$ python3 args.py A B C
This program was given 3 command-line arguments
Argument 1 is A
Argument 2 is B
Argument 3 is C
$
$ python3 args.py Hello World
This program was given 2 command-line arguments
Argument 1 is Hello
Argument 2 is World
$

Another Example

Here is another example, that uses a command-line argument to look up a value in a dictionary:

import sys

# The keys of this dictionary are superhero roles.
# The values are the actors who played the roles.
roles = {
    'Thor' : 'Chris Hemsworth',
    'Batman' : 'Christian Bale',
    'Superman' : 'Henry Cavill',
    'Wolverine' : 'Hugh Jackman'}

# If the first argument is 'who' and the second argument is 'played'
# and we have the role in our roles dictionary, then we can tell the
# user what actor played the role. We get the actor by looking it up
# in the dictionary.
# Otherwise, let the user know that the program is confused.
if sys.argv[1] == 'who' and sys.argv[2] == 'played' and sys.argv[3] in roles:
    role = sys.argv[3]
    actor = roles[role]
    print(actor + ' played ' + role)
else:
    print('Huh?')

Here are some runs:

$ python3 find-role.py who played Thor
Chris Hemsworth played Thor
$ python3 find-role.py who played Superman
Henry Cavill played Superman
$ python3 find-role.py who played Bane
Huh?
$ python3 find-role.py I heart movies!
Huh?
$