| 1 |
from typing import Annotated, Any |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import ( |
| 6 |
Group, |
| 7 |
Parameter, |
| 8 |
UnknownOptionError, |
| 9 |
) |
| 10 |
from cyclopts.exceptions import CoercionError |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
"cmd_str,expected", |
| 15 |
[ |
| 16 |
("--my-flag", True), |
| 17 |
("--my-flag=true", True), |
| 18 |
("--my-flag=false", False), |
| 19 |
("--no-my-flag", False), |
| 20 |
], |
| 21 |
) |
| 22 |
def test_boolean_flag_default(app, cmd_str, expected, assert_parse_args): |
| 23 |
@app.default |
| 24 |
def foo(my_flag: bool = True): |
| 25 |
pass |
| 26 |
|
| 27 |
assert_parse_args(foo, cmd_str, expected) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize( |
| 31 |
"cmd_str,expected", |
| 32 |
[ |
| 33 |
("--my-flag", True), |
| 34 |
("--my-flag=true", True), |
| 35 |
("--my-flag=false", False), |
| 36 |
("--no-my-flag", False), |
| 37 |
], |
| 38 |
) |
| 39 |
def test_boolean_flag_unannotated_default(app, cmd_str, expected, assert_parse_args): |
| 40 |
"""https://github.com/BrianPugh/cyclopts/issues/797 |
| 41 |
|
| 42 |
A parameter whose type is inferred from a boolean default (no annotation) |
| 43 |
should still get a generated negative ``--no-`` flag. |
| 44 |
""" |
| 45 |
|
| 46 |
@app.default |
| 47 |
def foo(my_flag=False): |
| 48 |
pass |
| 49 |
|
| 50 |
assert_parse_args(foo, cmd_str, expected) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
"cmd_str,expected", |
| 55 |
[ |
| 56 |
("--my-flag", True), |
| 57 |
("--my-flag=true", True), |
| 58 |
("--my-flag=false", False), |
| 59 |
("--no-my-flag", False), |
| 60 |
], |
| 61 |
) |
| 62 |
def test_boolean_flag_any_annotation_default(app, cmd_str, expected, assert_parse_args): |
| 63 |
"""``Any`` annotation with a bool default should behave like an unannotated bool default. |
| 64 |
|
| 65 |
``FieldInfo.hint`` already infers ``type(default)`` when the annotation resolves to ``Any``, |
| 66 |
so the parsing path treats this as ``bool``; the negative-flag path should match. |
| 67 |
""" |
| 68 |
|
| 69 |
@app.default |
| 70 |
def foo(my_flag: Any = False): |
| 71 |
pass |
| 72 |
|
| 73 |
assert_parse_args(foo, cmd_str, expected) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize( |
| 77 |
"cmd_str,expected", |
| 78 |
[ |
| 79 |
("--my-flag", True), |
| 80 |
("--my-flag=true", True), |
| 81 |
("--my-flag=false", False), |
| 82 |
("--no-my-flag", False), |
| 83 |
("--your-flag", True), |
| 84 |
("--no-your-flag", False), |
| 85 |
("-m", True), |
| 86 |
], |
| 87 |
) |
| 88 |
def test_boolean_flag_alias(app, cmd_str, expected, assert_parse_args): |
| 89 |
@app.default |
| 90 |
def foo(my_flag: Annotated[bool, Parameter(alias=["--your-flag", "-m"])] = True): |
| 91 |
pass |
| 92 |
|
| 93 |
assert_parse_args(foo, cmd_str, expected) |
| 94 |
|
| 95 |
|
| 96 |
def test_boolean_flag_app_parameter_default(app, assert_parse_args): |
| 97 |
app.default_parameter = Parameter(negative="") |
| 98 |
|
| 99 |
@app.default |
| 100 |
def foo(my_flag: bool = True): |
| 101 |
pass |
| 102 |
|
| 103 |
|
| 104 |
assert_parse_args(foo, "--my-flag", True) |
| 105 |
|
| 106 |
with pytest.raises(UnknownOptionError) as e: |
| 107 |
app.parse_args("--no-my-flag", exit_on_error=False) |
| 108 |
assert str(e.value) == "Unknown option: --no-my-flag. Did you mean --my-flag?" |
| 109 |
|
| 110 |
|
| 111 |
def test_boolean_flag_app_parameter_negative(app, assert_parse_args): |
| 112 |
@app.default |
| 113 |
def foo(my_flag: Annotated[bool, Parameter("", negative="--no-my-flag")] = True): |
| 114 |
pass |
| 115 |
|
| 116 |
assert_parse_args(foo, "--no-my-flag", False) |
| 117 |
|
| 118 |
with pytest.raises(UnknownOptionError): |
| 119 |
app.parse_args("--my-flag", exit_on_error=False, print_error=True) |
| 120 |
|
| 121 |
assert_parse_args(foo, "--no-my-flag=True", False) |
| 122 |
assert_parse_args(foo, "--no-my-flag=False", True) |
| 123 |
|
| 124 |
with pytest.raises(CoercionError): |
| 125 |
app("--no-my-flag=", exit_on_error=False) |
| 126 |
|
| 127 |
|
| 128 |
def test_boolean_flag_app_parameter_default_annotated_override(app, assert_parse_args): |
| 129 |
app.default_parameter = Parameter(negative="") |
| 130 |
|
| 131 |
@app.default |
| 132 |
def foo(my_flag: Annotated[bool, Parameter(negative="--NO-flag")] = True): |
| 133 |
pass |
| 134 |
|
| 135 |
assert_parse_args(foo, "--my-flag", True) |
| 136 |
assert_parse_args(foo, "--NO-flag", False) |
| 137 |
|
| 138 |
|
| 139 |
def test_boolean_flag_app_parameter_default_nested_annotated_override(app, assert_parse_args): |
| 140 |
app.default_parameter = Parameter(negative="") |
| 141 |
|
| 142 |
def my_converter(type_, tokens): |
| 143 |
return 5 |
| 144 |
|
| 145 |
my_int = Annotated[int, Parameter(converter=my_converter)] |
| 146 |
|
| 147 |
@app.default |
| 148 |
def foo(*, foo: Annotated[my_int, Parameter(name="--bar")] = True): |
| 149 |
pass |
| 150 |
|
| 151 |
assert_parse_args(foo, "--bar=10", foo=5) |
| 152 |
|
| 153 |
|
| 154 |
def test_boolean_flag_group_default_parameter_resolution_1(app, assert_parse_args): |
| 155 |
food_group = Group("Food", default_parameter=Parameter(negative_bool="group-")) |
| 156 |
|
| 157 |
@app.default |
| 158 |
def foo(flag: Annotated[bool, Parameter(group=food_group)]): |
| 159 |
pass |
| 160 |
|
| 161 |
assert_parse_args(foo, "--group-flag", False) |
| 162 |
|
| 163 |
|
| 164 |
@pytest.mark.parametrize( |
| 165 |
"cmd_str,expected", |
| 166 |
[ |
| 167 |
("--bar", True), |
| 168 |
("--no-bar", False), |
| 169 |
], |
| 170 |
) |
| 171 |
def test_boolean_flag_custom_positive(app, cmd_str, expected, assert_parse_args): |
| 172 |
@app.default |
| 173 |
def foo(my_flag: Annotated[bool, Parameter(name="--bar")] = True): |
| 174 |
pass |
| 175 |
|
| 176 |
assert_parse_args(foo, cmd_str, expected) |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.parametrize( |
| 180 |
"cmd_str,expected", |
| 181 |
[ |
| 182 |
("--bar", True), |
| 183 |
("--no-bar", False), |
| 184 |
], |
| 185 |
) |
| 186 |
def test_boolean_flag_custom_short_positive(app, cmd_str, expected, assert_parse_args): |
| 187 |
@app.default |
| 188 |
def foo(my_flag: Annotated[bool, Parameter(name=["--bar", "-b"])] = True): |
| 189 |
pass |
| 190 |
|
| 191 |
assert_parse_args(foo, cmd_str, expected) |
| 192 |
|
| 193 |
|
| 194 |
@pytest.mark.parametrize( |
| 195 |
"cmd_str,expected", |
| 196 |
[ |
| 197 |
("--my-flag", True), |
| 198 |
("--yesnt-my-flag", False), |
| 199 |
], |
| 200 |
) |
| 201 |
def test_boolean_flag_custom_negative(app, cmd_str, expected, assert_parse_args): |
| 202 |
@app.default |
| 203 |
def foo(my_flag: Annotated[bool, Parameter(negative="--yesnt-my-flag")] = True): |
| 204 |
pass |
| 205 |
|
| 206 |
assert_parse_args(foo, cmd_str, expected) |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.parametrize( |
| 210 |
"negative", |
| 211 |
["", (), []], |
| 212 |
) |
| 213 |
def test_boolean_flag_disable_negative(app, negative, assert_parse_args): |
| 214 |
@app.default |
| 215 |
def foo(my_flag: Annotated[bool, Parameter(negative=negative)] = True): |
| 216 |
pass |
| 217 |
|
| 218 |
assert_parse_args(foo, "--my-flag", True) |
| 219 |
with pytest.raises(UnknownOptionError): |
| 220 |
assert_parse_args(foo, "--no-my-flag", True) |
| 221 |
|
| 222 |
|
| 223 |
@pytest.mark.parametrize( |
| 224 |
"cmd_str,expected", |
| 225 |
[ |
| 226 |
|
| 227 |
("--my-flag", True), |
| 228 |
("--my_flag", True), |
| 229 |
|
| 230 |
("--no-my-flag", False), |
| 231 |
("--no_my_flag", False), |
| 232 |
("--no-my_flag", False), |
| 233 |
], |
| 234 |
) |
| 235 |
def test_boolean_flag_snake_case_negative(app, cmd_str, expected, assert_parse_args): |
| 236 |
"""Test that negative boolean flags accept both kebab-case and snake_case. |
| 237 |
|
| 238 |
Issue #692: Cyclopts accepts both `--some-flag` and `--some_flag` for |
| 239 |
positive flags, but only accepts `--no-some-flag` for negative flags. |
| 240 |
This test ensures that `--no_some_flag` and `--no-some_flag` also work. |
| 241 |
""" |
| 242 |
|
| 243 |
@app.default |
| 244 |
def foo(my_flag: bool = True): |
| 245 |
pass |
| 246 |
|
| 247 |
assert_parse_args(foo, cmd_str, expected) |
| 248 |
|
| 249 |
|
| 250 |
@pytest.mark.parametrize( |
| 251 |
"cmd_str,expected", |
| 252 |
[ |
| 253 |
("-n", True), |
| 254 |
("--dry-run", True), |
| 255 |
("-N", False), |
| 256 |
("--no-dry-run", False), |
| 257 |
], |
| 258 |
) |
| 259 |
def test_boolean_flag_uppercase_short_negative(app, cmd_str, expected, assert_parse_args): |
| 260 |
"""Uppercase short negative flags like -N should parse correctly. |
| 261 |
|
| 262 |
https://github.com/BrianPugh/cyclopts/issues/747 |
| 263 |
""" |
| 264 |
|
| 265 |
@app.default |
| 266 |
def foo( |
| 267 |
dry_run: Annotated[ |
| 268 |
bool, |
| 269 |
Parameter( |
| 270 |
name=["-n", "--dry-run"], |
| 271 |
negative=["-N", "--no-dry-run"], |
| 272 |
), |
| 273 |
] = True, |
| 274 |
): |
| 275 |
pass |
| 276 |
|
| 277 |
assert_parse_args(foo, cmd_str, expected) |