add short options to getopts. single letter options will work with both - or --

This commit is contained in:
Dan 2014-01-06 23:03:25 +01:00
parent f32f36fea4
commit 54196be509

View File

@ -971,11 +971,18 @@ class getopts(context):
self.spec = getopts # for repr self.spec = getopts # for repr
self.getopts = {} self.getopts = {}
self.getoptL = [] self.getoptL = []
self.getoptLs = ''
for (name, spec) in getopts.iteritems(): for (name, spec) in getopts.iteritems():
if spec == '': if spec == '':
if len(name) == 1:
self.getoptLs += name
self.getopts[name] = None
self.getoptL.append(name) self.getoptL.append(name)
self.getopts[name] = None self.getopts[name] = None
else: else:
if len(name) == 1:
self.getoptLs += name + ':'
self.getopts[name] = contextify(spec)
self.getoptL.append(name + '=') self.getoptL.append(name + '=')
self.getopts[name] = contextify(spec) self.getopts[name] = contextify(spec)
log.debug('getopts: %r', self.getopts) log.debug('getopts: %r', self.getopts)
@ -983,10 +990,13 @@ class getopts(context):
def __call__(self, irc, msg, args, state): def __call__(self, irc, msg, args, state):
log.debug('args before %r: %r', self, args) log.debug('args before %r: %r', self, args)
(optlist, rest) = getopt.getopt(args, '', self.getoptL) (optlist, rest) = getopt.getopt(args, self.getoptLs, self.getoptL)
getopts = [] getopts = []
for (opt, arg) in optlist: for (opt, arg) in optlist:
if opt.startswith('--'):
opt = opt[2:] # Strip -- opt = opt[2:] # Strip --
else:
opt = opt[1:]
log.debug('opt: %r, arg: %r', opt, arg) log.debug('opt: %r, arg: %r', opt, arg)
context = self.getopts[opt] context = self.getopts[opt]
if context is not None: if context is not None: