Python main() - Command Line Arguments
Today's example is the affirm.py program which you can download if you want to try it yourself. Some code is left to be done as an exercise in the file affirm-exercise.py.
Command Line Argument -affirm name
The affirm.py program has a few options to say nice things about a name. Here are three runs of the program in the command line. What the user types is in bold, followed by the program's printed output.
$ python3 affirm.py -affirm Lisa Everything is coming up Lisa $ python3 affirm.py -affirm Bart Looking good Bart $ python3 affirm.py -affirm Maggie Today is the day for Maggie $
Command line arguments, or "args", are extra information typed on the line when a program is run. The system is deceptively simple - the command line arguments are the words typed after the program.py on the command line, separated from each other by spaces. So in the first in this command line:
$ python3 affirm.py -affirm Lisa
The words -affirm and Lisa are the 2 command line args.
Command line arguments like -affirm often select a mode or option for the run of the program, and these options typically begin with a dash as we have here.
-hello name
With the -hello option, the program prints a plain hello like this:
$ python3 affirm.py -hello Bart Hello Bart
-n num name
With the -n option, the program prints the name a number of times, like this.
$ python3 affirm.py -n 10 Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie $ python3 affirm.py -n 100 Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie
In the examples above, the number of command line arguments is three: -n, number, name
Command Line Args Python Code
def main()
args = sys.argv[1:]
# args is a list of the command line args
The main() above begins with a CS106A standard line args = sys.argv[1:] which sets up a list named args to contain the command line arg strings. This line works and you can always use it. If you want to know what "sys.argv" is about, see the end of this document.
The args list contains one string for each command line argument.
1. For this command line:
$ python3 affirm.py -affirm Lisa
args is the list ['-affirm', 'Lisa']
2. For this command line
$ python3 affirm.py -n 10 Maggie
args is the list ['-n', '10', 'Maggie']
However many command line arguments the user typed in, they will populate the args list. Note that the args in the list are always strings.
How To Write main()
The code in main() can use a simple series of if-statements to detect the different options, such as -affirm, and run the appropriate code for each option.
For example, consider a run of the program with the -affirm option like this:
$ python3 affirm.py -affirm Lisa
Here is the if-statement in main() which detects this command line option and runs the code for it. The code checks if the number of args is 2, and the first arg (i.e. args[0]) is -affirm. If so, it prints the name which is in args[1] with a random affirmation.
def main()
args = sys.argv[1:]
# 1. Check for the arg pattern:
# python3 affirm.py -affirm Bart
# e.g. args[0] is '-affirm' and args[1] is 'Bart'
if len(args) == 2 and args[0] == '-affirm':
# Select random nice phrase
affirmation = random.choice(AFFIRMATIONS)
# Print with the name in args[1]
print(affirmation, args[1])
# .. later if statements for -hello and -n ...
Q: Why check if args[0] is equal to '-affirm'?
A: The args list holds the command line args. In this case we want to check if the first command line arg is the string '-affirm', and args[0] is the first arg.
main() String vs. int
Consider the use of the -n option
python3 affirm.py -n 10 Maggie
The values in the args list are always strings. In this case args[0] is the string '-n' and args[1] is the string '100'. The code needs to convert the string '100' to the int value 100 using the int() function.
Examples and Exercises
The file affirm.py has all three command line options working: -affirm, -hello, -n
You can run the program to see what it does, and look at the main() code as an example.
The file affirm-exercise.py is the same, but does not have the code for -hello and -n, so you can practice coding up those cases.
What is up With sys.argv?
The module sys holds interfaces to the operating system, and in particular it contains a list with the unfortunate name argv which holds the command line arguments. The name argv was chosen around 1974, and since then successive programming languages have just kept that name. By convention, the argv list contains an extra string at index 0 which is the name of the script itself, e.g. 'affirm.py'. This extra string is not helpful for us. Therefore we create the args list like this:
def main()
args = sys.argv[1:]
We choose the more sensible name "args" to hold the command line args, and we use a slice to skip over the unhelpful string at index 0, thus creating an args list that is exactly just the command line arguments.
Copyright 2020 Nick Parlante