Re: Python : parsing the command line options using optparse
On Sat, Mar 1, 2014 at 5:17 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > > You should start new threads for new questions. The subject line here has > nothing to do with the questions you ask. > > Sure Steven and thanks for replying and your suggestion for Question 2 ( same as Peter Otten ) worked and Iam closing this thread now :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
> > handler = object_type_dictionary[options.object_type] # look up the > function > handler() # call it > > The last two lines could also be merged into one > > object_type_dictionary[options.object_type]() > > but the first version may be clearer. > > Thanks for your valuable inputs all worked :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On Sat, 01 Mar 2014 16:43:11 +0530, Ganesh Pal wrote: > Iam done with the command line parsing but got stuck while trying to > implement switch kind of behavior with dictionaries. So posting 2 more > questions You should start new threads for new questions. The subject line here has nothing to do with the questions you ask. > Question 1 : > > Iam using the options.name directly for manipulations is this fine or > do I need to assign it to variable and then use it It is perfectly fine to use options.name directly. There's no need to save it to a temporary variable just to use it, unless doing this makes your code easier to read. > Question 2 : > > I wanted to use dictionary to match the above if else behavior (we don't > have switch in python I guess ) and If else looks very untidy. > > Is it possible to store the options.object_type as a key in the > dictionary and then based on the value entered in the command line > invoke the appropriate function Yes. You almost got it right here: > object_type_dictonary = { 'LIN' : corrupt_inode(), > 'INODE' : corrupt_lin(), > 'DATA' : corrupt_data(), > }; (By the way, this is Python, not C, and there is no need for redundant semi-colons at the end of each line.) The mistake you made is that you *called* the functions inside the dict. Instead, just refer to the function object itself, without calling it: object_type_dictonary = { # no parens after the functions 'LIN' : corrupt_inode, 'INODE' : corrupt_lin, 'DATA' : corrupt_data, } Then, after you look up the object type, then and only then call the function: func = object_type_dictionary[object_type] func() -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
Ganesh Pal wrote: > Iam using the options.name directly for manipulations is this fine or do > I need to assign it to variable and then use it > if options.object_type == 'LIN': >corrupt_inode() This is fine. You would only consider storing the value if you are going to use it very often (millions rather than thousands) in a time-critical loop. > I wanted to use dictionary to match the above if else behavior (we don't > have switch in python I guess ) and If else looks very untidy. > Is it possible to store the options.object_type as a key in the dictionary > and then based on the value entered in the command line invoke the > appropriate function > I tried creating a dictionary like this but Iam getting a wrong output > object_type_dictonary = { 'LIN' : corrupt_inode(), > 'INODE' : corrupt_lin(), > 'DATA' : corrupt_data(), >}; > and then ran # python corrupt.py -object_type= inode ( This prints all > the values for me) That's because you are storing the result of the function call when you should be storing the function itself: # build the dispatch table: object_type_dictionary = { "LIN": corrupt_lin, # no () here, we're storing the function "INODE": corrupt_inode, "DATA": corrupt_data } ... handler = object_type_dictionary[options.object_type] # look up the function handler() # call it The last two lines could also be merged into one object_type_dictionary[options.object_type]() but the first version may be clearer. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
> > > > Thanks Peter and Simon for the hints it worked : ) without ' =' > > # Python corrupt.py -o INODE -p /ifs/1.txt -q SET -f 1 > > Current Default Choice : > > Choice: INODE > Choice: SET > Choice: 1 > > > > Iam done with the command line parsing but got stuck while trying to implement switch kind of behavior with dictionaries. So posting 2 more questions Question 1 : Iam using the options.name directly for manipulations is this fine or do I need to assign it to variable and then use it Example: Entered at cli #python corrupt.py -object_type INODE --path_name/ifs/1.txt -operation_type SET Initialize all the command line option and then use it object_type = options.object_type path_name = options.path_name if object_type == 'LIN': corrupt_inode() elif object_type == 'DATA': corrupt_data() OR if options.object_type == 'LIN': corrupt_inode() elif options.object_type == 'DATA': corrupt_data() elif options.object_type == 'INODE': corrupt_data() #output #python corrupt.py -object_type INODE -p /ifs/1.txt -q SET -f 10 -m 10 -n 123 -l -c Corrupted inode _ Question 2 : I wanted to use dictionary to match the above if else behavior (we don't have switch in python I guess ) and If else looks very untidy. Is it possible to store the options.object_type as a key in the dictionary and then based on the value entered in the command line invoke the appropriate function I tried creating a dictionary like this but Iam getting a wrong output object_type_dictonary = { 'LIN' : corrupt_inode(), 'INODE' : corrupt_lin(), 'DATA' : corrupt_data(), }; and then ran # python corrupt.py -object_type= inode ( This prints all the values for me) Example : Corrupted inode Corrupted LIN Corrupted data PS : If user enters object_type= inode it should execute corrupt_inode and print corrupted inode Any help on tips highly helpful :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
> > They must be running an older version of FreeBSD since the default version > of python is 2.7. > > There is a FreeBSD package for argparse, the command would be something > like >pkg_add -r install py26-argparse > > > Rod > > Yes Iam running a older version of FreeBSD ( Iam actually running a derivative of FreeBSD ) Thanks for the suggestion I ran the command but I got the below error # pkg_add -r install py26-argparse Error: Unable to get ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-7-stable/Latest/install.tbz: File unavailable (e.g., file not found, no access) pkg_add: unable to fetch ' ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-7-stable/Latest/install.tbz' by URL Error: Unable to get ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-7-stable/Latest/py26-argparse.tbz: File unavailable (e.g., file not found, no access) pkg_add: unable to fetch ' ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-7-stable/Latest/py26-argparse.tbz' by URL Iam now trying to browse the link directly didnt find the patch , will keep you posted -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On 2014-02-26 04:30, Ganesh Pal wrote: On Tue, Feb 25, 2014 at 9:55 PM, Peter Otten <__pete...@web.de> wrote: As you are just starting I recommend that you use argparse instead of optparse. I would love to use argparse but the script that I plan to write has to run on host machines that Python 2.6 I have freebsd clients with python 2.6 dont want to install python new version on all the host machine which will be eventually upgraded to 2.7 . I wanted know if I could use argparse with python 2.6 and is it possible to add something like #pkg_add -r install python-argparse and install python argparse module before I use it. They must be running an older version of FreeBSD since the default version of python is 2.7. There is a FreeBSD package for argparse, the command would be something like pkg_add -r install py26-argparse Rod -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On Wed, Feb 26, 2014 at 4:57 PM, Peter Otten <__pete...@web.de> wrote: > > If you stick with optparse just pass the options without '=' > > -qXOR > > and > > -q XOR > > should both work. > > Thanks Peter and Simon for the hints it worked : ) without ' =' # Python corrupt.py -o INODE -p /ifs/1.txt -q SET -f 1 Current Default Choice : Choice: INODE Choice: SET Choice: 1 -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
Ganesh Pal wrote: > On Tue, Feb 25, 2014 at 9:55 PM, Peter Otten <__pete...@web.de> wrote: > >>As you are just starting I recommend that you use argparse instead of > optparse. > > I would love to use argparse but the script that I plan to write has to > run on host machines that Python 2.6 > > I have freebsd clients with python 2.6 dont want to install python new > version on all the host machine which will be eventually upgraded to 2.7 . > > I wanted know if I could use argparse with python 2.6 and is it possible > to > add something like #pkg_add -r install python-argparse and install > python argparse module before I use it. Probably, but I have no experience with freebsd. >> >If you are asking why short options don't work in conjunction with = -- >> >I don't know, it is probably a design choice of the optparse author. >> >argparse accepts short options with like -f=1234 > > I wanted to know why my sample program does not work with short hand > option (-p) and works with long hand option . > > Here is what is happening ( only short hand with -) > > # 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') > > > Result :says invalid choice: '=XOR' If you stick with optparse just pass the options without '=' -qXOR and -q XOR should both work. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On Wednesday, 26 February 2014 09:30:21 UTC, Ganesh Pal wrote: > Here is what is happening ( only short hand with -) > > # 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') Short hand options don't use '=' signs. Try python-5.py -p /ifs/1.txt -q XOR -f 1234 -n 1 -l --Simon -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On Tue, Feb 25, 2014 at 9:55 PM, Peter Otten <__pete...@web.de> wrote: >As you are just starting I recommend that you use argparse instead of optparse. I would love to use argparse but the script that I plan to write has to run on host machines that Python 2.6 I have freebsd clients with python 2.6 dont want to install python new version on all the host machine which will be eventually upgraded to 2.7 . I wanted know if I could use argparse with python 2.6 and is it possible to add something like #pkg_add -r install python-argparse and install python argparse module before I use it. > >If you are asking why short options don't work in conjunction with = -- I > >don't know, it is probably a design choice of the optparse author. > >argparse accepts short options with like -f=1234 > I wanted to know why my sample program does not work with short hand option (-p) and works with long hand option . Here is what is happening ( only short hand with -) # 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') Result :says invalid choice: '=XOR' Long hand Works ( -- , or double hypen ) fine. C:\Users\bahadg\Desktop>python python-5.py --path=/ifs/1.txt --operation=XOR -- offset=1234 --node=1 --log --fixcrc /ifs/1.txt -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
On 25/02/2014 15:31, Ganesh Pal wrote: 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 Do you have the needed permissions to simply drop the argparse code into the 2.6 stdlib? Could you use the third party docopt instead? It's on pypi and I think it's awesome :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Python : parsing the command line options using optparse
Ganesh Pal wrote: > 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 As you are just starting I recommend that you use argparse instead of optparse. > 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 , It's an instance of optparse.Values. > >> 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 ? While you can create such a list with [getattr(options, name) for name in ["path_name", "operation_type", ...]] I don't see what the advantage over the default format would be. In fact you are making it harder to access a specific option that way. > 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 If you are asking why short options don't work in conjunction with = -- I don't know, it is probably a design choice of the optparse author. argparse accepts short options with like -f=1234 > 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]",) You can move the choices into the help. Example using argparse : parser.add_argument( "-q", "--operation", metavar="OP", default='SET', choices=['XOR', 'ADD', 'SET'], help="The corruption operation on the object [choose from %(choices)s; default: %(default)s]") This becomes -q OP, --operation OP The corruption operation on the object [choose from XOR, ADD, SET; default: SET] in the help. -- https://mail.python.org/mailman/listinfo/python-list
Python : parsing the command line options using optparse
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/|/ifs/", 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, --helpshow this help message and exit -p /ifs/|/ifs/, --path=/ifs/|/ifs/ 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 !! -- https://mail.python.org/mailman/listinfo/python-list