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


Groups > comp.lang.python > #72306

Re: Optparse to parsing Suggestions !!

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'elif': 0.05; '"""': 0.07; 'args': 0.07; 'modify': 0.07; 'none:': 0.07; 'parser': 0.07; 'args)': 0.09; 'field)': 0.09; 'function:': 0.09; 'line:': 0.09; 'lookup': 0.09; 'optparse': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:parsing': 0.09; 'python': 0.11; 'def': 0.12; '#python': 0.16; 'default:': 0.16; 'main():': 0.16; 'optional': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simulate': 0.16; 'storing': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'entered': 0.20; 'command': 0.22; 'saying': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'parse': 0.24; 'skip:" 30': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'skip:s 70': 0.31; 'file': 0.32; 'run': 0.32; 'cases': 0.33; 'skip:d 20': 0.34; 'should': 0.36; 'skip:- 20': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:o 30': 0.61; 'choose': 0.64; 'world': 0.66; 'here': 0.66; 'sample': 0.67
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Optparse to parsing Suggestions !!
Date Fri, 30 May 2014 17:53:09 +0200
Organization None
References <CACT3xuVs-RiJFcz0BWkzpvyp89Kg8Hg2UB=hJ9HR+PVhr-hKeA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdb114.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
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.10490.1401465209.18130.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401465209 news.xs4all.nl 2829 [2001:888:2000:d::a6]:37676
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72306

Show key headers only | View raw


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] <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

I think you have to do it manually:

options, args = parser.parse_args()

if options.object == "super_block" and options.path is not None:
    parser.error("Paths not allowed for 'super_block' object")
elif options.object == "files" and options.path is None:
    parser.error("'files' object requires a path")

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


Thread

Re: Optparse to parsing Suggestions !! Peter Otten <__peter__@web.de> - 2014-05-30 17:53 +0200

csiph-web