Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72303

Re: Optparse to parsing Suggestions !!

References <CACT3xuVs-RiJFcz0BWkzpvyp89Kg8Hg2UB=hJ9HR+PVhr-hKeA@mail.gmail.com>
Date 2014-05-30 20:59 +0530
Subject Re: Optparse to parsing Suggestions !!
From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.10487.1401463772.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Fri, May 30, 2014 at 7:48 PM, Ganesh Pal <ganesh1pal@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Optparse to parsing Suggestions !! Ganesh Pal <ganesh1pal@gmail.com> - 2014-05-30 20:59 +0530

csiph-web