Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'modify': 0.07; 'parser': 0.07; 'args)': 0.09; 'field)': 0.09; 'function:': 0.09; 'line:': 0.09; 'lookup': 0.09; 'optparse': 0.09; 'skip:/ 10': 0.09; 'subject:parsing': 0.09; 'python': 0.11; 'def': 0.12; '#python': 0.16; 'default:': 0.16; 'main():': 0.16; 'optional': 0.16; 'simulate': 0.16; 'skip:" 70': 0.16; 'skip:" 80': 0.16; 'skip:a 100': 0.16; 'storing': 0.16; 'throw': 0.16; '\xc2\xa0if': 0.16; 'code.': 0.18; 'entered': 0.20; 'command': 0.22; 'saying': 0.22; 'error': 0.23; 'parse': 0.24; 'skip:" 30': 0.26; 'skip:" 20': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'run': 0.32; 'cases': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; '8bit%:9': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'even': 0.60; 'skip:\xc2 10': 0.60; 'skip:o 30': 0.61; 'choose': 0.64; '8bit%:10': 0.64; 'world': 0.66; 'here': 0.66; 'sample': 0.67; '\xc2\xa0\xc2\xa0': 0.74; '==>': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=VAkyzr6ekAJlF+f9xVTMwixmzIrimXTGgQn99qerUE8=; b=yoJur6xzvja3UHmm5xItexI7wG68a/3cKlQcjirRtCpiyhATfS2lhDhafv96RAI/MP InmNFjya1fZK5FD5VhF+3iMtiEfv1/mwCtylIL8cv2sTeWEANv2tVBarAb46s09NDbKc LD0Y6h0yGg3IKiwRJA1YXfD/7pIkGpT37BNIUgbCa6r5llnZGNzuDdDJg28sx5ty8scO CCAV+SOUbyVfraWtx7JDLLtL8twH9s4xVECWFz3thNOF7x19D8xSnQ8s+/OX1YwgT+sj lYRTFWWepLDKdOsvc617JmthQUZXKTqV9J5fLyguek8OKdRfUdeUJWs9/dwh2f6GyxC3 2AtQ== MIME-Version: 1.0 X-Received: by 10.112.150.130 with SMTP id ui2mr2902705lbb.80.1401459497786; Fri, 30 May 2014 07:18:17 -0700 (PDT) Date: Fri, 30 May 2014 19:48:17 +0530 Subject: Optparse to parsing Suggestions !! From: Ganesh Pal To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b3a821a88633804fa9eb75a X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 152 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401459907 news.xs4all.nl 2844 [2001:888:2000:d::a6]:44902 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72300 --047d7b3a821a88633804fa9eb75a Content-Type: text/plain; charset=UTF-8 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] .]', 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 --047d7b3a821a88633804fa9eb75a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hello Python world ,


I wanted suggestion on how to modify the below code = to help me accomodate the below two cases

# Here i= s the Sample code.

def main():
=C2=A0 =C2=A0 """ = ---MAIN--- """
=C2=A0 =C2=A0 parser =3D optparse.O= ptionParser(usage=3D'%prog [options] <arg1>.]', version=3D= 9;1.0')
=C2=A0 =C2=A0object_choice =3D ('super_block','blocks''= files', 'directory','links')

= =C2=A0 parser.add_option("-o", "--object", action=3D&qu= ot;store",metavar=3D"object",default=3D'inode',dest= =3D"object",type=3D'choice', choices=3Dobject_choice,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0help=3D'The object type to be corrupted [ choose from (' + &#= 39;,'.join(object_choice) + ') default: %default]',)
=
=C2=A0 =C2=A0 parser.add_option("-p", "--path= ",action=3D"store",metavar=3D"/mnt/",dest=3D"= path",type=3D"string",help=3D"The file or directory pat= h in /mnt/",)

=C2=A0 =C2=A0 parser.add_option("-n", "-= -size",action=3D"store",metavar=3D"size",dest=3D&q= uot;size",default=3D1,type=3D'int',help=3D"The offset fie= ld)

=C2=A0
# Instruct optparse to parse the program's = command line:
=C2=A0 (options, args) =3D parser.parse_args()

=C2=A0 =C2=A0 # Build the dispatch table:
= =C2=A0 =C2=A0 object_type_dictionary =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"super_block":super_block_= def,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"blocks":blocks_= def,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "files": files_corrupt= _def,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "directory": director= y_corrupt_def,
=C2=A0 =C2=A0 =C2=A0 =C2=A0"links": links_corrput_def, # no = () here,we're storing function
=C2=A0 =C2=A0 }

=
=C2=A0 =C2=A0 # Lookup the object type and then call the functio= n:
=C2=A0 =C2=A0 object_type_dictionary[options.object]()


Sample Run
# python corrupt.py= =C2=A0--object=3Dfiles --path=3D/tmp/ --size 1000


Questions :
=C2=A0 =C2=A0=C2=A0
Case 1:= =C2=A0# =C2=A0The =C2=A0--path is optional for few for the object type. = =C2=A0How do I simulate the below behaviour=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0#python corrupt.py =C2=A0--object=3Dsuper_block =C2=A0--size=3D10= 00 =C2=A0=C2=A0

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =3D=3D> =C2=A0Should work even if --path is not given=C2=A0
= =C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#python cor= rupt.py =C2=A0--object=3Dsuper_block =C2=A0--path=3D/tmp/--size 1000=C2=A0<= /div>

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D=3D> =C2=A0If user en= ters --object=3Dsuper_block and --path=3D/ifs/ should throw an error
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0saying its an= invaild combinations.
=C2=A0 =C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0=C2=A0
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
case 2: =C2=A0# The --path is man= datory for few of the object types and =C2=A0should complain if its not ent= ered .
=C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 # python corrupt.py =C2=A0--object=3Dfiles --path=3D/tmp/ --size 100= 0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0

Regards,
Ganesh
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0=C2=A0
--047d7b3a821a88633804fa9eb75a--