Fix baker command parsing

This commit is contained in:
Bastian Kleineidam 2010-03-10 20:48:47 +01:00
parent c4c9eceb28
commit c7570293db
3 changed files with 43 additions and 15 deletions

View File

@ -2,6 +2,7 @@
* Fix path error by using absolute pathname for archive when changing * Fix path error by using absolute pathname for archive when changing
the current working directory to the unpack directory. the current working directory to the unpack directory.
* Fix parsing of the "-v" short option.
0.7 "3000 Miles to Graceland" (released 8.3.2010) 0.7 "3000 Miles to Graceland" (released 8.3.2010)

View File

@ -120,6 +120,7 @@ class Cmd(object):
self.has_kwargs = has_kwargs self.has_kwargs = has_kwargs
self.docstring = docstring self.docstring = docstring
self.paramdocs = paramdocs self.paramdocs = paramdocs
self.param_keywords = False
class Baker(object): class Baker(object):
@ -189,8 +190,10 @@ class Baker(object):
# Zip up the keyword argument names with their defaults # Zip up the keyword argument names with their defaults
keywords = dict(zip(arglist[0-len(defaults):], defaults)) keywords = dict(zip(arglist[0-len(defaults):], defaults))
elif has_kwargs: elif has_kwargs:
# allow keyword arguments specified by params # Allow keyword arguments specified by params.
keywords = dict(zip(params.keys(), [""]*len(params))) keywords = dict(zip(params.keys(), [""]*len(params)))
# But set a flag to detect this
self.param_keywords = True
else: else:
keywords = {} keywords = {}
@ -350,14 +353,14 @@ class Baker(object):
# Take the next argument # Take the next argument
arg = argv.pop(0) arg = argv.pop(0)
if arg == "-": if arg == "--":
# All arguments following a single hyphen are treated as # All arguments following a double hyphen are treated as
# positional arguments # positional arguments
vargs.extend(argv) vargs.extend(argv)
break break
elif arg == "--": elif arg == "-":
# What to do with a bare --? Right now, it's ignored. # What to do with a bare -? Right now, it's ignored.
continue continue
elif arg.startswith("--"): elif arg.startswith("--"):
@ -384,9 +387,19 @@ class Baker(object):
# opposite of the default". # opposite of the default".
value = not default value = not default
else: else:
# Option is specified via the params value, assuming # The next item in the argument list is the value, i.e.
# a True value # --keyword value
value = True if not argv or argv[0].startswith("-") or self.param_keywords:
# There isn't a value available, or its derived
# from parameters. Just use True, assuming this
# is a flag.
value = True
else:
value = argv.pop(0)
try:
value = totype(value, default)
except TypeError:
pass
# Store this option # Store this option
kwargs[name] = value kwargs[name] = value
@ -411,15 +424,23 @@ class Baker(object):
kwargs[name] = not default kwargs[name] = not default
else: else:
# This option requires a value... # This option requires a value...
if i == len(arg)-1: if self.param_keywords:
# This is the last character in the list, so the # It's derived from parameters. Just use True,
# next argument on the command line is the value. # assuming this is a flag.
value = argv.pop(0) value = True
else: elif i < len(arg)-1:
# There are other characters after this one, so # There are other characters after this one, so
# the rest of the characters must represent the # the rest of the characters must represent the
# value (i.e. old-style UNIX option like -Nname) # value (i.e. old-style UNIX option like -Nname)
value = totype(arg[i+1:], default) value = totype(arg[i+1:], default)
elif argv:
# This is the last character in the list, so the
# next argument on the command line is the value.
value = argv.pop(0)
else:
# There is no value available. Just use True,
# assuming this is a flag.
value = True
try: try:
kwargs[name] = totype(value, default) kwargs[name] = totype(value, default)

View File

@ -48,8 +48,14 @@ class TestBaker (unittest.TestCase):
self.assertEqual(res, ('argvalue1', 'argvalue2', True)) self.assertEqual(res, ('argvalue1', 'argvalue2', True))
def test_func_kwargs_params (self): def test_func_kwargs_params (self):
@baker.command(params={"verbose": "Be verbose"}) @baker.command(shortopts={"verbose": "v"}, params={"verbose": "Be verbose"})
def func(*args, **kwargs): def func(*args, **kwargs):
return kwargs['verbose'] return kwargs['verbose']
res = baker.run(argv=[__file__, 'func', '--verbose']) res = baker.run(argv=[__file__, 'func', '--verbose', 'arg1'])
self.assertEqual(res, True)
res = baker.run(argv=[__file__, 'func', 'arg1', '--verbose'])
self.assertEqual(res, True)
res = baker.run(argv=[__file__, 'func', '-v'])
self.assertEqual(res, True)
res = baker.run(argv=[__file__, 'func', '-v', 'arg1'])
self.assertEqual(res, True) self.assertEqual(res, True)