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


Groups > comp.lang.python > #72332 > unrolled thread

Re: Optparse to parsing Suggestions !!

Started byPeter Otten <__peter__@web.de>
First post2014-05-31 12:32 +0200
Last post2014-05-31 12:32 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Optparse to parsing Suggestions !! Peter Otten <__peter__@web.de> - 2014-05-31 12:32 +0200

#72332 — Re: Optparse to parsing Suggestions !!

FromPeter Otten <__peter__@web.de>
Date2014-05-31 12:32 +0200
SubjectRe: Optparse to parsing Suggestions !!
Message-ID<mailman.10506.1401532345.18130.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web