dreamsys software

Python Scripting Tutorial

OptionParser


The python class optparse.OptionParser, is a powerful tool for creating options for your script. In our previous example, we just had the user enter two command line arguments to the python script, without specifying which is which. Most users expect to be able to give parameters in any specific order and specify which is which, this can be done in python very easily. Let's change our years.py script to show how:

#!/usr/bin/env python
# Years till 100
import sys
import optparse

parser = optparse.OptionParser()
parser.add_option('-n', '--name', dest='name', help='Your Name')
parser.add_option('-a', '--age', dest='age', help='Your Age', type=int)

(options, args) = parser.parse_args()

if options.name is None:
    options.name = raw_input('Enter Name:')

if options.age is None:
    options.age = int(raw_input('Enter Age:'))

sayHello = 'Hello ' + options.name + ','

if options.age == 100:
    sayAge = 'You are already 100 years old!'
elif options.age < 100:
    sayAge = 'You will be 100 in ' + str(100 - options.age) + ' years!'
else:
    sayAge = 'You turned 100 ' + str(options.age - 100) + ' years ago!'

print sayHello, sayAge

This script imports the optparse package in order to make use of the class OptionParser. The OptionParse class will allow you to add options to your script and it will generate a help option based on the options you provide. In this example, we are adding two options: -n (or --name) and -a (or --age). The first parameter to add_option is the short option and the 2nd is the long option, it is quite common in the Unix & Linux environment to add a short and long version of an option. Tthe next optional parameters to the add_option function are dest=, which is the variable name created, help=, which is the help text generated and type=, which gives the type for the variable. By default the type is string, but for age, we want to make it int.

Finally, after adding the options, we call the parse_args function, which will return an options object and an args list object. We can access the variables defined in "dest=" when adding options on the options object returned. So it will have two options, options.name and options.age. If one of the variables wasn't passed in, we can check by using the "if variableName is None" condition. Then we will load from user input as before.

Now, there are several ways to run this script:

./years.pyPrompts for user and age
./years.py -n JoeSets user, prompts for age
./years.py --name JoeSets user, prompts for age
./years.py -a 25Sets age, prompts for user
./years.py --age 25Sets age, prompts for user
./years.py -a 25 --name JoeSets age, sets user
./years.py -n Joe --age 25Sets age, sets user

Another thing you can do now is run the help option, by specifying either -h or --help:

./years.py -h

This will give the following output and then exit the script:

usage: years.py [options]

options:
  -h, --help            show this help message and exit
  -n NAME, --name=NAME  Your Name
  -a AGE, --age=AGE     Your Age

Now that our script is complete, we can move on to more interesting topics. In the next section we will cover looping.

Prev (If/Else) | Next (Looping)


Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6