Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'elif': 0.05; 'args': 0.07; 'none:': 0.07; '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.16; 'convey': 0.16; 'i.e': 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; 'skip:p 40': 0.19; 'meant': 0.20; 'example': 0.22; 'header:User-Agent:1': 0.23; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'url:python': 0.33; 'basic': 0.35; 'acceptable': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; '2014,': 0.84 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: Sat, 31 May 2014 12:32:07 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb5da.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401532345 news.xs4all.nl 2870 [2001:888:2000:d::a6]:44736 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72332 Am Sa Mai 31 2014, 13:08:12 schrieb Ganesh Pal: >> 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") >> >> -- >> https://mail.python.org/mailman/listinfo/python-list > > Thanks Peter , above will work for case 2. > > How about case 1. i.e . > > The --path is optional for few for the object type. > > #python corrupt.py --object=super_block --size=1000 > > ==> Should work even if --path is not given and should not complain > if --path is missing My code example is meant to convey the basic idea; you have to adapt it to your needs. E. g. if options.object == "super_block": if not path_is_acceptable_for_super_block(options.path): parser.error( "{!r} not an acceptable path for super_block object" .format(options.path)) elif options.object == "files" and options.path is None: parser.error("'files' object requires a path") where you are to write the path_is_acceptable_for_super_block() function to comply with your requirements.