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