Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'elif': 0.05; 'output': 0.05; 'subject:Python': 0.06; 'assign': 0.07; 'initialize': 0.07; 'parsing': 0.09; 'subject:command': 0.09; 'subject:parsing': 0.09; 'subject:using': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; '#python': 0.16; "'data'": 0.16; 'cli': 0.16; 'subject:options': 0.16; 'to:addr:web.de': 0.16; 'appropriate': 0.16; 'variable': 0.18; 'trying': 0.19; 'entered': 0.20; 'command': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'creating': 0.23; 'fine': 0.24; 'helpful': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'subject: : ': 0.26; 'switch': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'posting': 0.31; 'invoke': 0.31; 'prints': 0.31; 'option': 0.32; 'worked': 0.33; 'guess': 0.33; 'subject:the': 0.34; 'but': 0.35; 'received:google.com': 0.35; '(we': 0.36; 'done': 0.36; 'thanks': 0.36; 'possible': 0.36; 'should': 0.36; 'wrong': 0.37; 'skip:- 20': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'url:options': 0.38; 'skip:- 10': 0.38; 'tips': 0.61; 'kind': 0.63; 'more': 0.64; 'hints': 0.68; 'default': 0.69; 'behavior': 0.77; 'don’t': 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 :cc:content-type; bh=dJLSR9OZ9ROepr7Ozw8oYDozE5RISBqFxfqC+yKTYWc=; b=PznxRoqckyS0rZIfE+owx3KC7U4M0vVp3fQpIr5IdOQgsr0PdhWGeDr1u9qG8VcnxI V+dpeAKUckJrrTgONUeqtLoA8ofJr46qFdiaLfJDT84xFYHxiI5sjf1LQ+h1fTVbE6hc 2KOrQM6GpUYHKGGEjjsb9rCSES7Y0PMeBpxrdfSvLD8oEJQZNrgAJibXQ/+9HUZVqF1o iIQs0/l8kCDK55cNykEBQKqoXNi01C/TBMtBbWguWWsEzdTeDDFgzYQjrcGxpv70eTv2 OxcDuSXCmyUYSe7ORjqLoDuBaA4BkL14wRN25eQ8x+syqz0zN1S+ef/pf7QD2HopJSK8 dAIA== MIME-Version: 1.0 X-Received: by 10.152.181.3 with SMTP id ds3mr15297038lac.33.1393672391265; Sat, 01 Mar 2014 03:13:11 -0800 (PST) In-Reply-To: References: Date: Sat, 1 Mar 2014 16:43:11 +0530 Subject: Re: Python : parsing the command line options using optparse From: Ganesh Pal To: Peter Otten <__peter__@web.de> Content-Type: multipart/alternative; boundary=001a11c26f14d0920e04f389a382 Cc: python-list@python.org 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: 299 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393672393 news.xs4all.nl 2879 [2001:888:2000:d::a6]:40025 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67307 --001a11c26f14d0920e04f389a382 Content-Type: text/plain; charset=ISO-8859-1 > > > > Thanks Peter and Simon for the hints it worked : ) without ' =' > > # Python corrupt.py -o INODE -p /ifs/1.txt -q SET -f 1 > > Current Default Choice : > > Choice: INODE > Choice: SET > Choice: 1 > > > > Iam done with the command line parsing but got stuck while trying to implement switch kind of behavior with dictionaries. So posting 2 more questions Question 1 : Iam using the options.name directly for manipulations is this fine or do I need to assign it to variable and then use it Example: Entered at cli #python corrupt.py -object_type INODE --path_name/ifs/1.txt -operation_type SET Initialize all the command line option and then use it object_type = options.object_type path_name = options.path_name if object_type == 'LIN': corrupt_inode() elif object_type == 'DATA': corrupt_data() OR if options.object_type == 'LIN': corrupt_inode() elif options.object_type == 'DATA': corrupt_data() elif options.object_type == 'INODE': corrupt_data() #output #python corrupt.py -object_type INODE -p /ifs/1.txt -q SET -f 10 -m 10 -n 123 -l -c Corrupted inode _________________________________________________________________________________________________________ Question 2 : I wanted to use dictionary to match the above if else behavior (we don't have switch in python I guess ) and If else looks very untidy. Is it possible to store the options.object_type as a key in the dictionary and then based on the value entered in the command line invoke the appropriate function I tried creating a dictionary like this but Iam getting a wrong output object_type_dictonary = { 'LIN' : corrupt_inode(), 'INODE' : corrupt_lin(), 'DATA' : corrupt_data(), }; and then ran # python corrupt.py -object_type= inode ( This prints all the values for me) Example : Corrupted inode Corrupted LIN Corrupted data PS : If user enters object_type= inode it should execute corrupt_inode and print corrupted inode Any help on tips highly helpful :) --001a11c26f14d0920e04f389a382 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


Thanks  Peter &nb= sp;and Sim= on for the hints  it worked  : )  without ' =3D&#= 39;

# Python corrupt.py -o INODE -p /ifs/1.txt -q SET = -f 1

Current Default Choice :

Choice: INODE
Choice: SET
Choice: 1





Iam done with the command line par= sing but got stuck while trying to implement switch kind of behavior with dictionaries.  So pos= ting 2 more questions 

 

Question 1 :


Iam using the option= s.name  directly  for manipulations is this fine or do I need to assign it to variable and then use it



Example:


Entered at cli #python corrupt.py –object_type INODE&n= bsp; --path_name/ifs/1.txt -operation_type SET 



Initialize all the command line option and then use = it


object_type =3D options.object_type

path_name  =3D options.path_name

if object_type =3D=3D 'LIN':

        corrupt_i= node()

   elif  object_type  =3D=3D 'DATA':

         &nb= sp;      corrupt_data()

 

         &nb= sp;     OR

if options.object_type =3D=3D 'LIN':

       corrupt_inode()=

   elif  options.object_type  = =3D=3D 'DATA':

         cor= rupt_data()

   elif  options.object_type  = =3D=3D 'INODE':

         cor= rupt_data()



#output

#python corrupt.py –object_type INODE  -p= /ifs/1.txt -q SET -f 10

 -m 10  -n 123  -l  -c

Corrupted inode

____________________________________________________= _____________________________________________________


Question 2 :


I wanted to use dictionary to match the above if els= e behavior (we don’t have switch in python I guess ) and  If else looks very untidy.


Is it possible to store the options.object_type as a= key in the dictionary and   then based on the value entered in the command line invoke the appropriate function

 


I tried creating a dictionary like this but Iam = getting a wrong output


  

object_type_dictonary =3D { 'LIN' : corrupt_= inode(),

        &nbs= p;            &= nbsp;               &nb= sp;'INODE' : corrupt_lin(),

        &nbs= p;             =             &nb= sp;      'DATA' : corrupt_data(),

         &nb= sp;         };



and then  ra= n # python corrupt.py –object_type=3D inode  (  This prints= all the values for me)


Example :


Corrupted inode

Corrupted LIN

Corrupted  data

 

PS : If user e= nters object_type=3D inode it should execute  corrupt_inode  &nbs= p;and print corrupted inode

 

Any help = on  tips highly helpful :)

<= div class=3D"gmail_extra">



<= br>




<= br>

--001a11c26f14d0920e04f389a382--