]> arthur.barton.de Git - bup.git/commitdiff
Automatically handle "--no-" prefix on long options.
authorAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 23:10:02 +0000 (18:10 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 23:10:02 +0000 (18:10 -0500)
Similar to how git does it.

options.py
t/toptions.py

index 697ac0547a1ac854c19c96a0346bb09ee922789b..6b67c0640af910ee35c3feb0e3653935e31bd809 100644 (file)
@@ -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)
index 5390883faaaf02bdb00fb0d3ec2cec318225977a..221756facdbeeccc2e7cc32c227f9218492566b8 100644 (file)
@@ -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))