Skip to content

Commit

Permalink
More fixes for parsing list of tuples with insufficient arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Feb 21, 2024
1 parent cb52571 commit 1bb78a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
23 changes: 8 additions & 15 deletions cyclopts/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import shlex
import sys
from contextlib import suppress
from typing import Iterable, List, Tuple, Union

from cyclopts._convert import token_count
Expand Down Expand Up @@ -95,33 +96,25 @@ def _parse_kw_and_flags(command: ResolvedCommand, tokens, mapping):
else:
tokens_per_element, consume_all = token_count(iparam)

if consume_all:
j = 0 # Makes pyright[reportUnboundVariable] happy
try:
with suppress(IndexError):
if consume_all:
for j in itertools.count():
token = tokens[i + 1 + j]
if not cparam.allow_leading_hyphen and _is_option_like(token):
break
cli_values.append(token)
skip_next_iterations += 1
except IndexError:
if j == 0:
raise MissingArgumentError(parameter=iparam, tokens_so_far=cli_values) from None
elif j % tokens_per_element != 0:
raise MissingArgumentError(parameter=iparam, tokens_so_far=cli_values) from None
else:
consume_count += tokens_per_element
try:
else:
consume_count += tokens_per_element
for j in range(consume_count):
token = tokens[i + 1 + j]

if not cparam.allow_leading_hyphen:
_validate_is_not_option_like(token)

cli_values.append(token)
skip_next_iterations += 1
except IndexError:
raise MissingArgumentError(parameter=iparam, tokens_so_far=cli_values) from None

if not cli_values or len(cli_values) % tokens_per_element:
raise MissingArgumentError(parameter=iparam, tokens_so_far=cli_values)

# Update mapping
if iparam is kwargs_iparam:
Expand Down
23 changes: 18 additions & 5 deletions tests/test_bind_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,36 @@ def foo(a: Optional[List[int]] = None):
assert_parse_args(foo, "foo")


def test_list_tuple_missing_arguments_no_arguments(app, assert_parse_args):
@pytest.mark.parametrize(
"cmd",
[
"foo --item",
],
)
def test_list_tuple_missing_arguments_no_arguments(app, cmd):
"""Missing values."""

@app.command
def foo(item: List[Tuple[int, str]]):
pass

with pytest.raises(MissingArgumentError):
app("foo --item", exit_on_error=False)
app(cmd, exit_on_error=False)


def test_list_tuple_missing_arguments_non_divisible(app, assert_parse_args):
@pytest.mark.parametrize(
"cmd",
[
"foo --item 1 alice 2",
"foo --item a --stuff g",
],
)
def test_list_tuple_missing_arguments_non_divisible(app, cmd):
"""Missing values."""

@app.command
def foo(item: List[Tuple[int, str]]):
def foo(item: List[Tuple[int, str]], stuff: str = ""):
pass

with pytest.raises(MissingArgumentError):
app("foo --item 1 alice 2", exit_on_error=False)
app(cmd, exit_on_error=False)

0 comments on commit 1bb78a5

Please sign in to comment.