]> arthur.barton.de Git - bup.git/commitdiff
options parser: automatically convert strings to ints when appropriate.
authorAvery Pennarun <apenwarr@gmail.com>
Tue, 12 Jan 2010 03:59:46 +0000 (22:59 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Tue, 12 Jan 2010 03:59:46 +0000 (22:59 -0500)
If the given parameter is exactly an int (ie. str(int(v)) == v) then convert
it to an int automatically.  This helps avoid weird bugs in apps using the
option parser.

options.py
t/toptions.py

index eab10c5198b94a3f7694aff8c0bc5c3c39d9ab5d..54ce8af15e7af2159184e3feec2248295bc31f74 100644 (file)
@@ -104,6 +104,12 @@ class Options:
                     assert(v == '')
                     opt[k] = (opt._opts.get(k) or 0) + 1
                 else:
+                    try:
+                        vv = int(v)
+                        if str(vv) == v:
+                            v = vv
+                    except ValueError:
+                        pass
                     opt[k] = v
         for (f1,f2) in self._aliases.items():
             opt[f1] = opt[f2]
index 221756facdbeeccc2e7cc32c227f9218492566b8..937cf707d5339adda34190df683a8716945cdd88 100644 (file)
@@ -44,6 +44,6 @@ def test_options():
     WVPASSEQ(flags[5], ('--longoption', '19'))
     WVPASSEQ(extra, ['hanky'])
     WVPASSEQ((opt.t, opt.q, opt.p, opt.l, opt.onlylong,
-              opt.neveropt), (3,1,7,'19',1,None))
+              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))