1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
import sys
import pytest
from cyclopts import MissingArgumentError, UnknownOptionError
@pytest.mark.parametrize(
"cmd_str",
[
"foo 1 2 3 4 5",
],
)
def test_star_args(app, cmd_str, assert_parse_args):
@app.command
def foo(a: int, b: int, *args: int):
pass
assert_parse_args(foo, cmd_str, 1, 2, 3, 4, 5)
@pytest.mark.parametrize(
"cmd_str",
[
"foo 1 2 3",
],
)
def test_pos_only(app, cmd_str, assert_parse_args):
@app.command
def foo(a: int, b: int, c: int, /):
pass
assert_parse_args(foo, cmd_str, 1, 2, 3)
@pytest.mark.parametrize(
"cmd_str_e",
[
("foo 1 2 --c=3", UnknownOptionError), # Unknown option "--c"
],
)
def test_pos_only_exceptions(app, cmd_str_e):
cmd_str, e = cmd_str_e
@app.command
def foo(a: int, b: int, c: int, /):
pass
with pytest.raises(e):
app.parse_args(cmd_str, print_error=False, exit_on_error=False)
@pytest.mark.parametrize(
"cmd_str",
[
"foo 1 2 3 4",
"foo 1 2 3 --d 4",
"foo 1 2 --d=4 3",
],
)
def test_pos_only_extended(app, cmd_str, assert_parse_args):
@app.command
def foo(a: int, b: int, c: int, /, d: int):
pass
assert_parse_args(foo, cmd_str, 1, 2, 3, 4)
@pytest.mark.parametrize(
"cmd_str_e",
[
("foo 1 2 3", MissingArgumentError),
("foo 1 2", MissingArgumentError),
],
)
def test_pos_only_extended_exceptions(app, cmd_str_e):
cmd_str, e = cmd_str_e
@app.command
def foo(a: int, b: int, c: int, /, d: int):
pass
with pytest.raises(e):
app.parse_args(cmd_str, print_error=False, exit_on_error=False)
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="https://peps.python.org/pep-0563/ Postponed Evaluation of Annotations"
)
@pytest.mark.parametrize(
"cmd_str",
[
"foo a 2 3 4",
"foo a 2 3 --d 4",
"foo a 2 --d=4 3",
],
)
def test_pos_only_extended_str_type(app, cmd_str, assert_parse_args):
@app.command
def foo(a: "str", b: "int", c: int, /, d: "int"):
pass
assert_parse_args(foo, cmd_str, "a", 2, 3, 4)
|