Python : parsing the command line options using optparse

Ganesh Pal ganesh1pal at gmail.com
Tue Feb 25 10:31:20 EST 2014


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",

                  type="string",

                  help = "The  file or directory path of the object in
/ifs",)



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 :
%default]",)





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

                  type="long",

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





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

                  type='int',

                  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 python-5.py --path=/ifs/1.txt --operation=XOR  --node=11 --log
-fixcrc


>>  Looks like the dest variable stores it in a dictionary ,


 >>  print  options.path_name ( gives the option entered in the command
line)


       /ifs/1.txt


 I wanted to store all the options in a list like
[/ifs/1.txt,XOR,11,log_file,fix_crc]

 please suggest on the same ?







Question(2)



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 ?


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

Usage: python-5.py [options]

python-5.py: error: option -q: invalid choice: '=XOR' (choose from 'XOR',
'ADD',

 'SET', 'MODIFY', 'RENAME', 'DELETE', 'KILL')



This Works ( --)

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

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

/ifs/1.txt





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",
default='SET',

                  type='choice', choices=['XOR', 'ADD', 'SET'
|'MODIFY'|'RENAME'|'DELETE'|'KILL'],

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



 #python-5.py --help



Usage: python-5.py [options]

Options:

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

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

                        The  file or directory path of the object in /ifs

  -q XOR|ADD|SET|MODIFY|RENAME|DELETE|KILL|,
--operation=XOR|ADD|SET|MODIFY|RENA

ME|DELETE|KILL|

                        The corruption operation on the object [default :
SET]

  -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 !!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140225/5fa75a39/attachment.html>


More information about the Python-list mailing list