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


Groups > comp.lang.python > #72332

Re: Optparse to parsing Suggestions !!

From Peter Otten <__peter__@web.de>
Subject Re: Optparse to parsing Suggestions !!
Date 2014-05-31 12:32 +0200
Organization None
References <CACT3xuVs-RiJFcz0BWkzpvyp89Kg8Hg2UB=hJ9HR+PVhr-hKeA@mail.gmail.com> <lma9h7$fhn$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.10506.1401532345.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

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


Thread

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

csiph-web