Skip to content
Myr

Fix bug with multiple args after star arg

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Testcase taken from #336, thanks @ckamradt Also fixed a potential bug hazard inside _fetch, what if the fetched value is false-ish? Fix #316 Fix #336 Original commit message: >Addresses issue #316. Previous behavior only worked if there was a >single arg specified after an arg with nargs=-1.

authored by Chris Kamradt · committed by Markus Unterwaditzer · parent 3ddc556 · Browse files · 2 files+41 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+41 added−1 removed

  • tests:+34 added−0 removed
  • other:+7 added−1 removed

2 files+41 added−1 removed

modified:click/utils.py+7 added-1 removed
36 unmodified lines
37 >>> unpack_args(range(6), [-1])38 (((0, 1, 2, 3, 4, 5),), [])39 >>> unpack_args(range(6), [1, 1])40 ((0, 1), [2, 3, 4, 5])41 >>> unpack_args(range(6), [-1,1,1,1,1])42 (((0, 1), 5, 4, 3, 2), [])43 """44 args = deque(args)45 nargs_spec = deque(nargs_spec)46 rv = []47 spos = None4849 def _fetch(c):50 try:49 return (spos is not None and c.pop() or c.popleft())51 if spos is None:52 return c.popleft()53 else:54 return c.pop()55 except IndexError:56 return None5758 while nargs_spec:
17 unmodified lines
76 # we fill it with the remainder.77 if spos is not None:78 rv[spos] = tuple(args)79 args = []80 rv[spos + 1:] = reversed(rv[spos + 1:])8182 return tuple(rv), list(args)8384
352 unmodified lines
modified:tests/test_arguments.py+34 added
208 unmodified lines
209 '-foo',210 'bar',211 '-x',212 ]213214215def test_nargs_star_ordering(runner):216 @click.command()217 @click.argument('a', nargs=-1)218 @click.argument('b')219 @click.argument('c')220 def cmd(a, b, c):221 for arg in (a, b, c):222 click.echo(arg)223224 result = runner.invoke(cmd, ['a', 'b', 'c'])225 assert result.output.splitlines() == [226 "('a',)",227 'b',228 'c',229 ]230231232def test_nargs_specified_plus_star_ordering(runner):233 @click.command()234 @click.argument('a', nargs=-1)235 @click.argument('b')236 @click.argument('c', nargs=2)237 def cmd(a, b, c):238 for arg in (a, b, c):239 click.echo(arg)240241 result = runner.invoke(cmd, ['a', 'b', 'c', 'd', 'e', 'f'])242 assert result.output.splitlines() == [243 "('a', 'b', 'c')",244 'd',245 "('e', 'f')",246 ]