Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!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; '8bit%:1': 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; 'wrote:': 0.18; 'code.': 0.18; 'entered': 0.20; 'command': 0.22; 'email addr:gmail.com>': 0.22; 'saying': 0.22; 'error': 0.23; 'parse': 0.24; 'skip:" 30': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'accomplished': 0.31; 'file': 0.32; 'this.': 0.32; 'run': 0.32; 'cases': 0.33; 'fri,': 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; 'pm,': 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; '30,': 0.65; 'world': 0.66; 'here': 0.66; 'sample': 0.67; '8bit%:31': 0.68; '\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:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=//yF6Mot0n+wU3NuBRma1W2wO40OlvtLzWGEtCBgKg8=; b=IiwSE+gCP48CQl45RQkRpIOX8zigWGOHe9fJwAJu7oNJ/rezDNbh0OD7l57pWQZtgC Y+1dxL58JY5M7kxEXjO5wLyaEk9mrchJgwqY8bQIFT4ibpqwslF4Jnht7UIFGSNsD6sS sTkJcuGNG0PW1kfSkg6EWqwj2M732n8fARWF6IMktKtcH0jSx2VTkp8lhcHQRNYI3yZu ibyxyZsNtybhwITWYj/LqmI+MxiLhSsCj718pFWEHYW3FGo7IlIxZvqwiCr8PTqLLWKJ oLwIuYAhmQCcooAhLKdRE2vaHeESy+W2CBM6GggihXH0KTjeJDipeUtuuCGN+ztoqf+v PsRg== MIME-Version: 1.0 X-Received: by 10.113.3.139 with SMTP id bw11mr12617085lbd.63.1401463770826; Fri, 30 May 2014 08:29:30 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 May 2014 20:59:30 +0530 Subject: Re: Optparse to parsing Suggestions !! From: Ganesh Pal To: python-list@python.org Content-Type: multipart/alternative; boundary=001a1134945439ce5504fa9fb6ba 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: 184 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401463772 news.xs4all.nl 2975 [2001:888:2000:d::a6]:55402 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72303 --001a1134945439ce5504fa9fb6ba Content-Type: text/plain; charset=UTF-8 On Fri, May 30, 2014 at 7:48 PM, Ganesh Pal 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] .]', > 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. --001a1134945439ce5504fa9fb6ba Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On Fri, May 30, 2014 at 7:48 PM, Ganesh Pal <ganesh1pal@gmail.com= > wrote:

Hello Python world ,<= /div>


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

<= div># Here is 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


Can these be accomplished =C2=A0with=C2=A0<= /span>optparse=C2=A0using a callback.=C2=A0=C2=A0 I= was reading about this.
--001a1134945439ce5504fa9fb6ba--