Python : parsing the command line options using optparse


Ganesh Pal

Hi Folks ,

Iam newbie to Python, Iam trying to use optparse module and write a script
that will parse the command line options ..I had to use opt parse instead
of argparse because by host Operating system is still using python 2.6

Below is the simple program ( Feel free to correct the error ) and 3 quick
questions on the same.

/* sample Program "/

import optparse

parser = optparse.OptionParser()

parser.add_option("-p", "--path", action="store",
metavar="/ifs/<file_name>|/ifs/<dir_name>", dest="path_name",


help = "The file or directory path of the object in

parser.add_option("-q", "--operation", action="store",
metavar="XOR|ADD|SET", dest="operation_type", default='SET',

type='choice', choices=['XOR', 'ADD', 'SET'],

help = "The corruption operation on the object [default :

parser.add_option("-f", "--offset", action="store", metavar="HEX",
dest="offset_value", default=0x41306141,


help= "The valid offset value [default : %default]" )

parser.add_option("-n", "--node", action="store",metavar="id",
dest="node_value", default= 1,


help = "The node id [default : %default]")

parser.add_option("-l", "--log", action="store_true",dest="log_file",

help = "log the isi_corrupt execution result ")

parser.add_option("-c", "--fixcrc", action="store_true",dest="fix_crc",

help = "The CRC fix of the corrupted objects ")

# instruct optparse to parse the program's command line:

(options, args) = parser.parse_args()

print options.path_name

Questions (1)

#python --path=/ifs/1.txt --operation=XOR --node=11 --log



I wanted to store all the options in a list like

please suggest on the same ?


Why does this program work only with - option and not - in the above code

I think its not working when the type= 'choice' or its somethimf else ?

# -p=/ifs/1.txt -q=XOR -f=1234 -n=1 -l

Usage: [options] error: option -q: invalid choice: '=XOR' (choose from 'XOR',


This Works ( --)

C:\Users\bahadg\Desktop>python --path=/ifs/1.txt
--operation=XOR --

offset=1234 --node=1 --log --fixcrc


Question (3)

If I have really long metavar and the help looks very messy ,is there a
way to make it look elegant.

Example :

parser.add_option("-q", "--operation", action="store",
metavar="XOR|ADD|SET|MODIFY|RENAME|DELETE|KILL|", dest="operation_type",

type='choice', choices=['XOR', 'ADD', 'SET'

help = "The corruption operation on the object [default :
%default]",) --help

Usage: [options]


-h, --help show this help message and exit

-p /ifs/<file_name>|/ifs/<dir_name>,

The file or directory path of the object in /ifs



The corruption operation on the object [default :

-f HEX, --offset=HEX The valid offset value [default : 1093689665]

-n id, --node=id The node id [default : 1]

-l, --log log the isi_corrupt execution result

-c, --fixcrc The CRC fix of the corrupted objects

Thanks is advance !!

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Optparse to parsing Suggestions !! 0
optparse options 2
optparse eats $ 2
PEP proposal optparse 8
testing command line scripts that use optparse 0
optparse 3
optparse 4
OptParse and Constant values 0

Members online

Forum statistics

Latest member

Latest Threads