diff options
author | cinap_lenrek <cinap_lenrek@localhost> | 2011-05-03 11:25:13 +0000 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@localhost> | 2011-05-03 11:25:13 +0000 |
commit | 458120dd40db6b4df55a4e96b650e16798ef06a0 (patch) | |
tree | 8f82685be24fef97e715c6f5ca4c68d34d5074ee /sys/src/cmd/python/Doc/lib/caseless.py | |
parent | 3a742c699f6806c1145aea5149bf15de15a0afd7 (diff) |
add hg and python
Diffstat (limited to 'sys/src/cmd/python/Doc/lib/caseless.py')
-rwxr-xr-x | sys/src/cmd/python/Doc/lib/caseless.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/sys/src/cmd/python/Doc/lib/caseless.py b/sys/src/cmd/python/Doc/lib/caseless.py new file mode 100755 index 000000000..cae94be91 --- /dev/null +++ b/sys/src/cmd/python/Doc/lib/caseless.py @@ -0,0 +1,60 @@ +from optparse import Option, OptionParser, _match_abbrev + +# This case-insensitive option parser relies on having a +# case-insensitive dictionary type available. Here's one +# for Python 2.2. Note that a *real* case-insensitive +# dictionary type would also have to implement __new__(), +# update(), and setdefault() -- but that's not the point +# of this exercise. + +class caseless_dict (dict): + def __setitem__ (self, key, value): + dict.__setitem__(self, key.lower(), value) + + def __getitem__ (self, key): + return dict.__getitem__(self, key.lower()) + + def get (self, key, default=None): + return dict.get(self, key.lower()) + + def has_key (self, key): + return dict.has_key(self, key.lower()) + + +class CaselessOptionParser (OptionParser): + + def _create_option_list (self): + self.option_list = [] + self._short_opt = caseless_dict() + self._long_opt = caseless_dict() + self._long_opts = [] + self.defaults = {} + + def _match_long_opt (self, opt): + return _match_abbrev(opt.lower(), self._long_opt.keys()) + + +if __name__ == "__main__": + from optik.errors import OptionConflictError + + # test 1: no options to start with + parser = CaselessOptionParser() + try: + parser.add_option("-H", dest="blah") + except OptionConflictError: + print "ok: got OptionConflictError for -H" + else: + print "not ok: no conflict between -h and -H" + + parser.add_option("-f", "--file", dest="file") + #print repr(parser.get_option("-f")) + #print repr(parser.get_option("-F")) + #print repr(parser.get_option("--file")) + #print repr(parser.get_option("--fIlE")) + (options, args) = parser.parse_args(["--FiLe", "foo"]) + assert options.file == "foo", options.file + print "ok: case insensitive long options work" + + (options, args) = parser.parse_args(["-F", "bar"]) + assert options.file == "bar", options.file + print "ok: case insensitive short options work" |