]> git.michaelhowe.org Git - packages/b/bup.git/commitdiff
options.py: better support for explicit no-* options.
authorAvery Pennarun <apenwarr@gmail.com>
Wed, 8 Sep 2010 07:31:19 +0000 (00:31 -0700)
committerAvery Pennarun <apenwarr@gmail.com>
Wed, 8 Sep 2010 07:33:28 +0000 (00:33 -0700)
If a declared option name starts with no-xxx, then the 'xxx' option starts
off set to True by default, so that no-xxx is False by default, so that
passing --no-xxx as an option will have the desired effect of setting
--no-xxx=True (and thus --xxx=False).

Previously, trying to list a --no-xxx option in the argument list would
trigger an assertion failure.

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
lib/bup/options.py
lib/bup/t/toptions.py

index cc185e295cef34a3cf6ef7d8fc0bbf98193d79c9..0529f5983c5b8fe11a00f3de559927712499e6f7 100644 (file)
@@ -11,9 +11,14 @@ class OptDict:
         self._opts = {}
 
     def __setitem__(self, k, v):
+        if k.startswith('no-') or k.startswith('no_'):
+            k = k[3:]
+            v = not v
         self._opts[k] = v
 
     def __getitem__(self, k):
+        if k.startswith('no-') or k.startswith('no_'):
+            return not self._opts[k[3:]]
         return self._opts[k]
 
     def __getattr__(self, k):
@@ -34,6 +39,15 @@ def _intify(v):
     return v
 
 
+def _remove_negative_kv(k, v):
+    if k.startswith('no-') or k.startswith('no_'):
+        return k[3:], not v
+    return k,v
+
+def _remove_negative_k(k):
+    return _remove_negative_kv(k, None)[0]
+
+
 class Options:
     """Option parser.
     When constructed, two strings are mandatory. The first one is the command
@@ -91,16 +105,16 @@ class Options:
                 flagl = flags.split(',')
                 flagl_nice = []
                 for f in flagl:
-                    self._aliases[f] = flagl[0]
+                    f,dvi = _remove_negative_kv(f, _intify(defval))
+                    self._aliases[f] = _remove_negative_k(flagl[0])
                     self._hasparms[f] = has_parm
-                    self._defaults[f] = _intify(defval)
+                    self._defaults[f] = dvi
                     if len(f) == 1:
                         self._shortopts += f + (has_parm and ':' or '')
                         flagl_nice.append('-' + f)
                     else:
                         f_nice = re.sub(r'\W', '_', f)
-                        self._aliases[f_nice] = flagl[0]
-                        assert(not f.startswith('no-')) # supported implicitly
+                        self._aliases[f_nice] = _remove_negative_k(flagl[0])
                         self._longopts.append(f + (has_parm and '=' or ''))
                         self._longopts.append('no-' + f)
                         flagl_nice.append('--' + f)
index da78995d2fe9af87752b2288b6c3356df7d3d491..d7077f6b5f41c315864f72e665833c207968e0b4 100644 (file)
@@ -8,9 +8,12 @@ def test_optdict():
     d['x'] = 5
     d['y'] = 4
     d['z'] = 99
+    d['no_other_thing'] = 5
     WVPASSEQ(d.x, 5)
     WVPASSEQ(d.y, 4)
     WVPASSEQ(d.z, 99)
+    WVPASSEQ(d.no_z, False)
+    WVPASSEQ(d.no_other_thing, True)
     try:
         print d.p
     except:
@@ -29,6 +32,7 @@ l,longoption=   long option with parameters and a really really long description
 p= short option with parameters
 onlylong  long option with no short
 neveropt never called options
+no-stupid  disable stupidity
 """
 
 @wvtest
@@ -45,5 +49,6 @@ 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))
+    WVPASSEQ((opt.stupid, opt.no_stupid), (True, False))
     (opt,flags,extra) = o.parse(['--onlylong', '-t', '--no-onlylong'])
     WVPASSEQ((opt.t, opt.q, opt.onlylong), (1, None, 0))