1. Matt Chaput
  2. baker
  3. Issues
Issue #26 new

Command with shortopts and kwargs failed in 1.3

Kamal Mustafa
created an issue

I have code that working fine in 1.1 but failed when updating to 1.3. It produce the following error:-

{{{

!python

baker.run() File "/usr/local/lib/python2.6/dist-packages/Baker-1.3-py2.6.egg/baker.py", line 823, in run value = self.apply(*self.parse(argv), instance=instance) File "/usr/local/lib/python2.6/dist-packages/Baker-1.3-py2.6.egg/baker.py", line 739, in parse args, kwargs = self.parse_args(scriptname, cmd, options, test=test) File "/usr/local/lib/python2.6/dist-packages/Baker-1.3-py2.6.egg/baker.py", line 662, in parse_args value = argv.pop(0) IndexError: pop from empty list }}}

You can verify this with the following test:-

{{{

!python

def test_shortopts_with_kwargs(self): """Test short options with kwargs""" b = baker.Baker()

    @b.command(shortopts={"alfa": "a", "bravo": "b", "charlie": "c"})
    def test(alfa="1", bravo="2", charlie=False, **kwargs):
        return (alfa, bravo, charlie)

    self.assertEqual(b.run(["s", "test", "-a", "100", "-cb200"],
                           main=False),
                     ("100", "200", True))

}}}

Looking further, look like there's some changes in how Baker get the value for shortopts in 1.3. In 1.1, the code in parse_args look like this:-

{{{

!python

            # For each character after the '-'...
            for i in xrange(1, len(arg)):
                char = arg[i]
                if char not in shortchars:
                    continue

                # Get the long option name corresponding to this char
                name = shortchars[char]

}}}

But in 1.3, it has changed to:-

{{{

!python

# For each character after the '-'... for i in range(1, len(arg)): char = arg[i] if cmd.has_kwargs: name = char default = keywords.get(name) elif char not in shortchars: continue else: # Get the long option name corresponding to this char name = shortchars[char] default = keywords[name] }}}

It look for value in keywords if cmd.has_kwargs is True but instead of using shortchars[char] as key, it use char. I try to fix this by doing:-

{{{

!python

                if cmd.has_kwargs:
                    name = char
                    default = keywords.get(name) or shortchars.get(name)

}}}

While the above tests pass, another tests failed:-

{{{

!python

FAIL: Test args and *kwargs with default arguments

Traceback (most recent call last): File "tests/test_baker.py", line 287, in test_defaulted_args_and_kwargs (1, {"b": "2"})) File "tests/test_baker.py", line 164, in assertEqual super(TestBaker, self).assertEqual(a, b) AssertionError: ('1', {'b': '2'}) != (1, {'b': '2'}) }}}

Comments (1)

  1. Log in to comment