If you are going to use a Python script as a command, one of the first things the script needs to do is process the arguments from the command line. We took a look at the way that command line arguments are available in the tutorial about making Python scripts.
When you run a Python script from the command line, the arguments are available in a list that is part of the sys module, called sys.argv. This means that if you have a script called showargs.py that looks like this:
import sys
print sys.argv
and you enter:
python showargs.py a b c d e
then the following will be displayed on the screen:
['showargs.py', 'a', 'b', 'd', 'e']
Arguments are like words — sequences of characters, separated by spaces.
Notice that all the arguments in sys.argv are strings. This means that if you need to use an argument as a number in your script, you have to convert it from a string to either an integer or a floating-point number.
For example, say your script takes two numeric arguments for the width and height of an image you are creating:
python make_picture.py 720 480
You will need to convert those values from strings to integers before you use them to create an image:
width = int(sys.argv[1])
height = int(sys.argv[2])
The first element of sys.argv is the command name; that's why width has an index of 1, and height has an index of 2.
Defining variables from the elements of the sys.argv list isn't strictly necessary — you could use int(sys.argv[1]) throughout the script whenever you needed to specify the width. It's just easier to read the script when you've identified what the argument actually means by giving it a name through its assignment to a variable.
Assigning a series of variable names to the elements of the sys.argv list can get pretty verbose if you have a lot of arguments. For example, in the shape tutorial, the rectangles.py script has these arguments:
Usage: rectangles.py
The simplest way to save the argument values into variables would just assign them one at a time:
width = int(argv[1])
height = int(argv[2])
count = int(argv[3])
length = int(argv[4])
base = argv[5]
type = argv[6]
However, Python lets us associate a list of variables with a list of values, so we can make the argument assignments in a single statement:
[width, height, count, length, base, type] = sys.argv[1:]
Notice that you need to skip sys.argv[0] by using a slice of the list (a sublist starting with the first element and containing the rest of the list, written [1:] using brackets), since the first element of sys.argv is always the name of the script. But we still have the problem that the width, height, count, and length arguments are all integers, and now we have strings again.
To fix this, we can use the map function to convert a list of strings to a list of integers. The map function takes two arguments, a function name and a list:
map(function-name, list)
The map function returns a list that is the result of calling the function on each of the elements of the list. This means that we can create a list of integers from a list of strings by calling map with the int function as an argument. We'll select out the first four arguments, which need to be integers, using a slice of [1:5] as an operator on the list:
[width, height, count, length] = map(int, sys.argv[1:5])
Now we've got the base and type arguments left over, but we can slice those out, too:
[base, type] = sys.argv[5:]
The brackets around the variable names are optional; if there are commas on the left side of an assignment statement, a list is assumed. This means that we could rewrite our argument processing statements like this:
width, height, count, length = map(int, sys.argv[1:5])
base, type = sys.argv[5:]
This is a much more compact way of setting our argument variables, but it does assume that the length of the list has already be verified to be the right size. Usually, the script will do that at the beginning, and print out some documentation if the number of arguments is incorrect.