Optparse to parsing Suggestions !!

Peter Otten __peter__ at web.de
Sat May 31 06:32:07 EDT 2014


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.




More information about the Python-list mailing list