Optparse to parsing Suggestions !!
Ganesh Pal
ganesh1pal at gmail.com
Fri May 30 11:29:30 EDT 2014
On Fri, May 30, 2014 at 7:48 PM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>
> Hello Python world ,
>
>
> I wanted suggestion on how to modify the below code to help me accomodate
> the below two cases
>
> # Here is the Sample code.
>
> def main():
> """ ---MAIN--- """
> parser = optparse.OptionParser(usage='%prog [options] <arg1>.]',
> version='1.0')
> object_choice = ('super_block','blocks''files', 'directory','links')
>
> parser.add_option("-o", "--object",
> action="store",metavar="object",default='inode',dest="object",type='choice',
> choices=object_choice,
> help='The object type to be corrupted [ choose from
> (' + ','.join(object_choice) + ') default: %default]',)
>
> parser.add_option("-p",
> "--path",action="store",metavar="/mnt/",dest="path",type="string",help="The
> file or directory path in /mnt/",)
>
> parser.add_option("-n",
> "--size",action="store",metavar="size",dest="size",default=1,type='int',help="The
> offset field)
>
>
> # Instruct optparse to parse the program's command line:
> (options, args) = parser.parse_args()
>
> # Build the dispatch table:
> object_type_dictionary = {
> "super_block":super_block_def,
> "blocks":blocks_def,
> "files": files_corrupt_def,
> "directory": directory_corrupt_def,
> "links": links_corrput_def, # no () here,we're storing function
> }
>
> # Lookup the object type and then call the function:
> object_type_dictionary[options.object]()
>
>
> Sample Run
> # python corrupt.py --object=files --path=/tmp/ --size 1000
>
>
> Questions :
>
> Case 1: # The --path is optional for few for the object type. How do I
> simulate the below behaviour
>
> #python corrupt.py --object=super_block --size=1000
>
> ==> Should work even if --path is not given
>
> #python corrupt.py --object=super_block --path=/tmp/--size 1000
>
> ==> If user enters --object=super_block and --path=/ifs/ should
> throw an error
>
> saying its an invaild combinations.
>
>
>
> case 2: # The --path is mandatory for few of the object types and should
> complain if its not entered .
>
> # python corrupt.py --object=files --path=/tmp/ --size 1000
>
>
>
Can these be accomplished with optparse using a callback. I was reading
about this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140530/e4ea5203/attachment.html>
More information about the Python-list
mailing list