Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67307
| 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 | <ganesh1pal@gmail.com> |
| 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 | <CACT3xuXWBQC2s2XxE=Cav+RK78voPjbLA0cWSxL50ekktJZ2cg@mail.gmail.com> |
| References | <CACT3xuWuV8omgE6oV0PNGfwdf-yrO3KPbjHtu=B+NHNuOhA_Jw@mail.gmail.com> <leig4s$ri5$1@ger.gmane.org> <CACT3xuUGtsER0B3xwAB2CqQ5Zb=v72w8+DQkaYz681xeSeJ2CQ@mail.gmail.com> <lekj1t$v7n$1@ger.gmane.org> <CACT3xuXWBQC2s2XxE=Cav+RK78voPjbLA0cWSxL50ekktJZ2cg@mail.gmail.com> |
| Date | Sat, 1 Mar 2014 16:43:11 +0530 |
| Subject | Re: Python : parsing the command line options using optparse |
| From | Ganesh Pal <ganesh1pal@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7507.1393672393.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
>
>
>
> 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 :)
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Python : parsing the command line options using optparse Ganesh Pal <ganesh1pal@gmail.com> - 2014-03-01 16:43 +0530
Re: Python : parsing the command line options using optparse Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 11:47 +0000
Re: Python : parsing the command line options using optparse Ganesh Pal <ganesh1pal@gmail.com> - 2014-03-01 17:47 +0530
csiph-web