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):
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
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)
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:
p= short option with parameters
onlylong long option with no short
neveropt never called options
+no-stupid disable stupidity
"""
@wvtest
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))