optparse question

Peter Otten __peter__ at web.de
Sun Sep 19 00:41:58 CEST 2004


GMTaglia wrote:

> I think it will be better if only the exact option will match, it will
> avoid typing mistakes and/or accidentally wrong matches, imho.

The source is there to override, even if it scares you away with abundant
underscores.

import optparse

def _(s): return s

class OptionParser(optparse.OptionParser):
    def _match_long_opt(self, opt):
        if opt in self._long_opt:
            return opt
        raise optparse.BadOptionError(_("no such option: %s") % opt)

if __name__ == "__main__":
    p = OptionParser()
    p.add_option("--long", action="store_true")
    options, args = p.parse_args()
    print "long:", options.long
 
Peter




More information about the Python-list mailing list