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


Groups > comp.lang.python > #72300

Optparse to parsing Suggestions !!

Date 2014-05-30 19:48 +0530
Subject Optparse to parsing Suggestions !!
From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.10485.1401459907.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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



Regards,
Ganesh

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


Thread

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

csiph-web