From: Avery Pennarun Date: Thu, 31 Dec 2009 23:10:02 +0000 (-0500) Subject: Automatically handle "--no-" prefix on long options. X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=73c29d1bca0a0e08980eb4d474669f3fe5231e43;p=packages%2Fb%2Fbup.git Automatically handle "--no-" prefix on long options. Similar to how git does it. --- diff --git a/options.py b/options.py index 697ac05..6b67c06 100644 --- a/options.py +++ b/options.py @@ -57,7 +57,9 @@ class Options: self._shortopts += f + (has_parm and ':' or '') flagl_nice.append('-' + f) else: + assert(not f.startswith('no-')) # supported implicitly self._longopts.append(f + (has_parm and '=' or '')) + self._longopts.append('no-' + f) flagl_nice.append('--' + f) flags_nice = ', '.join(flagl_nice) if has_parm: @@ -91,12 +93,16 @@ class Options: k = k[1:] if k in ['h', '?', 'help']: self.usage() - k = self._aliases[k] - if not self._hasparms[k]: - assert(v == '') - opt[k] = (opt._opts.get(k) or 0) + 1 + if k.startswith('no-'): + k = self._aliases[k[3:]] + opt[k] = None else: - opt[k] = v + k = self._aliases[k] + if not self._hasparms[k]: + assert(v == '') + opt[k] = (opt._opts.get(k) or 0) + 1 + else: + opt[k] = v for (f1,f2) in self._aliases.items(): opt[f1] = opt[f2] return (opt,flags,extra) diff --git a/t/toptions.py b/t/toptions.py index 5390883..221756f 100644 --- a/t/toptions.py +++ b/t/toptions.py @@ -45,3 +45,5 @@ def test_options(): WVPASSEQ(extra, ['hanky']) WVPASSEQ((opt.t, opt.q, opt.p, opt.l, opt.onlylong, opt.neveropt), (3,1,7,'19',1,None)) + (opt,flags,extra) = o.parse(['--onlylong', '-t', '--no-onlylong']) + WVPASSEQ((opt.t, opt.q, opt.onlylong), (1, None, None))