| 1 | from typing import Annotated |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import cyclopts |
| 6 | from cyclopts import Parameter |
| 7 | from cyclopts.exceptions import RequiresEqualsError |
| 8 | |
| 9 | |
| 10 | def test_requires_equals_with_equals(app, assert_parse_args): |
| 11 | """--option=value should work with requires_equals=True.""" |
| 12 | |
| 13 | @app.default |
| 14 | def main(*, name: Annotated[str, Parameter(requires_equals=True)]): |
| 15 | pass |
| 16 | |
| 17 | assert_parse_args(main, "--name=alice", name="alice") |
| 18 | |
| 19 | |
| 20 | def test_requires_equals_space_separated_rejected(app): |
| 21 | """--option value should be rejected with requires_equals=True.""" |
| 22 | |
| 23 | @app.default |
| 24 | def main(*, name: Annotated[str, Parameter(requires_equals=True)]): |
| 25 | pass |
| 26 | |
| 27 | with pytest.raises(RequiresEqualsError): |
| 28 | app.parse_args("--name alice", print_error=False, exit_on_error=False) |
| 29 | |
| 30 | |
| 31 | def test_requires_equals_missing_value(app): |
| 32 | """--option with no value at all should be rejected with requires_equals=True.""" |
| 33 | |
| 34 | @app.default |
| 35 | def main(*, name: Annotated[str, Parameter(requires_equals=True)]): |
| 36 | pass |
| 37 | |
| 38 | with pytest.raises(RequiresEqualsError): |
| 39 | app.parse_args("--name", print_error=False, exit_on_error=False) |
| 40 | |
| 41 | |
| 42 | def test_requires_equals_boolean_flag_unaffected(app, assert_parse_args): |
| 43 | """Boolean flags should work regardless of requires_equals.""" |
| 44 | |
| 45 | @app.default |
| 46 | def main(*, verbose: Annotated[bool, Parameter(requires_equals=True)] = False): |
| 47 | pass |
| 48 | |
| 49 | assert_parse_args(main, "--verbose", verbose=True) |
| 50 | |
| 51 | |
| 52 | def test_requires_equals_short_option_unaffected(app, assert_parse_args): |
| 53 | """Short options (-o value) should not be affected by requires_equals.""" |
| 54 | |
| 55 | @app.default |
| 56 | def main(*, name: Annotated[str, Parameter(name=["-n", "--name"], requires_equals=True)]): |
| 57 | pass |
| 58 | |
| 59 | assert_parse_args(main, "-n alice", name="alice") |
| 60 | |
| 61 | |
| 62 | def test_requires_equals_short_option_long_rejected(app): |
| 63 | """The long form should still be rejected when using space separation.""" |
| 64 | |
| 65 | @app.default |
| 66 | def main(*, name: Annotated[str, Parameter(name=["-n", "--name"], requires_equals=True)]): |
| 67 | pass |
| 68 | |
| 69 | with pytest.raises(RequiresEqualsError): |
| 70 | app.parse_args("--name alice", print_error=False, exit_on_error=False) |
| 71 | |
| 72 | |
| 73 | def test_requires_equals_default_false(app, assert_parse_args): |
| 74 | """Default behavior (requires_equals=False) should accept space-separated.""" |
| 75 | |
| 76 | @app.default |
| 77 | def main(*, name: str): |
| 78 | pass |
| 79 | |
| 80 | assert_parse_args(main, "--name alice", name="alice") |
| 81 | assert_parse_args(main, "--name=alice", name="alice") |
| 82 | |
| 83 | |
| 84 | def test_requires_equals_app_default_parameter(): |
| 85 | """App-wide default_parameter should apply requires_equals to all params.""" |
| 86 | app = cyclopts.App( |
| 87 | result_action="return_value", |
| 88 | default_parameter=Parameter(requires_equals=True), |
| 89 | ) |
| 90 | |
| 91 | @app.default |
| 92 | def main(*, name: str, count: int): |
| 93 | pass |
| 94 | |
| 95 | _, bound, _ = app.parse_args("--name=alice --count=3", print_error=False, exit_on_error=False) |
| 96 | assert bound.arguments == {"name": "alice", "count": 3} |
| 97 | |
| 98 | |
| 99 | def test_requires_equals_app_default_parameter_rejects_space(): |
| 100 | """App-wide requires_equals should reject space-separated values.""" |
| 101 | app = cyclopts.App( |
| 102 | result_action="return_value", |
| 103 | default_parameter=Parameter(requires_equals=True), |
| 104 | ) |
| 105 | |
| 106 | @app.default |
| 107 | def main(*, name: str, count: int): |
| 108 | pass |
| 109 | |
| 110 | with pytest.raises(RequiresEqualsError): |
| 111 | app.parse_args("--name alice --count=3", print_error=False, exit_on_error=False) |
| 112 | |
| 113 | |
| 114 | def test_requires_equals_error_message(app): |
| 115 | """Error message should recommend using = syntax.""" |
| 116 | |
| 117 | @app.default |
| 118 | def main(*, name: Annotated[str, Parameter(requires_equals=True)]): |
| 119 | pass |
| 120 | |
| 121 | with pytest.raises(RequiresEqualsError, match=r"Use --name=VALUE"): |
| 122 | app.parse_args("--name alice", print_error=False, exit_on_error=False) |
| 123 | |
| 124 | |
| 125 | def test_requires_equals_mixed_parameters(app, assert_parse_args): |
| 126 | """Mix of requires_equals=True and default parameters.""" |
| 127 | |
| 128 | @app.default |
| 129 | def main( |
| 130 | *, |
| 131 | strict: Annotated[str, Parameter(requires_equals=True)], |
| 132 | loose: str, |
| 133 | ): |
| 134 | pass |
| 135 | |
| 136 | assert_parse_args(main, "--strict=hello --loose world", strict="hello", loose="world") |
| 137 | |
| 138 | |
| 139 | def test_requires_equals_mixed_parameters_reject(app): |
| 140 | """requires_equals only applies to the parameter it's set on.""" |
| 141 | |
| 142 | @app.default |
| 143 | def main( |
| 144 | *, |
| 145 | strict: Annotated[str, Parameter(requires_equals=True)], |
| 146 | loose: str, |
| 147 | ): |
| 148 | pass |
| 149 | |
| 150 | with pytest.raises(RequiresEqualsError): |
| 151 | app.parse_args("--strict hello --loose world", print_error=False, exit_on_error=False) |
| 152 | |
| 153 | |
| 154 | def test_requires_equals_and_consume_multiple_mutually_exclusive(app): |
| 155 | """requires_equals and consume_multiple cannot be used together.""" |
| 156 | |
| 157 | @app.default |
| 158 | def main(*, urls: Annotated[list[str], Parameter(requires_equals=True, consume_multiple=True)]): |
| 159 | pass |
| 160 | |
| 161 | with pytest.raises(ValueError, match="cannot be used together"): |
| 162 | app.parse_args("--urls=a", print_error=False, exit_on_error=False) |
| 163 | |
| 164 | |
| 165 | def test_requires_equals_and_consume_multiple_int_mutually_exclusive(app): |
| 166 | """requires_equals and consume_multiple=1 cannot be used together.""" |
| 167 | |
| 168 | @app.default |
| 169 | def main(*, urls: Annotated[list[str], Parameter(requires_equals=True, consume_multiple=1)]): |
| 170 | pass |
| 171 | |
| 172 | with pytest.raises(ValueError, match="cannot be used together"): |
| 173 | app.parse_args("--urls=a", print_error=False, exit_on_error=False) |
| 174 | |
| 175 | |
| 176 | def test_requires_equals_and_consume_multiple_tuple_mutually_exclusive(app): |
| 177 | """requires_equals and consume_multiple=(1, 3) cannot be used together.""" |
| 178 | |
| 179 | @app.default |
| 180 | def main(*, urls: Annotated[list[str], Parameter(requires_equals=True, consume_multiple=(1, 3))]): |
| 181 | pass |
| 182 | |
| 183 | with pytest.raises(ValueError, match="cannot be used together"): |
| 184 | app.parse_args("--urls=a", print_error=False, exit_on_error=False) |
| 185 | |
| 186 | |
| 187 | def test_requires_equals_consume_multiple_repeated_equals(app): |
| 188 | """With requires_equals, list values can be provided by repeating --option=value.""" |
| 189 | |
| 190 | @app.default |
| 191 | def main(*, urls: Annotated[list[str], Parameter(requires_equals=True)]): |
| 192 | pass |
| 193 | |
| 194 | _, bound, _ = app.parse_args("--urls=a --urls=b --urls=c", print_error=False, exit_on_error=False) |
| 195 | assert bound.arguments == {"urls": ["a", "b", "c"]} |