| 1 |
from collections.abc import Sequence |
| 2 |
from dataclasses import dataclass |
| 3 |
from enum import Enum |
| 4 |
from functools import partial |
| 5 |
from textwrap import dedent |
| 6 |
from typing import Annotated, Literal, Optional, TypedDict |
| 7 |
|
| 8 |
import attrs |
| 9 |
import pytest |
| 10 |
from pydantic import BaseModel, Field |
| 11 |
|
| 12 |
from cyclopts import App, Group, Parameter |
| 13 |
from cyclopts.argument import ArgumentCollection |
| 14 |
from cyclopts.exceptions import CoercionError, MissingArgumentError |
| 15 |
from cyclopts.group_extractors import RegisteredCommand |
| 16 |
from cyclopts.help import ( |
| 17 |
HelpPanel, |
| 18 |
create_parameter_help_panel, |
| 19 |
format_command_entries, |
| 20 |
format_usage, |
| 21 |
) |
| 22 |
from cyclopts.help.formatters import DefaultFormatter, PlainFormatter |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture |
| 26 |
def app(): |
| 27 |
return App( |
| 28 |
name="app", |
| 29 |
help="App Help String Line 1.", |
| 30 |
result_action="return_value", |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
def test_empty_help_panel_rich_silent(console): |
| 35 |
help_panel = HelpPanel(format="command", title="test") |
| 36 |
formatter = DefaultFormatter() |
| 37 |
rendered = formatter._render_panel(help_panel, console, console.options) |
| 38 |
|
| 39 |
with console.capture() as capture: |
| 40 |
console.print(rendered) |
| 41 |
|
| 42 |
actual = capture.get() |
| 43 |
assert actual == "" |
| 44 |
|
| 45 |
|
| 46 |
def test_help_mutable_default(app): |
| 47 |
"""Ensures it doesn't crash; see issue #215.""" |
| 48 |
|
| 49 |
@app.default |
| 50 |
def main(users: list[str] = ["a", "b"]) -> None: |
| 51 |
print(users) |
| 52 |
|
| 53 |
app(["--help"]) |
| 54 |
|
| 55 |
|
| 56 |
def test_help_default_action(app, console): |
| 57 |
"""No command should default to help.""" |
| 58 |
with console.capture() as capture: |
| 59 |
app([], console=console) |
| 60 |
|
| 61 |
actual = capture.get() |
| 62 |
expected = dedent( |
| 63 |
"""\ |
| 64 |
Usage: app |
| 65 |
|
| 66 |
App Help String Line 1. |
| 67 |
|
| 68 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 69 |
│ --help (-h) Display this message and exit. │ |
| 70 |
│ --version Display application version. │ |
| 71 |
╰────────────────────────────────────────────────────────────────────╯ |
| 72 |
""" |
| 73 |
) |
| 74 |
assert actual == expected |
| 75 |
|
| 76 |
|
| 77 |
def test_help_custom_usage(app, console): |
| 78 |
app.usage = "My custom usage." |
| 79 |
with console.capture() as capture: |
| 80 |
app([], console=console) |
| 81 |
|
| 82 |
actual = capture.get() |
| 83 |
expected = dedent( |
| 84 |
"""\ |
| 85 |
Usage: My custom usage. |
| 86 |
|
| 87 |
App Help String Line 1. |
| 88 |
|
| 89 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 90 |
│ --help (-h) Display this message and exit. │ |
| 91 |
│ --version Display application version. │ |
| 92 |
╰────────────────────────────────────────────────────────────────────╯ |
| 93 |
""" |
| 94 |
) |
| 95 |
assert actual == expected |
| 96 |
|
| 97 |
|
| 98 |
def test_help_custom_usage_subapp(app, console): |
| 99 |
"""Intentionally do not print --help/--version flags in subapp help.""" |
| 100 |
app.command(App(name="foo", usage="My custom usage.")) |
| 101 |
|
| 102 |
with console.capture() as capture: |
| 103 |
app(["foo", "--help"], console=console) |
| 104 |
|
| 105 |
actual = capture.get() |
| 106 |
expected = dedent( |
| 107 |
"""\ |
| 108 |
Usage: My custom usage. |
| 109 |
|
| 110 |
""" |
| 111 |
) |
| 112 |
assert actual == expected |
| 113 |
|
| 114 |
|
| 115 |
def test_help_default_help_flags(console): |
| 116 |
"""Standard help flags.""" |
| 117 |
app = App(name="app", help="App Help String Line 1.", result_action="return_value") |
| 118 |
with console.capture() as capture: |
| 119 |
app(["--help"], console=console) |
| 120 |
|
| 121 |
actual = capture.get() |
| 122 |
expected = dedent( |
| 123 |
"""\ |
| 124 |
Usage: app |
| 125 |
|
| 126 |
App Help String Line 1. |
| 127 |
|
| 128 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 129 |
│ --help (-h) Display this message and exit. │ |
| 130 |
│ --version Display application version. │ |
| 131 |
╰────────────────────────────────────────────────────────────────────╯ |
| 132 |
""" |
| 133 |
) |
| 134 |
|
| 135 |
assert actual == expected |
| 136 |
|
| 137 |
|
| 138 |
def test_help_format_usage_empty(console): |
| 139 |
app = App( |
| 140 |
name="app", |
| 141 |
help="App Help String Line 1.", |
| 142 |
help_flags=[], |
| 143 |
version_flags=[], |
| 144 |
) |
| 145 |
|
| 146 |
with console.capture() as capture: |
| 147 |
console.print(format_usage(app, [])) |
| 148 |
actual = capture.get() |
| 149 |
assert actual == "app\n\n" |
| 150 |
|
| 151 |
|
| 152 |
def test_help_format_usage_command(app, console): |
| 153 |
@app.command |
| 154 |
def foo(): |
| 155 |
pass |
| 156 |
|
| 157 |
with console.capture() as capture: |
| 158 |
console.print(format_usage(app, [])) |
| 159 |
actual = capture.get() |
| 160 |
assert actual == "app COMMAND\n\n" |
| 161 |
|
| 162 |
|
| 163 |
def test_format_commands_docstring(app, console): |
| 164 |
@app.command |
| 165 |
def foo(): |
| 166 |
"""Docstring for foo. |
| 167 |
|
| 168 |
This should not be shown. |
| 169 |
""" |
| 170 |
|
| 171 |
panel = HelpPanel(title="Commands", format="command") |
| 172 |
panel.entries.extend(format_command_entries((RegisteredCommand(("foo",), app["foo"]),), format="restructuredtext")) |
| 173 |
formatter = DefaultFormatter() |
| 174 |
rendered = formatter._render_panel(panel, console, console.options) |
| 175 |
with console.capture() as capture: |
| 176 |
console.print(rendered) |
| 177 |
|
| 178 |
actual = capture.get() |
| 179 |
assert actual == ( |
| 180 |
"╭─ Commands ─────────────────────────────────────────────────────────╮\n" |
| 181 |
"│ foo Docstring for foo. │\n" |
| 182 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
def test_format_commands_docstring_multi_line_pep0257(app, console): |
| 187 |
""" |
| 188 |
PEP-0257 says that the short_description and long_description should be separated by an empty newline. |
| 189 |
We hijack the docstring parsing a little bit to have the following properties: |
| 190 |
* The first block of text is ALWAYS the short-description. |
| 191 |
* There cannot be a long-description without a short-description. |
| 192 |
|
| 193 |
See: |
| 194 |
* https://github.com/BrianPugh/cyclopts/issues/74 |
| 195 |
* https://github.com/BrianPugh/cyclopts/issues/393 |
| 196 |
* https://github.com/BrianPugh/cyclopts/issues/402 |
| 197 |
""" |
| 198 |
|
| 199 |
@app.command |
| 200 |
def foo(): |
| 201 |
""" |
| 202 |
This function doesn't have a |
| 203 |
long description. |
| 204 |
""" |
| 205 |
|
| 206 |
panel = HelpPanel(title="Commands", format="command") |
| 207 |
panel.entries.extend(format_command_entries((RegisteredCommand(("foo",), app["foo"]),), format="restructuredtext")) |
| 208 |
formatter = DefaultFormatter() |
| 209 |
rendered = formatter._render_panel(panel, console, console.options) |
| 210 |
with console.capture() as capture: |
| 211 |
console.print(rendered) |
| 212 |
|
| 213 |
actual = capture.get() |
| 214 |
assert actual == dedent( |
| 215 |
"""\ |
| 216 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 217 |
│ foo This function doesn't have a long description. │ |
| 218 |
╰────────────────────────────────────────────────────────────────────╯ |
| 219 |
""" |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
def test_format_commands_no_show(app, console, assert_parse_args): |
| 224 |
@app.command |
| 225 |
def foo(): |
| 226 |
"""Docstring for foo.""" |
| 227 |
pass |
| 228 |
|
| 229 |
@app.command(show=False) |
| 230 |
def bar(): |
| 231 |
"""Should not be shown.""" |
| 232 |
pass |
| 233 |
|
| 234 |
assert_parse_args(foo, "foo") |
| 235 |
assert_parse_args(bar, "bar") |
| 236 |
|
| 237 |
with console.capture() as capture: |
| 238 |
app.help_print([], console=console) |
| 239 |
|
| 240 |
actual = capture.get() |
| 241 |
expected = dedent( |
| 242 |
"""\ |
| 243 |
Usage: app COMMAND |
| 244 |
|
| 245 |
App Help String Line 1. |
| 246 |
|
| 247 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 248 |
│ foo Docstring for foo. │ |
| 249 |
│ --help (-h) Display this message and exit. │ |
| 250 |
│ --version Display application version. │ |
| 251 |
╰────────────────────────────────────────────────────────────────────╯ |
| 252 |
""" |
| 253 |
) |
| 254 |
assert actual == expected |
| 255 |
|
| 256 |
|
| 257 |
def test_format_commands_explicit_help(app, console): |
| 258 |
@app.command(help="Docstring for foo.") |
| 259 |
def foo(): |
| 260 |
"""Should not be shown.""" |
| 261 |
pass |
| 262 |
|
| 263 |
panel = HelpPanel(title="Commands", format="command") |
| 264 |
panel.entries.extend(format_command_entries((RegisteredCommand(("foo",), app["foo"]),), format="restructuredtext")) |
| 265 |
formatter = DefaultFormatter() |
| 266 |
rendered = formatter._render_panel(panel, console, console.options) |
| 267 |
with console.capture() as capture: |
| 268 |
console.print(rendered) |
| 269 |
|
| 270 |
actual = capture.get() |
| 271 |
assert actual == ( |
| 272 |
"╭─ Commands ─────────────────────────────────────────────────────────╮\n" |
| 273 |
"│ foo Docstring for foo. │\n" |
| 274 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 275 |
) |
| 276 |
|
| 277 |
|
| 278 |
def test_format_commands_explicit_name(app, console): |
| 279 |
@app.command(name="bar") |
| 280 |
def foo(): |
| 281 |
"""Docstring for bar. |
| 282 |
|
| 283 |
This should not be shown. |
| 284 |
""" |
| 285 |
pass |
| 286 |
|
| 287 |
panel = HelpPanel(title="Commands", format="command") |
| 288 |
panel.entries.extend(format_command_entries((RegisteredCommand(("bar",), app["bar"]),), format="restructuredtext")) |
| 289 |
formatter = DefaultFormatter() |
| 290 |
rendered = formatter._render_panel(panel, console, console.options) |
| 291 |
with console.capture() as capture: |
| 292 |
console.print(rendered) |
| 293 |
|
| 294 |
actual = capture.get() |
| 295 |
assert actual == ( |
| 296 |
"╭─ Commands ─────────────────────────────────────────────────────────╮\n" |
| 297 |
"│ bar Docstring for bar. │\n" |
| 298 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 299 |
) |
| 300 |
|
| 301 |
|
| 302 |
def test_help_functools_partial_1(app, console): |
| 303 |
def foo(a: int, b: int, c: int): |
| 304 |
"""Docstring for foo.""" |
| 305 |
|
| 306 |
partial_foo = partial(foo, c=3) |
| 307 |
app.command(partial_foo) |
| 308 |
|
| 309 |
with console.capture() as capture: |
| 310 |
app.help_print(console=console) |
| 311 |
actual = capture.get() |
| 312 |
expected = dedent( |
| 313 |
"""\ |
| 314 |
Usage: app COMMAND |
| 315 |
|
| 316 |
App Help String Line 1. |
| 317 |
|
| 318 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 319 |
│ foo Docstring for foo. │ |
| 320 |
│ --help (-h) Display this message and exit. │ |
| 321 |
│ --version Display application version. │ |
| 322 |
╰────────────────────────────────────────────────────────────────────╯ |
| 323 |
""" |
| 324 |
) |
| 325 |
assert actual == expected |
| 326 |
|
| 327 |
|
| 328 |
def test_help_functools_partial_2(app, console): |
| 329 |
"""Test help docstring resolution for :class:`functools.partial` functions. |
| 330 |
|
| 331 |
The argument ``c`` with ``parse=False`` should not be shown in the help page. |
| 332 |
|
| 333 |
The argument ``b`` should be shown with its :class:`functools.partial` default value. |
| 334 |
""" |
| 335 |
|
| 336 |
def foo(a: int, b: int, c: Annotated[int, Parameter(parse=False)]): |
| 337 |
"""Docstring for foo. |
| 338 |
|
| 339 |
Parameters |
| 340 |
---------- |
| 341 |
a: int |
| 342 |
Docstring for a. |
| 343 |
b: int |
| 344 |
Docstring for b. |
| 345 |
c: int |
| 346 |
Docstring for c. |
| 347 |
""" |
| 348 |
|
| 349 |
partial_foo = partial(foo, b=2, c=3) |
| 350 |
app.command(partial_foo) |
| 351 |
|
| 352 |
with console.capture() as capture: |
| 353 |
app.help_print("foo", console=console) |
| 354 |
|
| 355 |
actual = capture.get() |
| 356 |
expected = dedent( |
| 357 |
"""\ |
| 358 |
Usage: app foo [OPTIONS] A |
| 359 |
|
| 360 |
Docstring for foo. |
| 361 |
|
| 362 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 363 |
│ * A --a Docstring for a. [required] │ |
| 364 |
│ --b Docstring for b. [default: 2] │ |
| 365 |
╰────────────────────────────────────────────────────────────────────╯ |
| 366 |
""" |
| 367 |
) |
| 368 |
assert actual == expected |
| 369 |
|
| 370 |
|
| 371 |
def test_help_empty(console): |
| 372 |
app = App(name="foo", version_flags=[], help_flags=[]) |
| 373 |
|
| 374 |
with console.capture() as capture: |
| 375 |
app.help_print(console=console) |
| 376 |
actual = capture.get() |
| 377 |
|
| 378 |
assert actual == "Usage: foo\n\n" |
| 379 |
|
| 380 |
|
| 381 |
def test_format_choices_rich_format(app, console, assert_parse_args): |
| 382 |
app.help_format = "rich" |
| 383 |
|
| 384 |
@app.default |
| 385 |
def foo(region: Literal["us", "ca"]): |
| 386 |
"""Docstring for foo.""" |
| 387 |
pass |
| 388 |
|
| 389 |
with console.capture() as capture: |
| 390 |
app.help_print([], console=console) |
| 391 |
|
| 392 |
actual = capture.get() |
| 393 |
expected = dedent( |
| 394 |
"""\ |
| 395 |
Usage: app REGION |
| 396 |
|
| 397 |
App Help String Line 1. |
| 398 |
|
| 399 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 400 |
│ --help (-h) Display this message and exit. │ |
| 401 |
│ --version Display application version. │ |
| 402 |
╰────────────────────────────────────────────────────────────────────╯ |
| 403 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 404 |
│ * REGION --region [choices: us, ca] [required] │ |
| 405 |
╰────────────────────────────────────────────────────────────────────╯ |
| 406 |
""" |
| 407 |
) |
| 408 |
assert actual == expected |
| 409 |
|
| 410 |
|
| 411 |
@pytest.fixture |
| 412 |
def capture_format_group_parameters(console, default_function_groups): |
| 413 |
def inner(cmd): |
| 414 |
argument_collection = ArgumentCollection._from_callable( |
| 415 |
cmd, |
| 416 |
None, |
| 417 |
parse_docstring=True, |
| 418 |
) |
| 419 |
|
| 420 |
with console.capture() as capture: |
| 421 |
group = argument_collection.groups[0] |
| 422 |
group_argument_collection = argument_collection.filter_by(group=group) |
| 423 |
panel = create_parameter_help_panel(group, group_argument_collection, "restructuredtext") |
| 424 |
formatter = DefaultFormatter() |
| 425 |
rendered = formatter._render_panel(panel, console, console.options) |
| 426 |
console.print(rendered) |
| 427 |
|
| 428 |
return capture.get() |
| 429 |
|
| 430 |
return inner |
| 431 |
|
| 432 |
|
| 433 |
def test_help_format_group_parameters_empty(capture_format_group_parameters): |
| 434 |
def cmd( |
| 435 |
foo: Annotated[str, Parameter(show=False)], |
| 436 |
): |
| 437 |
pass |
| 438 |
|
| 439 |
actual = capture_format_group_parameters(cmd) |
| 440 |
expected = "" |
| 441 |
assert actual == expected |
| 442 |
|
| 443 |
|
| 444 |
def test_help_format_group_parameters(capture_format_group_parameters): |
| 445 |
def cmd( |
| 446 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 447 |
bar: Annotated[str, Parameter(help="Docstring for bar.")], |
| 448 |
): |
| 449 |
pass |
| 450 |
|
| 451 |
actual = capture_format_group_parameters(cmd) |
| 452 |
expected = dedent( |
| 453 |
"""\ |
| 454 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 455 |
│ * FOO --foo Docstring for foo. [required] │ |
| 456 |
│ * BAR --bar Docstring for bar. [required] │ |
| 457 |
╰────────────────────────────────────────────────────────────────────╯ |
| 458 |
""" |
| 459 |
) |
| 460 |
assert actual == expected |
| 461 |
|
| 462 |
|
| 463 |
def test_help_format_group_parameters_short_name(capture_format_group_parameters): |
| 464 |
def cmd( |
| 465 |
foo: Annotated[str, Parameter(name=["--foo", "-f"], help="Docstring for foo.")], |
| 466 |
): |
| 467 |
pass |
| 468 |
|
| 469 |
actual = capture_format_group_parameters(cmd) |
| 470 |
expected = dedent( |
| 471 |
"""\ |
| 472 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 473 |
│ * FOO --foo -f Docstring for foo. [required] │ |
| 474 |
╰────────────────────────────────────────────────────────────────────╯ |
| 475 |
""" |
| 476 |
) |
| 477 |
assert actual == expected |
| 478 |
|
| 479 |
|
| 480 |
def test_help_format_group_parameters_short_name_first(capture_format_group_parameters): |
| 481 |
"""Label should derive from long-form name even when short flag is listed first.""" |
| 482 |
|
| 483 |
def cmd( |
| 484 |
foo: Annotated[str, Parameter(name=["-f", "--foo"], help="Docstring for foo.")], |
| 485 |
): |
| 486 |
pass |
| 487 |
|
| 488 |
actual = capture_format_group_parameters(cmd) |
| 489 |
expected = dedent( |
| 490 |
"""\ |
| 491 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 492 |
│ * FOO --foo -f Docstring for foo. [required] │ |
| 493 |
╰────────────────────────────────────────────────────────────────────╯ |
| 494 |
""" |
| 495 |
) |
| 496 |
assert actual == expected |
| 497 |
|
| 498 |
|
| 499 |
def test_help_format_group_parameters_from_docstring(capture_format_group_parameters): |
| 500 |
def cmd(foo: str, bar: str): |
| 501 |
""" |
| 502 |
|
| 503 |
Parameters |
| 504 |
---------- |
| 505 |
foo: str |
| 506 |
Docstring for foo. |
| 507 |
bar: str |
| 508 |
Docstring for bar. |
| 509 |
""" |
| 510 |
pass |
| 511 |
|
| 512 |
actual = capture_format_group_parameters(cmd) |
| 513 |
expected = dedent( |
| 514 |
"""\ |
| 515 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 516 |
│ * FOO --foo Docstring for foo. [required] │ |
| 517 |
│ * BAR --bar Docstring for bar. [required] │ |
| 518 |
╰────────────────────────────────────────────────────────────────────╯ |
| 519 |
""" |
| 520 |
) |
| 521 |
assert actual == expected |
| 522 |
|
| 523 |
|
| 524 |
def test_help_format_group_parameters_sphinx_directives(capture_format_group_parameters): |
| 525 |
def cmd(foo: str, bar: float = 1.0, baz: int = 5): |
| 526 |
""" |
| 527 |
Command with Sphinx directives. |
| 528 |
|
| 529 |
Parameters |
| 530 |
---------- |
| 531 |
foo: str |
| 532 |
Foo parameter |
| 533 |
bar: float |
| 534 |
Bar parameter |
| 535 |
|
| 536 |
.. versionadded:: 0.47 |
| 537 |
baz: int |
| 538 |
Baz parameter |
| 539 |
|
| 540 |
.. deprecated:: 2.0 |
| 541 |
Use something else instead |
| 542 |
""" |
| 543 |
|
| 544 |
actual = capture_format_group_parameters(cmd) |
| 545 |
expected = dedent( |
| 546 |
"""\ |
| 547 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 548 |
│ * FOO --foo Foo parameter [required] │ |
| 549 |
│ BAR --bar Bar parameter [Added in v0.47] [default: 1.0] │ |
| 550 |
│ BAZ --baz Baz parameter [⚠ Deprecated in v2.0] Use something │ |
| 551 |
│ else instead [default: 5] │ |
| 552 |
╰────────────────────────────────────────────────────────────────────╯ |
| 553 |
""" |
| 554 |
) |
| 555 |
assert actual == expected |
| 556 |
|
| 557 |
|
| 558 |
def test_help_format_group_parameters_sphinx_versionchanged(capture_format_group_parameters): |
| 559 |
def cmd(foo: str): |
| 560 |
""" |
| 561 |
Command with versionchanged directive. |
| 562 |
|
| 563 |
Parameters |
| 564 |
---------- |
| 565 |
foo: str |
| 566 |
Foo parameter |
| 567 |
|
| 568 |
.. versionchanged:: 1.5 |
| 569 |
""" |
| 570 |
|
| 571 |
actual = capture_format_group_parameters(cmd) |
| 572 |
expected = dedent( |
| 573 |
"""\ |
| 574 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 575 |
│ * FOO --foo Foo parameter [Changed in v1.5] [required] │ |
| 576 |
╰────────────────────────────────────────────────────────────────────╯ |
| 577 |
""" |
| 578 |
) |
| 579 |
assert actual == expected |
| 580 |
|
| 581 |
|
| 582 |
def test_help_format_group_parameters_sphinx_note_warning_seealso(capture_format_group_parameters): |
| 583 |
def cmd(foo: str, bar: str, baz: str): |
| 584 |
""" |
| 585 |
Command with note, warning, and seealso directives. |
| 586 |
|
| 587 |
Parameters |
| 588 |
---------- |
| 589 |
foo: str |
| 590 |
Foo parameter |
| 591 |
|
| 592 |
.. note:: This is important |
| 593 |
bar: str |
| 594 |
Bar parameter |
| 595 |
|
| 596 |
.. warning:: Be careful here |
| 597 |
baz: str |
| 598 |
Baz parameter |
| 599 |
|
| 600 |
.. seealso:: Related function |
| 601 |
""" |
| 602 |
|
| 603 |
actual = capture_format_group_parameters(cmd) |
| 604 |
expected = dedent( |
| 605 |
"""\ |
| 606 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 607 |
│ * FOO --foo Foo parameter │ |
| 608 |
│ │ |
| 609 |
│ Note: This is important [required] │ |
| 610 |
│ * BAR --bar Bar parameter │ |
| 611 |
│ │ |
| 612 |
│ ⚠ Warning: Be careful here [required] │ |
| 613 |
│ * BAZ --baz Baz parameter │ |
| 614 |
│ │ |
| 615 |
│ See also: Related function [required] │ |
| 616 |
╰────────────────────────────────────────────────────────────────────╯ |
| 617 |
""" |
| 618 |
) |
| 619 |
assert actual == expected |
| 620 |
|
| 621 |
|
| 622 |
def test_help_format_group_parameters_sphinx_multiple_directives(capture_format_group_parameters): |
| 623 |
def cmd(foo: str): |
| 624 |
""" |
| 625 |
Command with multiple directives on one parameter. |
| 626 |
|
| 627 |
Parameters |
| 628 |
---------- |
| 629 |
foo: str |
| 630 |
Foo parameter |
| 631 |
|
| 632 |
.. versionadded:: 1.0 |
| 633 |
|
| 634 |
.. versionchanged:: 2.0 |
| 635 |
|
| 636 |
.. note:: Important note |
| 637 |
""" |
| 638 |
|
| 639 |
actual = capture_format_group_parameters(cmd) |
| 640 |
expected = dedent( |
| 641 |
"""\ |
| 642 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 643 |
│ * FOO --foo Foo parameter [Added in v1.0] [Changed in v2.0] │ |
| 644 |
│ │ |
| 645 |
│ Note: Important note [required] │ |
| 646 |
╰────────────────────────────────────────────────────────────────────╯ |
| 647 |
""" |
| 648 |
) |
| 649 |
assert actual == expected |
| 650 |
|
| 651 |
|
| 652 |
def test_help_format_group_parameters_sphinx_deprecated_no_content(capture_format_group_parameters): |
| 653 |
def cmd(foo: str): |
| 654 |
""" |
| 655 |
Command with deprecated directive without content. |
| 656 |
|
| 657 |
Parameters |
| 658 |
---------- |
| 659 |
foo: str |
| 660 |
Foo parameter |
| 661 |
|
| 662 |
.. deprecated:: 3.0 |
| 663 |
""" |
| 664 |
|
| 665 |
actual = capture_format_group_parameters(cmd) |
| 666 |
expected = dedent( |
| 667 |
"""\ |
| 668 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 669 |
│ * FOO --foo Foo parameter [⚠ Deprecated in v3.0] [required] │ |
| 670 |
╰────────────────────────────────────────────────────────────────────╯ |
| 671 |
""" |
| 672 |
) |
| 673 |
assert actual == expected |
| 674 |
|
| 675 |
|
| 676 |
def test_help_format_group_parameters_bool_flag(capture_format_group_parameters): |
| 677 |
def cmd( |
| 678 |
foo: Annotated[bool, Parameter(help="Docstring for foo.")] = True, |
| 679 |
): |
| 680 |
pass |
| 681 |
|
| 682 |
actual = capture_format_group_parameters(cmd) |
| 683 |
expected = dedent( |
| 684 |
"""\ |
| 685 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 686 |
│ FOO --foo --no-foo Docstring for foo. [default: True] │ |
| 687 |
╰────────────────────────────────────────────────────────────────────╯ |
| 688 |
""" |
| 689 |
) |
| 690 |
assert actual == expected |
| 691 |
|
| 692 |
|
| 693 |
@pytest.mark.parametrize("negative_str", ["--yesnt-foo", "yesnt-foo"]) |
| 694 |
def test_help_format_group_parameters_bool_flag_custom_negative(capture_format_group_parameters, negative_str): |
| 695 |
def cmd( |
| 696 |
foo: Annotated[bool, Parameter(negative=negative_str, help="Docstring for foo.")] = True, |
| 697 |
): |
| 698 |
pass |
| 699 |
|
| 700 |
actual = capture_format_group_parameters(cmd) |
| 701 |
expected = dedent( |
| 702 |
"""\ |
| 703 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 704 |
│ FOO --foo --yesnt-foo Docstring for foo. [default: True] │ |
| 705 |
╰────────────────────────────────────────────────────────────────────╯ |
| 706 |
""" |
| 707 |
) |
| 708 |
assert actual == expected |
| 709 |
|
| 710 |
|
| 711 |
@pytest.mark.parametrize("negative_str", ["--negative", "negative"]) |
| 712 |
def test_help_format_group_parameters_bool_or_none_custom_negative(capture_format_group_parameters, negative_str): |
| 713 |
"""Test that custom negative doesn't appear twice for bool | None.""" |
| 714 |
|
| 715 |
def cmd( |
| 716 |
param: Annotated[bool | None, Parameter(negative=negative_str, help="Docstring for param.")] = None, |
| 717 |
): |
| 718 |
pass |
| 719 |
|
| 720 |
actual = capture_format_group_parameters(cmd) |
| 721 |
expected = dedent( |
| 722 |
"""\ |
| 723 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 724 |
│ PARAM --param --negative Docstring for param. │ |
| 725 |
╰────────────────────────────────────────────────────────────────────╯ |
| 726 |
""" |
| 727 |
) |
| 728 |
assert actual == expected |
| 729 |
|
| 730 |
|
| 731 |
def test_help_format_group_parameters_list_flag(capture_format_group_parameters): |
| 732 |
def cmd( |
| 733 |
foo: Annotated[list[int] | None, Parameter(help="Docstring for foo.")] = None, |
| 734 |
): |
| 735 |
pass |
| 736 |
|
| 737 |
actual = capture_format_group_parameters(cmd) |
| 738 |
expected = dedent( |
| 739 |
"""\ |
| 740 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 741 |
│ FOO --foo --empty-foo Docstring for foo. │ |
| 742 |
╰────────────────────────────────────────────────────────────────────╯ |
| 743 |
""" |
| 744 |
) |
| 745 |
assert actual == expected |
| 746 |
|
| 747 |
|
| 748 |
def test_help_format_group_parameters_defaults(capture_format_group_parameters): |
| 749 |
def cmd( |
| 750 |
foo: Annotated[str, Parameter(help="Docstring for foo.")] = "fizz", |
| 751 |
bar: Annotated[str, Parameter(help="Docstring for bar.")] = "buzz", |
| 752 |
): |
| 753 |
pass |
| 754 |
|
| 755 |
actual = capture_format_group_parameters(cmd) |
| 756 |
expected = dedent( |
| 757 |
"""\ |
| 758 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 759 |
│ FOO --foo Docstring for foo. [default: fizz] │ |
| 760 |
│ BAR --bar Docstring for bar. [default: buzz] │ |
| 761 |
╰────────────────────────────────────────────────────────────────────╯ |
| 762 |
""" |
| 763 |
) |
| 764 |
assert actual == expected |
| 765 |
|
| 766 |
|
| 767 |
def test_help_format_group_parameters_defaults_no_show(capture_format_group_parameters): |
| 768 |
def cmd( |
| 769 |
foo: Annotated[str, Parameter(show_default=False, help="Docstring for foo.")] = "fizz", |
| 770 |
bar: Annotated[str, Parameter(help="Docstring for bar.")] = "buzz", |
| 771 |
): |
| 772 |
pass |
| 773 |
|
| 774 |
actual = capture_format_group_parameters(cmd) |
| 775 |
expected = dedent( |
| 776 |
"""\ |
| 777 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 778 |
│ FOO --foo Docstring for foo. │ |
| 779 |
│ BAR --bar Docstring for bar. [default: buzz] │ |
| 780 |
╰────────────────────────────────────────────────────────────────────╯ |
| 781 |
""" |
| 782 |
) |
| 783 |
assert actual == expected |
| 784 |
|
| 785 |
|
| 786 |
def test_help_format_group_parameters_show_default_string(capture_format_group_parameters): |
| 787 |
def cmd( |
| 788 |
foo: Annotated[str, Parameter(show_default="automatic", help="Docstring for foo.")] = "fizz", |
| 789 |
bar: Annotated[str | None, Parameter(show_default="automatic", help="Docstring for bar.")] = None, |
| 790 |
): |
| 791 |
pass |
| 792 |
|
| 793 |
actual = capture_format_group_parameters(cmd) |
| 794 |
expected = dedent( |
| 795 |
"""\ |
| 796 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 797 |
│ FOO --foo Docstring for foo. [default: automatic] │ |
| 798 |
│ BAR --bar Docstring for bar. [default: automatic] │ |
| 799 |
╰────────────────────────────────────────────────────────────────────╯ |
| 800 |
""" |
| 801 |
) |
| 802 |
assert actual == expected |
| 803 |
|
| 804 |
|
| 805 |
def test_help_format_dataclass_default_parameter_negative_propagation(app, console): |
| 806 |
app.default_parameter = Parameter(negative=()) |
| 807 |
|
| 808 |
@Parameter(name="*") |
| 809 |
@dataclass |
| 810 |
class Common: |
| 811 |
force: bool |
| 812 |
|
| 813 |
@app.default |
| 814 |
def default(common: Common): |
| 815 |
pass |
| 816 |
|
| 817 |
with console.capture() as capture: |
| 818 |
app.help_print([], console=console) |
| 819 |
|
| 820 |
actual = capture.get() |
| 821 |
expected = dedent( |
| 822 |
"""\ |
| 823 |
Usage: app FORCE |
| 824 |
|
| 825 |
App Help String Line 1. |
| 826 |
|
| 827 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 828 |
│ --help (-h) Display this message and exit. │ |
| 829 |
│ --version Display application version. │ |
| 830 |
╰────────────────────────────────────────────────────────────────────╯ |
| 831 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 832 |
│ * FORCE --force [required] │ |
| 833 |
╰────────────────────────────────────────────────────────────────────╯ |
| 834 |
""" |
| 835 |
) |
| 836 |
assert actual == expected |
| 837 |
|
| 838 |
|
| 839 |
def test_help_format_dataclass_decorated_parameter_negative_propagation(app, console): |
| 840 |
@Parameter(name="*", negative=()) |
| 841 |
@dataclass |
| 842 |
class Common: |
| 843 |
force: bool |
| 844 |
|
| 845 |
@app.default |
| 846 |
def default(common: Common): |
| 847 |
pass |
| 848 |
|
| 849 |
with console.capture() as capture: |
| 850 |
app.help_print([], console=console) |
| 851 |
|
| 852 |
actual = capture.get() |
| 853 |
expected = dedent( |
| 854 |
"""\ |
| 855 |
Usage: app FORCE |
| 856 |
|
| 857 |
App Help String Line 1. |
| 858 |
|
| 859 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 860 |
│ --help (-h) Display this message and exit. │ |
| 861 |
│ --version Display application version. │ |
| 862 |
╰────────────────────────────────────────────────────────────────────╯ |
| 863 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 864 |
│ * FORCE --force [required] │ |
| 865 |
╰────────────────────────────────────────────────────────────────────╯ |
| 866 |
""" |
| 867 |
) |
| 868 |
assert actual == expected |
| 869 |
|
| 870 |
|
| 871 |
def test_help_format_group_parameters_choices_literal_no_show(capture_format_group_parameters): |
| 872 |
def cmd( |
| 873 |
foo: Annotated[Literal["fizz", "buzz"], Parameter(show_choices=False, help="Docstring for foo.")] = "fizz", |
| 874 |
bar: Annotated[Literal["fizz", "buzz"], Parameter(help="Docstring for bar.")] = "buzz", |
| 875 |
): |
| 876 |
pass |
| 877 |
|
| 878 |
actual = capture_format_group_parameters(cmd) |
| 879 |
expected = dedent( |
| 880 |
"""\ |
| 881 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 882 |
│ FOO --foo Docstring for foo. [default: fizz] │ |
| 883 |
│ BAR --bar Docstring for bar. [choices: fizz, buzz] [default: │ |
| 884 |
│ buzz] │ |
| 885 |
╰────────────────────────────────────────────────────────────────────╯ |
| 886 |
""" |
| 887 |
) |
| 888 |
assert actual == expected |
| 889 |
|
| 890 |
|
| 891 |
def test_help_format_group_parameters_choices_literal_union(capture_format_group_parameters): |
| 892 |
def cmd( |
| 893 |
foo: Annotated[int | Literal["fizz", "buzz"] | Literal["bar"], Parameter(help="Docstring for foo.")] = "fizz", |
| 894 |
): |
| 895 |
pass |
| 896 |
|
| 897 |
actual = capture_format_group_parameters(cmd) |
| 898 |
expected = dedent( |
| 899 |
"""\ |
| 900 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 901 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz, bar] [default: │ |
| 902 |
│ fizz] │ |
| 903 |
╰────────────────────────────────────────────────────────────────────╯ |
| 904 |
""" |
| 905 |
) |
| 906 |
assert actual == expected |
| 907 |
|
| 908 |
|
| 909 |
def test_help_format_group_parameters_choices_literal_union_python310_syntax_0(capture_format_group_parameters): |
| 910 |
def cmd( |
| 911 |
foo: Annotated[ |
| 912 |
Literal["fizz", "buzz"] | Literal["bar"], Parameter(help="Docstring for foo.") |
| 913 |
] = "fizz", |
| 914 |
): |
| 915 |
pass |
| 916 |
|
| 917 |
actual = capture_format_group_parameters(cmd) |
| 918 |
expected = dedent( |
| 919 |
"""\ |
| 920 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 921 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz, bar] [default: │ |
| 922 |
│ fizz] │ |
| 923 |
╰────────────────────────────────────────────────────────────────────╯ |
| 924 |
""" |
| 925 |
) |
| 926 |
assert actual == expected |
| 927 |
|
| 928 |
|
| 929 |
def test_help_format_group_parameters_choices_literal_union_python310_syntax_1(capture_format_group_parameters): |
| 930 |
def cmd(foo: Literal["fizz", "buzz"] | Literal["bar"] = "fizz"): |
| 931 |
pass |
| 932 |
|
| 933 |
actual = capture_format_group_parameters(cmd) |
| 934 |
expected = dedent( |
| 935 |
"""\ |
| 936 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 937 |
│ FOO --foo [choices: fizz, buzz, bar] [default: fizz] │ |
| 938 |
╰────────────────────────────────────────────────────────────────────╯ |
| 939 |
""" |
| 940 |
) |
| 941 |
assert actual == expected |
| 942 |
|
| 943 |
|
| 944 |
def test_help_format_group_parameters_choices_enum(capture_format_group_parameters): |
| 945 |
class CompSciProblem(Enum): |
| 946 |
fizz = "bleep bloop blop" |
| 947 |
buzz = "blop bleep bloop" |
| 948 |
|
| 949 |
def cmd( |
| 950 |
foo: Annotated[CompSciProblem, Parameter(help="Docstring for foo.")] = CompSciProblem.fizz, |
| 951 |
bar: Annotated[CompSciProblem, Parameter(help="Docstring for bar.")] = CompSciProblem.buzz, |
| 952 |
): |
| 953 |
pass |
| 954 |
|
| 955 |
actual = capture_format_group_parameters(cmd) |
| 956 |
expected = dedent( |
| 957 |
"""\ |
| 958 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 959 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz] [default: │ |
| 960 |
│ fizz] │ |
| 961 |
│ BAR --bar Docstring for bar. [choices: fizz, buzz] [default: │ |
| 962 |
│ buzz] │ |
| 963 |
╰────────────────────────────────────────────────────────────────────╯ |
| 964 |
""" |
| 965 |
) |
| 966 |
assert actual == expected |
| 967 |
|
| 968 |
|
| 969 |
def test_help_format_group_parameters_choices_enum_list(capture_format_group_parameters): |
| 970 |
class CompSciProblem(Enum): |
| 971 |
fizz = "bleep bloop blop" |
| 972 |
buzz = "blop bleep bloop" |
| 973 |
|
| 974 |
fizz_alias = fizz |
| 975 |
|
| 976 |
def cmd( |
| 977 |
foo: Annotated[ |
| 978 |
list[CompSciProblem] | None, |
| 979 |
Parameter(help="Docstring for foo.", negative_iterable=(), show_default=False, show_choices=True), |
| 980 |
] = None, |
| 981 |
): |
| 982 |
pass |
| 983 |
|
| 984 |
actual = capture_format_group_parameters(cmd) |
| 985 |
expected = dedent( |
| 986 |
"""\ |
| 987 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 988 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz, fizz-alias] │ |
| 989 |
╰────────────────────────────────────────────────────────────────────╯ |
| 990 |
""" |
| 991 |
) |
| 992 |
assert actual == expected |
| 993 |
|
| 994 |
|
| 995 |
def test_help_format_group_parameters_choices_enum_list_typing(capture_format_group_parameters): |
| 996 |
class CompSciProblem(Enum): |
| 997 |
fizz = "bleep bloop blop" |
| 998 |
buzz = "blop bleep bloop" |
| 999 |
|
| 1000 |
def cmd( |
| 1001 |
foo: Annotated[ |
| 1002 |
list[CompSciProblem] | None, |
| 1003 |
Parameter(help="Docstring for foo.", negative_iterable=(), show_default=False, show_choices=True), |
| 1004 |
] = None, |
| 1005 |
): |
| 1006 |
pass |
| 1007 |
|
| 1008 |
actual = capture_format_group_parameters(cmd) |
| 1009 |
expected = dedent( |
| 1010 |
"""\ |
| 1011 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1012 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz] │ |
| 1013 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1014 |
""" |
| 1015 |
) |
| 1016 |
assert actual == expected |
| 1017 |
|
| 1018 |
|
| 1019 |
def test_help_format_group_parameters_choices_enum_sequence(capture_format_group_parameters): |
| 1020 |
class CompSciProblem(Enum): |
| 1021 |
fizz = "bleep bloop blop" |
| 1022 |
buzz = "blop bleep bloop" |
| 1023 |
|
| 1024 |
def cmd( |
| 1025 |
foo: Annotated[ |
| 1026 |
Sequence[CompSciProblem] | None, |
| 1027 |
Parameter(help="Docstring for foo.", negative_iterable=(), show_default=False, show_choices=True), |
| 1028 |
] = None, |
| 1029 |
): |
| 1030 |
pass |
| 1031 |
|
| 1032 |
actual = capture_format_group_parameters(cmd) |
| 1033 |
expected = dedent( |
| 1034 |
"""\ |
| 1035 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1036 |
│ FOO --foo Docstring for foo. [choices: fizz, buzz] │ |
| 1037 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1038 |
""" |
| 1039 |
) |
| 1040 |
assert actual == expected |
| 1041 |
|
| 1042 |
|
| 1043 |
def test_help_format_group_parameters_choices_literal_sequence(capture_format_group_parameters): |
| 1044 |
def cmd( |
| 1045 |
steps_to_skip: Annotated[ |
| 1046 |
Sequence[Literal["build", "deploy"]] | None, |
| 1047 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_default=False, show_choices=True), |
| 1048 |
] = None, |
| 1049 |
): |
| 1050 |
pass |
| 1051 |
|
| 1052 |
actual = capture_format_group_parameters(cmd) |
| 1053 |
expected = dedent( |
| 1054 |
"""\ |
| 1055 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1056 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1057 |
│ --steps-to-skip deploy] │ |
| 1058 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1059 |
""" |
| 1060 |
) |
| 1061 |
print(expected) |
| 1062 |
assert actual == expected |
| 1063 |
|
| 1064 |
|
| 1065 |
def test_help_format_group_parameters_choices_literal_set(capture_format_group_parameters): |
| 1066 |
def cmd( |
| 1067 |
steps_to_skip: Annotated[ |
| 1068 |
set[Literal["build", "deploy"]] | None, |
| 1069 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_default=False, show_choices=True), |
| 1070 |
] = None, |
| 1071 |
): |
| 1072 |
pass |
| 1073 |
|
| 1074 |
actual = capture_format_group_parameters(cmd) |
| 1075 |
expected = dedent( |
| 1076 |
"""\ |
| 1077 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1078 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1079 |
│ --steps-to-skip deploy] │ |
| 1080 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1081 |
""" |
| 1082 |
) |
| 1083 |
print(expected) |
| 1084 |
assert actual == expected |
| 1085 |
|
| 1086 |
|
| 1087 |
def test_help_parameter_string_annotation(capture_format_group_parameters): |
| 1088 |
def cmd(number: "Annotated[int,Parameter(name=['--number','-n'])]"): |
| 1089 |
"""Print number. |
| 1090 |
|
| 1091 |
Args: |
| 1092 |
number (int): A number to print. |
| 1093 |
""" |
| 1094 |
pass |
| 1095 |
|
| 1096 |
actual = capture_format_group_parameters(cmd) |
| 1097 |
expected = dedent( |
| 1098 |
"""\ |
| 1099 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1100 |
│ * NUMBER --number -n A number to print. [required] │ |
| 1101 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1102 |
""" |
| 1103 |
) |
| 1104 |
print(actual) |
| 1105 |
print(expected) |
| 1106 |
assert actual == expected |
| 1107 |
|
| 1108 |
|
| 1109 |
def test_help_format_group_parameters_choices_literal_set_typing(capture_format_group_parameters): |
| 1110 |
def cmd( |
| 1111 |
steps_to_skip: Annotated[ |
| 1112 |
set[Literal["build", "deploy"]] | None, |
| 1113 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_default=False, show_choices=True), |
| 1114 |
] = None, |
| 1115 |
): |
| 1116 |
pass |
| 1117 |
|
| 1118 |
actual = capture_format_group_parameters(cmd) |
| 1119 |
expected = dedent( |
| 1120 |
"""\ |
| 1121 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1122 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1123 |
│ --steps-to-skip deploy] │ |
| 1124 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1125 |
""" |
| 1126 |
) |
| 1127 |
assert actual == expected |
| 1128 |
|
| 1129 |
|
| 1130 |
def test_help_format_group_parameters_choices_literal_tuple(capture_format_group_parameters): |
| 1131 |
def cmd( |
| 1132 |
steps_to_skip: Annotated[ |
| 1133 |
tuple[Literal["build", "deploy"]] | None, |
| 1134 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_default=False, show_choices=True), |
| 1135 |
] = None, |
| 1136 |
): |
| 1137 |
pass |
| 1138 |
|
| 1139 |
actual = capture_format_group_parameters(cmd) |
| 1140 |
expected = dedent( |
| 1141 |
"""\ |
| 1142 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1143 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1144 |
│ --steps-to-skip deploy] │ |
| 1145 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1146 |
""" |
| 1147 |
) |
| 1148 |
print(actual) |
| 1149 |
assert actual == expected |
| 1150 |
|
| 1151 |
|
| 1152 |
def test_help_format_group_parameters_choices_literal_tuple_typing(capture_format_group_parameters): |
| 1153 |
def cmd( |
| 1154 |
steps_to_skip: Annotated[ |
| 1155 |
tuple[Literal["build", "deploy"]], |
| 1156 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_choices=True), |
| 1157 |
] = ("build",), |
| 1158 |
): |
| 1159 |
pass |
| 1160 |
|
| 1161 |
actual = capture_format_group_parameters(cmd) |
| 1162 |
expected = dedent( |
| 1163 |
"""\ |
| 1164 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1165 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1166 |
│ --steps-to-skip deploy] [default: ('build',)] │ |
| 1167 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1168 |
""" |
| 1169 |
) |
| 1170 |
assert actual == expected |
| 1171 |
|
| 1172 |
|
| 1173 |
def test_help_format_group_parameters_choices_literal_tuple_variadic_typing(capture_format_group_parameters): |
| 1174 |
def cmd( |
| 1175 |
steps_to_skip: Annotated[ |
| 1176 |
tuple[Literal["build", "deploy"], ...], |
| 1177 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_choices=True), |
| 1178 |
] = (), |
| 1179 |
): |
| 1180 |
pass |
| 1181 |
|
| 1182 |
actual = capture_format_group_parameters(cmd) |
| 1183 |
expected = dedent( |
| 1184 |
"""\ |
| 1185 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1186 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1187 |
│ --steps-to-skip deploy] [default: ()] │ |
| 1188 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1189 |
""" |
| 1190 |
) |
| 1191 |
assert actual == expected |
| 1192 |
|
| 1193 |
|
| 1194 |
def test_help_format_group_parameters_choices_literal_tuple_variadic(capture_format_group_parameters): |
| 1195 |
def cmd( |
| 1196 |
steps_to_skip: Annotated[ |
| 1197 |
tuple[Literal["build", "deploy"], ...], |
| 1198 |
Parameter(help="Docstring for steps_to_skip.", negative_iterable=(), show_choices=True), |
| 1199 |
] = ("build",), |
| 1200 |
): |
| 1201 |
pass |
| 1202 |
|
| 1203 |
actual = capture_format_group_parameters(cmd) |
| 1204 |
expected = dedent( |
| 1205 |
"""\ |
| 1206 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1207 |
│ STEPS-TO-SKIP Docstring for steps_to_skip. [choices: build, │ |
| 1208 |
│ --steps-to-skip deploy] [default: ('build',)] │ |
| 1209 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1210 |
""" |
| 1211 |
) |
| 1212 |
print(actual) |
| 1213 |
assert actual == expected |
| 1214 |
|
| 1215 |
|
| 1216 |
def test_help_format_group_parameters_env_var(capture_format_group_parameters): |
| 1217 |
def cmd( |
| 1218 |
foo: Annotated[int, Parameter(env_var=["FOO", "BAR"], help="Docstring for foo.")] = 123, |
| 1219 |
): |
| 1220 |
pass |
| 1221 |
|
| 1222 |
actual = capture_format_group_parameters(cmd) |
| 1223 |
expected = dedent( |
| 1224 |
"""\ |
| 1225 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1226 |
│ FOO --foo Docstring for foo. [env var: FOO, BAR] [default: 123] │ |
| 1227 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1228 |
""" |
| 1229 |
) |
| 1230 |
assert actual == expected |
| 1231 |
|
| 1232 |
|
| 1233 |
def test_help_print_function(app, console): |
| 1234 |
@app.command(help="Cmd help string.") |
| 1235 |
def cmd( |
| 1236 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1237 |
*, |
| 1238 |
bar: Annotated[str, Parameter(help="Docstring for bar.")], |
| 1239 |
): |
| 1240 |
pass |
| 1241 |
|
| 1242 |
with console.capture() as capture: |
| 1243 |
app.help_print(["cmd"], console=console) |
| 1244 |
|
| 1245 |
actual = capture.get() |
| 1246 |
expected = dedent( |
| 1247 |
"""\ |
| 1248 |
Usage: app cmd --bar STR FOO |
| 1249 |
|
| 1250 |
Cmd help string. |
| 1251 |
|
| 1252 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1253 |
│ * FOO --foo Docstring for foo. [required] │ |
| 1254 |
│ * --bar Docstring for bar. [required] │ |
| 1255 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1256 |
""" |
| 1257 |
) |
| 1258 |
assert actual == expected |
| 1259 |
|
| 1260 |
|
| 1261 |
def test_help_print_parameter_required(app, console): |
| 1262 |
""" |
| 1263 |
Notes |
| 1264 |
----- |
| 1265 |
* The default value should not show up in the help-page. |
| 1266 |
""" |
| 1267 |
|
| 1268 |
@app.command(help="Cmd help string.") |
| 1269 |
def cmd( |
| 1270 |
foo: Annotated[str, Parameter(required=False, help="Docstring for foo.")], |
| 1271 |
*, |
| 1272 |
bar: Annotated[str, Parameter(required=True, help="Docstring for bar.")] = "some-default-value", |
| 1273 |
): |
| 1274 |
pass |
| 1275 |
|
| 1276 |
with console.capture() as capture: |
| 1277 |
app.help_print(["cmd"], console=console) |
| 1278 |
|
| 1279 |
actual = capture.get() |
| 1280 |
expected = dedent( |
| 1281 |
"""\ |
| 1282 |
Usage: app cmd --bar STR [ARGS] |
| 1283 |
|
| 1284 |
Cmd help string. |
| 1285 |
|
| 1286 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1287 |
│ FOO --foo Docstring for foo. │ |
| 1288 |
│ * --bar Docstring for bar. [required] │ |
| 1289 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1290 |
""" |
| 1291 |
) |
| 1292 |
assert actual == expected |
| 1293 |
|
| 1294 |
with pytest.raises(MissingArgumentError): |
| 1295 |
app.parse_args("cmd value1", exit_on_error=False) |
| 1296 |
|
| 1297 |
|
| 1298 |
def test_help_print_function_defaults(app, console): |
| 1299 |
@app.command(help="Cmd help string.") |
| 1300 |
def cmd( |
| 1301 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 1302 |
bar: Annotated[str, Parameter(help="Docstring for bar.")] = "bar-value", |
| 1303 |
baz: Annotated[str, Parameter(help="Docstring for bar.", env_var="BAZ")] = "baz-value", |
| 1304 |
): |
| 1305 |
pass |
| 1306 |
|
| 1307 |
with console.capture() as capture: |
| 1308 |
app.help_print(["cmd"], console=console) |
| 1309 |
|
| 1310 |
actual = capture.get() |
| 1311 |
expected = dedent( |
| 1312 |
"""\ |
| 1313 |
Usage: app cmd [OPTIONS] |
| 1314 |
|
| 1315 |
Cmd help string. |
| 1316 |
|
| 1317 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1318 |
│ --bar Docstring for bar. [default: bar-value] │ |
| 1319 |
│ --baz Docstring for bar. [env var: BAZ] [default: baz-value] │ |
| 1320 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1321 |
""" |
| 1322 |
) |
| 1323 |
assert actual == expected |
| 1324 |
|
| 1325 |
|
| 1326 |
def test_help_print_function_no_parse(app, console): |
| 1327 |
@app.command(help="Cmd help string.") |
| 1328 |
def cmd( |
| 1329 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1330 |
*, |
| 1331 |
bar: Annotated[str, Parameter(parse=False)], |
| 1332 |
): |
| 1333 |
pass |
| 1334 |
|
| 1335 |
with console.capture() as capture: |
| 1336 |
app.help_print(["cmd"], console=console) |
| 1337 |
|
| 1338 |
actual = capture.get() |
| 1339 |
expected = dedent( |
| 1340 |
"""\ |
| 1341 |
Usage: app cmd FOO |
| 1342 |
|
| 1343 |
Cmd help string. |
| 1344 |
|
| 1345 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1346 |
│ * FOO --foo Docstring for foo. [required] │ |
| 1347 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1348 |
""" |
| 1349 |
) |
| 1350 |
assert actual == expected |
| 1351 |
|
| 1352 |
|
| 1353 |
def test_help_print_parameter_group_description(app, console): |
| 1354 |
@app.command(group_parameters=Group("Custom Title", help="Parameter description.")) |
| 1355 |
def cmd( |
| 1356 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1357 |
*, |
| 1358 |
bar: Annotated[str, Parameter(parse=False)], |
| 1359 |
): |
| 1360 |
pass |
| 1361 |
|
| 1362 |
with console.capture() as capture: |
| 1363 |
app.help_print(["cmd"], console=console) |
| 1364 |
|
| 1365 |
actual = capture.get() |
| 1366 |
expected = dedent( |
| 1367 |
"""\ |
| 1368 |
Usage: app cmd FOO |
| 1369 |
|
| 1370 |
╭─ Custom Title ─────────────────────────────────────────────────────╮ |
| 1371 |
│ Parameter description. │ |
| 1372 |
│ │ |
| 1373 |
│ * FOO --foo Docstring for foo. [required] │ |
| 1374 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1375 |
""" |
| 1376 |
) |
| 1377 |
|
| 1378 |
assert actual == expected |
| 1379 |
|
| 1380 |
|
| 1381 |
def test_help_print_parameter_group_no_show(app, console): |
| 1382 |
no_show_group = Group("Custom Title", help="Parameter description.", show=False) |
| 1383 |
|
| 1384 |
@app.command |
| 1385 |
def cmd( |
| 1386 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1387 |
bar: Annotated[str, Parameter(help="Docstring for foo.", group=no_show_group)], |
| 1388 |
): |
| 1389 |
pass |
| 1390 |
|
| 1391 |
with console.capture() as capture: |
| 1392 |
app.help_print(["cmd"], console=console) |
| 1393 |
|
| 1394 |
actual = capture.get() |
| 1395 |
expected = dedent( |
| 1396 |
"""\ |
| 1397 |
Usage: app cmd FOO BAR |
| 1398 |
|
| 1399 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1400 |
│ * FOO --foo Docstring for foo. [required] │ |
| 1401 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1402 |
""" |
| 1403 |
) |
| 1404 |
|
| 1405 |
assert actual == expected |
| 1406 |
|
| 1407 |
|
| 1408 |
def test_help_print_command_group_description(app, console): |
| 1409 |
@app.command(group=Group("Custom Title", help="Command description.")) |
| 1410 |
def cmd( |
| 1411 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1412 |
*, |
| 1413 |
bar: Annotated[str, Parameter(parse=False)], |
| 1414 |
): |
| 1415 |
pass |
| 1416 |
|
| 1417 |
with console.capture() as capture: |
| 1418 |
app.help_print([], console=console) |
| 1419 |
|
| 1420 |
actual = capture.get() |
| 1421 |
expected = dedent( |
| 1422 |
"""\ |
| 1423 |
Usage: app COMMAND |
| 1424 |
|
| 1425 |
App Help String Line 1. |
| 1426 |
|
| 1427 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1428 |
│ --help (-h) Display this message and exit. │ |
| 1429 |
│ --version Display application version. │ |
| 1430 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1431 |
╭─ Custom Title ─────────────────────────────────────────────────────╮ |
| 1432 |
│ Command description. │ |
| 1433 |
│ │ |
| 1434 |
│ cmd │ |
| 1435 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1436 |
""" |
| 1437 |
) |
| 1438 |
|
| 1439 |
assert actual == expected |
| 1440 |
|
| 1441 |
|
| 1442 |
def test_help_print_command_group_no_show(app, console): |
| 1443 |
no_show_group = Group("Custom Title", show=False) |
| 1444 |
|
| 1445 |
@app.command(group=no_show_group) |
| 1446 |
def cmd1(): |
| 1447 |
pass |
| 1448 |
|
| 1449 |
@app.command() |
| 1450 |
def cmd2(): |
| 1451 |
pass |
| 1452 |
|
| 1453 |
with console.capture() as capture: |
| 1454 |
app.help_print([], console=console) |
| 1455 |
|
| 1456 |
actual = capture.get() |
| 1457 |
expected = dedent( |
| 1458 |
"""\ |
| 1459 |
Usage: app COMMAND |
| 1460 |
|
| 1461 |
App Help String Line 1. |
| 1462 |
|
| 1463 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1464 |
│ cmd2 │ |
| 1465 |
│ --help (-h) Display this message and exit. │ |
| 1466 |
│ --version Display application version. │ |
| 1467 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1468 |
""" |
| 1469 |
) |
| 1470 |
|
| 1471 |
assert actual == expected |
| 1472 |
|
| 1473 |
|
| 1474 |
def test_help_print_combined_parameter_command_group(app, console): |
| 1475 |
group = Group("Custom Title") |
| 1476 |
|
| 1477 |
app["--help"].group = group |
| 1478 |
|
| 1479 |
@app.default |
| 1480 |
def default(value1: Annotated[int, Parameter(group=group)]): |
| 1481 |
pass |
| 1482 |
|
| 1483 |
with console.capture() as capture: |
| 1484 |
app.help_print([], console=console) |
| 1485 |
|
| 1486 |
actual = capture.get() |
| 1487 |
expected = dedent( |
| 1488 |
"""\ |
| 1489 |
Usage: app VALUE1 |
| 1490 |
|
| 1491 |
App Help String Line 1. |
| 1492 |
|
| 1493 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1494 |
│ --version Display application version. │ |
| 1495 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1496 |
╭─ Custom Title ─────────────────────────────────────────────────────╮ |
| 1497 |
│ * VALUE1 --value1 [required] │ |
| 1498 |
│ --help -h Display this message and exit. │ |
| 1499 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1500 |
""" |
| 1501 |
) |
| 1502 |
|
| 1503 |
assert actual == expected |
| 1504 |
|
| 1505 |
|
| 1506 |
def test_help_print_commands(app, console): |
| 1507 |
@app.command(help="Cmd1 help string.") |
| 1508 |
def cmd1(): |
| 1509 |
pass |
| 1510 |
|
| 1511 |
@app.command(help="Cmd2 help string.") |
| 1512 |
def cmd2(): |
| 1513 |
pass |
| 1514 |
|
| 1515 |
with console.capture() as capture: |
| 1516 |
app.help_print([], console=console) |
| 1517 |
|
| 1518 |
actual = capture.get() |
| 1519 |
expected = dedent( |
| 1520 |
"""\ |
| 1521 |
Usage: app COMMAND |
| 1522 |
|
| 1523 |
App Help String Line 1. |
| 1524 |
|
| 1525 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1526 |
│ cmd1 Cmd1 help string. │ |
| 1527 |
│ cmd2 Cmd2 help string. │ |
| 1528 |
│ --help (-h) Display this message and exit. │ |
| 1529 |
│ --version Display application version. │ |
| 1530 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1531 |
""" |
| 1532 |
) |
| 1533 |
assert actual == expected |
| 1534 |
|
| 1535 |
|
| 1536 |
def test_help_print_commands_with_aliases(app, console): |
| 1537 |
"""Test that command aliases are displayed in parentheses.""" |
| 1538 |
|
| 1539 |
@app.command(alias=["g", "greet-alias"], help="Say hello.") |
| 1540 |
def greet(): |
| 1541 |
pass |
| 1542 |
|
| 1543 |
@app.command(alias="b", help="Say goodbye.") |
| 1544 |
def bye(): |
| 1545 |
pass |
| 1546 |
|
| 1547 |
@app.command(help="No aliases.") |
| 1548 |
def solo(): |
| 1549 |
pass |
| 1550 |
|
| 1551 |
with console.capture() as capture: |
| 1552 |
app.help_print([], console=console) |
| 1553 |
|
| 1554 |
actual = capture.get() |
| 1555 |
expected = dedent( |
| 1556 |
"""\ |
| 1557 |
Usage: app COMMAND |
| 1558 |
|
| 1559 |
App Help String Line 1. |
| 1560 |
|
| 1561 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1562 |
│ bye (b) Say goodbye. │ |
| 1563 |
│ greet (g, greet-alias) Say hello. │ |
| 1564 |
│ solo No aliases. │ |
| 1565 |
│ --help (-h) Display this message and exit. │ |
| 1566 |
│ --version Display application version. │ |
| 1567 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1568 |
""" |
| 1569 |
) |
| 1570 |
assert actual == expected |
| 1571 |
|
| 1572 |
|
| 1573 |
def test_help_print_commands_group_sort_key(app, console): |
| 1574 |
@app.command(group=Group("4", sort_key=5)) |
| 1575 |
def cmd1(): |
| 1576 |
pass |
| 1577 |
|
| 1578 |
@app.command(group=Group("3", sort_key=lambda x: 10)) |
| 1579 |
def cmd2(): |
| 1580 |
pass |
| 1581 |
|
| 1582 |
@app.command(group=Group("2", sort_key=lambda x: None)) |
| 1583 |
def cmd3(): |
| 1584 |
pass |
| 1585 |
|
| 1586 |
@app.command(group=Group("1")) |
| 1587 |
def cmd4(): |
| 1588 |
pass |
| 1589 |
|
| 1590 |
with console.capture() as capture: |
| 1591 |
app.help_print([], console=console) |
| 1592 |
|
| 1593 |
actual = capture.get() |
| 1594 |
expected = dedent( |
| 1595 |
"""\ |
| 1596 |
Usage: app COMMAND |
| 1597 |
|
| 1598 |
App Help String Line 1. |
| 1599 |
|
| 1600 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1601 |
│ --help (-h) Display this message and exit. │ |
| 1602 |
│ --version Display application version. │ |
| 1603 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1604 |
╭─ 4 ────────────────────────────────────────────────────────────────╮ |
| 1605 |
│ cmd1 │ |
| 1606 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1607 |
╭─ 3 ────────────────────────────────────────────────────────────────╮ |
| 1608 |
│ cmd2 │ |
| 1609 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1610 |
╭─ 1 ────────────────────────────────────────────────────────────────╮ |
| 1611 |
│ cmd4 │ |
| 1612 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1613 |
╭─ 2 ────────────────────────────────────────────────────────────────╮ |
| 1614 |
│ cmd3 │ |
| 1615 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1616 |
""" |
| 1617 |
) |
| 1618 |
assert actual == expected |
| 1619 |
|
| 1620 |
|
| 1621 |
def test_help_print_commands_and_function(app, console): |
| 1622 |
@app.command(help="Cmd1 help string.") |
| 1623 |
def cmd1(): |
| 1624 |
pass |
| 1625 |
|
| 1626 |
@app.command(help="Cmd2 help string.") |
| 1627 |
def cmd2(): |
| 1628 |
pass |
| 1629 |
|
| 1630 |
@app.default() |
| 1631 |
def default( |
| 1632 |
foo: Annotated[str, Parameter(help="Docstring for foo.")], |
| 1633 |
*, |
| 1634 |
bar: Annotated[str, Parameter(help="Docstring for bar.")], |
| 1635 |
): |
| 1636 |
pass |
| 1637 |
|
| 1638 |
with console.capture() as capture: |
| 1639 |
app.help_print([], console=console) |
| 1640 |
|
| 1641 |
actual = capture.get() |
| 1642 |
expected = dedent( |
| 1643 |
"""\ |
| 1644 |
Usage: app COMMAND --bar STR FOO |
| 1645 |
|
| 1646 |
App Help String Line 1. |
| 1647 |
|
| 1648 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1649 |
│ cmd1 Cmd1 help string. │ |
| 1650 |
│ cmd2 Cmd2 help string. │ |
| 1651 |
│ --help (-h) Display this message and exit. │ |
| 1652 |
│ --version Display application version. │ |
| 1653 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1654 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1655 |
│ * FOO --foo Docstring for foo. [required] │ |
| 1656 |
│ * --bar Docstring for bar. [required] │ |
| 1657 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1658 |
""" |
| 1659 |
) |
| 1660 |
assert actual == expected |
| 1661 |
|
| 1662 |
|
| 1663 |
def test_help_print_commands_special_flag_reassign(app, console): |
| 1664 |
app["--help"].group = "Admin" |
| 1665 |
with console.capture() as capture: |
| 1666 |
app.help_print([], console=console) |
| 1667 |
|
| 1668 |
actual = capture.get() |
| 1669 |
expected = dedent( |
| 1670 |
"""\ |
| 1671 |
Usage: app |
| 1672 |
|
| 1673 |
App Help String Line 1. |
| 1674 |
|
| 1675 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1676 |
│ --version Display application version. │ |
| 1677 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1678 |
╭─ Admin ────────────────────────────────────────────────────────────╮ |
| 1679 |
│ --help (-h) Display this message and exit. │ |
| 1680 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1681 |
""" |
| 1682 |
) |
| 1683 |
assert actual == expected |
| 1684 |
|
| 1685 |
|
| 1686 |
def test_help_print_parameters_no_negative_from_default_parameter(app, console): |
| 1687 |
app.default_parameter = Parameter(negative=()) |
| 1688 |
|
| 1689 |
@app.command |
| 1690 |
def foo(*, flag: bool): |
| 1691 |
pass |
| 1692 |
|
| 1693 |
with console.capture() as capture: |
| 1694 |
app.help_print(["foo"], console=console) |
| 1695 |
|
| 1696 |
actual = capture.get() |
| 1697 |
expected = dedent( |
| 1698 |
"""\ |
| 1699 |
Usage: app foo --flag BOOL |
| 1700 |
|
| 1701 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1702 |
│ * --flag [required] │ |
| 1703 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1704 |
""" |
| 1705 |
) |
| 1706 |
assert actual == expected |
| 1707 |
|
| 1708 |
|
| 1709 |
def test_help_print_commands_plus_meta(console): |
| 1710 |
app = App( |
| 1711 |
name="app", |
| 1712 |
help_flags=[], |
| 1713 |
version_flags=[], |
| 1714 |
help="App Help String Line 1.", |
| 1715 |
) |
| 1716 |
|
| 1717 |
app.meta.group_arguments = "Session Arguments" |
| 1718 |
app.meta.group_parameters = "Session Parameters" |
| 1719 |
|
| 1720 |
@app.command(help="Cmd1 help string.") |
| 1721 |
def cmd1(): |
| 1722 |
pass |
| 1723 |
|
| 1724 |
@app.meta.command(help="Meta cmd help string.") |
| 1725 |
def meta_cmd(): |
| 1726 |
pass |
| 1727 |
|
| 1728 |
@app.command(help="Cmd2 help string.") |
| 1729 |
def cmd2(): |
| 1730 |
pass |
| 1731 |
|
| 1732 |
@app.meta.default |
| 1733 |
def main( |
| 1734 |
*tokens: Annotated[str, Parameter(show=False)], |
| 1735 |
hostname: Annotated[str, Parameter(help="Hostname to connect to.")], |
| 1736 |
): |
| 1737 |
pass |
| 1738 |
|
| 1739 |
app.meta.help_flags = "--help" |
| 1740 |
app.meta["--help"].group = "Admin" |
| 1741 |
|
| 1742 |
with console.capture() as capture: |
| 1743 |
app.help_print([], console=console) |
| 1744 |
|
| 1745 |
actual = capture.get() |
| 1746 |
expected = dedent( |
| 1747 |
"""\ |
| 1748 |
Usage: app COMMAND --hostname STR |
| 1749 |
|
| 1750 |
App Help String Line 1. |
| 1751 |
|
| 1752 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1753 |
│ cmd1 Cmd1 help string. │ |
| 1754 |
│ cmd2 Cmd2 help string. │ |
| 1755 |
│ meta-cmd Meta cmd help string. │ |
| 1756 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1757 |
╭─ Admin ────────────────────────────────────────────────────────────╮ |
| 1758 |
│ --help Display this message and exit. │ |
| 1759 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1760 |
╭─ Session Parameters ───────────────────────────────────────────────╮ |
| 1761 |
│ * --hostname Hostname to connect to. [required] │ |
| 1762 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1763 |
""" |
| 1764 |
) |
| 1765 |
assert actual == expected |
| 1766 |
|
| 1767 |
|
| 1768 |
def test_help_print_commands_sort_key(app, console): |
| 1769 |
@app.command |
| 1770 |
def alice(): |
| 1771 |
pass |
| 1772 |
|
| 1773 |
@app.command |
| 1774 |
def bob(): |
| 1775 |
pass |
| 1776 |
|
| 1777 |
app["bob"].sort_key = 2 |
| 1778 |
|
| 1779 |
@app.command(sort_key=1) |
| 1780 |
def charlie(): |
| 1781 |
pass |
| 1782 |
|
| 1783 |
with console.capture() as capture: |
| 1784 |
app.help_print([], console=console) |
| 1785 |
|
| 1786 |
actual = capture.get() |
| 1787 |
|
| 1788 |
expected = dedent( |
| 1789 |
"""\ |
| 1790 |
Usage: app COMMAND |
| 1791 |
|
| 1792 |
App Help String Line 1. |
| 1793 |
|
| 1794 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1795 |
│ charlie │ |
| 1796 |
│ bob │ |
| 1797 |
│ alice │ |
| 1798 |
│ --help (-h) Display this message and exit. │ |
| 1799 |
│ --version Display application version. │ |
| 1800 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1801 |
""" |
| 1802 |
) |
| 1803 |
assert actual == expected |
| 1804 |
|
| 1805 |
|
| 1806 |
def test_help_print_commands_sort_key_generator(app, console): |
| 1807 |
"""Test that App.sort_key accepts and processes generators.""" |
| 1808 |
|
| 1809 |
def sort_key_gen(): |
| 1810 |
yield 2 |
| 1811 |
|
| 1812 |
@app.command(sort_key=(x for x in [1])) |
| 1813 |
def alice(): |
| 1814 |
pass |
| 1815 |
|
| 1816 |
@app.command |
| 1817 |
def bob(): |
| 1818 |
pass |
| 1819 |
|
| 1820 |
|
| 1821 |
app["bob"].sort_key = sort_key_gen() |
| 1822 |
|
| 1823 |
@app.command(sort_key=3) |
| 1824 |
def charlie(): |
| 1825 |
pass |
| 1826 |
|
| 1827 |
with console.capture() as capture: |
| 1828 |
app.help_print([], console=console) |
| 1829 |
|
| 1830 |
actual = capture.get() |
| 1831 |
|
| 1832 |
|
| 1833 |
assert "│ alice" in actual |
| 1834 |
assert "│ bob" in actual |
| 1835 |
assert "│ charlie" in actual |
| 1836 |
|
| 1837 |
|
| 1838 |
alice_pos = actual.index("alice") |
| 1839 |
bob_pos = actual.index("bob") |
| 1840 |
charlie_pos = actual.index("charlie") |
| 1841 |
|
| 1842 |
assert alice_pos < bob_pos < charlie_pos, ( |
| 1843 |
f"Commands not in expected order. Positions: alice={alice_pos}, bob={bob_pos}, charlie={charlie_pos}" |
| 1844 |
) |
| 1845 |
|
| 1846 |
|
| 1847 |
def test_help_print_commands_plus_meta_short(app, console): |
| 1848 |
app.help = None |
| 1849 |
|
| 1850 |
@app.command(help="Cmd1 help string.") |
| 1851 |
def cmd1(): |
| 1852 |
pass |
| 1853 |
|
| 1854 |
app.meta.group_arguments = "Session Arguments" |
| 1855 |
app.meta.group_parameters = "Session Parameters" |
| 1856 |
|
| 1857 |
@app.meta.command(help="Meta cmd help string.") |
| 1858 |
def meta_cmd(a: int): |
| 1859 |
""" |
| 1860 |
|
| 1861 |
Parameters |
| 1862 |
---------- |
| 1863 |
a |
| 1864 |
Some value. |
| 1865 |
""" |
| 1866 |
pass |
| 1867 |
|
| 1868 |
|
| 1869 |
app["meta-cmd"].group_parameters = app.group_parameters |
| 1870 |
|
| 1871 |
@app.command(help="Cmd2 help string.") |
| 1872 |
def cmd2(): |
| 1873 |
pass |
| 1874 |
|
| 1875 |
@app.meta.default |
| 1876 |
def main( |
| 1877 |
*tokens: str, |
| 1878 |
hostname: Annotated[str, Parameter(name=["--hostname", "-n"], help="Hostname to connect to.")], |
| 1879 |
): |
| 1880 |
"""App Help String Line 1 from meta.""" |
| 1881 |
pass |
| 1882 |
|
| 1883 |
with console.capture() as capture: |
| 1884 |
app.help_print([], console=console) |
| 1885 |
|
| 1886 |
actual = capture.get() |
| 1887 |
expected = dedent( |
| 1888 |
"""\ |
| 1889 |
Usage: app COMMAND --hostname STR [ARGS...] |
| 1890 |
|
| 1891 |
App Help String Line 1 from meta. |
| 1892 |
|
| 1893 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1894 |
│ cmd1 Cmd1 help string. │ |
| 1895 |
│ cmd2 Cmd2 help string. │ |
| 1896 |
│ meta-cmd Meta cmd help string. │ |
| 1897 |
│ --help (-h) Display this message and exit. │ |
| 1898 |
│ --version Display application version. │ |
| 1899 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1900 |
╭─ Session Arguments ────────────────────────────────────────────────╮ |
| 1901 |
│ TOKENS │ |
| 1902 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1903 |
╭─ Session Parameters ───────────────────────────────────────────────╮ |
| 1904 |
│ * --hostname -n Hostname to connect to. [required] │ |
| 1905 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1906 |
""" |
| 1907 |
) |
| 1908 |
assert actual == expected |
| 1909 |
|
| 1910 |
|
| 1911 |
@app.default |
| 1912 |
def root_default_cmd(rdp): |
| 1913 |
"""Root Default Command Short Description. |
| 1914 |
|
| 1915 |
Parameters |
| 1916 |
---------- |
| 1917 |
rdp: |
| 1918 |
RDP description. |
| 1919 |
""" |
| 1920 |
pass |
| 1921 |
|
| 1922 |
with console.capture() as capture: |
| 1923 |
app.help_print([], console=console) |
| 1924 |
|
| 1925 |
actual = capture.get() |
| 1926 |
expected = dedent( |
| 1927 |
"""\ |
| 1928 |
Usage: app COMMAND --hostname STR RDP [ARGS...] |
| 1929 |
|
| 1930 |
Root Default Command Short Description. |
| 1931 |
|
| 1932 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 1933 |
│ cmd1 Cmd1 help string. │ |
| 1934 |
│ cmd2 Cmd2 help string. │ |
| 1935 |
│ meta-cmd Meta cmd help string. │ |
| 1936 |
│ --help (-h) Display this message and exit. │ |
| 1937 |
│ --version Display application version. │ |
| 1938 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1939 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1940 |
│ * RDP --rdp RDP description. [required] │ |
| 1941 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1942 |
╭─ Session Arguments ────────────────────────────────────────────────╮ |
| 1943 |
│ TOKENS │ |
| 1944 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1945 |
╭─ Session Parameters ───────────────────────────────────────────────╮ |
| 1946 |
│ * --hostname -n Hostname to connect to. [required] │ |
| 1947 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1948 |
""" |
| 1949 |
) |
| 1950 |
assert actual == expected |
| 1951 |
|
| 1952 |
|
| 1953 |
with console.capture() as capture: |
| 1954 |
app.help_print(["meta-cmd"], console=console) |
| 1955 |
|
| 1956 |
actual = capture.get() |
| 1957 |
expected = dedent( |
| 1958 |
"""\ |
| 1959 |
Usage: app meta-cmd A |
| 1960 |
|
| 1961 |
Meta cmd help string. |
| 1962 |
|
| 1963 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 1964 |
│ * A --a Some value. [required] │ |
| 1965 |
╰────────────────────────────────────────────────────────────────────╯ |
| 1966 |
""" |
| 1967 |
) |
| 1968 |
assert actual == expected |
| 1969 |
|
| 1970 |
|
| 1971 |
def test_help_restructuredtext(app, console, normalize_trailing_whitespace): |
| 1972 |
description = dedent( |
| 1973 |
"""\ |
| 1974 |
This is a long sentence that |
| 1975 |
is spread across |
| 1976 |
three lines. |
| 1977 |
|
| 1978 |
This is a new paragraph. |
| 1979 |
This is another sentence of that paragraph. |
| 1980 |
`This is a hyperlink. <https://cyclopts.readthedocs.io>`_ |
| 1981 |
|
| 1982 |
The following are bulletpoints: |
| 1983 |
|
| 1984 |
* bulletpoint 1 |
| 1985 |
* bulletpoint 2 |
| 1986 |
""" |
| 1987 |
) |
| 1988 |
app = App(help=description, help_format="rst") |
| 1989 |
|
| 1990 |
@app.command |
| 1991 |
def foo(bar): |
| 1992 |
"""This is **bold**.""" |
| 1993 |
|
| 1994 |
with console.capture() as capture: |
| 1995 |
app.help_print([], console=console) |
| 1996 |
|
| 1997 |
actual = capture.get() |
| 1998 |
|
| 1999 |
expected = dedent( |
| 2000 |
"""\ |
| 2001 |
Usage: test_help COMMAND |
| 2002 |
|
| 2003 |
This is a long sentence that is spread across three lines. |
| 2004 |
|
| 2005 |
This is a new paragraph. This is another sentence of that paragraph. |
| 2006 |
This is a hyperlink. |
| 2007 |
|
| 2008 |
The following are bulletpoints: |
| 2009 |
|
| 2010 |
• bulletpoint 1 |
| 2011 |
• bulletpoint 2 |
| 2012 |
|
| 2013 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2014 |
│ foo This is bold. │ |
| 2015 |
│ --help (-h) Display this message and exit. │ |
| 2016 |
│ --version Display application version. │ |
| 2017 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2018 |
""" |
| 2019 |
) |
| 2020 |
|
| 2021 |
|
| 2022 |
assert normalize_trailing_whitespace(actual) == expected |
| 2023 |
|
| 2024 |
|
| 2025 |
def test_help_markdown(app, console, normalize_trailing_whitespace): |
| 2026 |
description = dedent( |
| 2027 |
"""\ |
| 2028 |
This is a long sentence that |
| 2029 |
is spread across |
| 2030 |
three lines. |
| 2031 |
|
| 2032 |
This is a new paragraph. |
| 2033 |
This is another sentence of that paragraph. |
| 2034 |
[This is a hyperlink.](https://cyclopts.readthedocs.io) |
| 2035 |
|
| 2036 |
The following are bulletpoints: |
| 2037 |
|
| 2038 |
* bulletpoint 1 |
| 2039 |
* bulletpoint 2 |
| 2040 |
""" |
| 2041 |
) |
| 2042 |
app = App(help=description, help_format="markdown") |
| 2043 |
|
| 2044 |
@app.command |
| 2045 |
def foo(bar): |
| 2046 |
"""This is **bold**.""" |
| 2047 |
|
| 2048 |
with console.capture() as capture: |
| 2049 |
app.help_print([], console=console) |
| 2050 |
|
| 2051 |
actual = capture.get() |
| 2052 |
|
| 2053 |
expected = dedent( |
| 2054 |
"""\ |
| 2055 |
Usage: test_help COMMAND |
| 2056 |
|
| 2057 |
This is a long sentence that is spread across three lines. |
| 2058 |
|
| 2059 |
This is a new paragraph. This is another sentence of that paragraph. |
| 2060 |
This is a hyperlink. |
| 2061 |
|
| 2062 |
The following are bulletpoints: |
| 2063 |
|
| 2064 |
• bulletpoint 1 |
| 2065 |
• bulletpoint 2 |
| 2066 |
|
| 2067 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2068 |
│ foo This is bold. │ |
| 2069 |
│ --help (-h) Display this message and exit. │ |
| 2070 |
│ --version Display application version. │ |
| 2071 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2072 |
""" |
| 2073 |
) |
| 2074 |
|
| 2075 |
|
| 2076 |
assert normalize_trailing_whitespace(actual) == expected |
| 2077 |
|
| 2078 |
|
| 2079 |
def test_help_rich(app, console, normalize_trailing_whitespace): |
| 2080 |
"""Newlines actually get interpreted with rich.""" |
| 2081 |
description = dedent( |
| 2082 |
"""\ |
| 2083 |
This is a short-description that |
| 2084 |
is spread across |
| 2085 |
three lines (against PEP0257). |
| 2086 |
|
| 2087 |
This is the first sentence of the long-description. |
| 2088 |
This is another sentence of that paragraph. |
| 2089 |
[red]This text is red.[/red] |
| 2090 |
""" |
| 2091 |
) |
| 2092 |
app = App(help=description, help_format="rich") |
| 2093 |
|
| 2094 |
@app.command |
| 2095 |
def foo(bar): |
| 2096 |
"""This is [italic]italic[/italic].""" |
| 2097 |
|
| 2098 |
with console.capture() as capture: |
| 2099 |
app.help_print([], console=console) |
| 2100 |
|
| 2101 |
actual = capture.get() |
| 2102 |
|
| 2103 |
expected = dedent( |
| 2104 |
"""\ |
| 2105 |
Usage: test_help COMMAND |
| 2106 |
|
| 2107 |
This is a short-description that is spread across three lines (against |
| 2108 |
PEP0257). |
| 2109 |
|
| 2110 |
This is the first sentence of the long-description. |
| 2111 |
This is another sentence of that paragraph. |
| 2112 |
This text is red. |
| 2113 |
|
| 2114 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2115 |
│ foo This is italic. │ |
| 2116 |
│ --help (-h) Display this message and exit. │ |
| 2117 |
│ --version Display application version. │ |
| 2118 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2119 |
""" |
| 2120 |
) |
| 2121 |
|
| 2122 |
assert actual == expected |
| 2123 |
|
| 2124 |
|
| 2125 |
def test_help_plaintext(app, console): |
| 2126 |
"""Tests that plaintext documents don't get interpreted otherwise.""" |
| 2127 |
description = dedent( |
| 2128 |
"""\ |
| 2129 |
This is the short description. |
| 2130 |
|
| 2131 |
This is a long sentence that |
| 2132 |
is spread across |
| 2133 |
three lines. |
| 2134 |
|
| 2135 |
This is a new paragraph. |
| 2136 |
This is another sentence of that paragraph. |
| 2137 |
[red]This text is red.[/red] |
| 2138 |
|
| 2139 |
These are bulletpoints: |
| 2140 |
|
| 2141 |
* point 1 |
| 2142 |
* point 2 |
| 2143 |
""" |
| 2144 |
) |
| 2145 |
app = App(help=description, help_format="plaintext") |
| 2146 |
|
| 2147 |
@app.command |
| 2148 |
def foo(bar): |
| 2149 |
"""This is [italic]italic[/italic].""" |
| 2150 |
|
| 2151 |
with console.capture() as capture: |
| 2152 |
app.help_print([], console=console) |
| 2153 |
|
| 2154 |
actual = capture.get() |
| 2155 |
|
| 2156 |
expected = dedent( |
| 2157 |
"""\ |
| 2158 |
Usage: test_help COMMAND |
| 2159 |
|
| 2160 |
This is the short description. |
| 2161 |
|
| 2162 |
This is a long sentence that |
| 2163 |
is spread across |
| 2164 |
three lines. |
| 2165 |
|
| 2166 |
This is a new paragraph. |
| 2167 |
This is another sentence of that paragraph. |
| 2168 |
[red]This text is red.[/red] |
| 2169 |
|
| 2170 |
These are bulletpoints: |
| 2171 |
|
| 2172 |
* point 1 |
| 2173 |
* point 2 |
| 2174 |
|
| 2175 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2176 |
│ foo This is [italic]italic[/italic]. │ |
| 2177 |
│ --help (-h) Display this message and exit. │ |
| 2178 |
│ --version Display application version. │ |
| 2179 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2180 |
""" |
| 2181 |
) |
| 2182 |
|
| 2183 |
assert actual == expected |
| 2184 |
|
| 2185 |
|
| 2186 |
def test_help_consistent_formatting(app, console): |
| 2187 |
"""Checks to make sure short-descriptions and full-descriptions |
| 2188 |
are rendered using the same formatter. |
| 2189 |
|
| 2190 |
https://github.com/BrianPugh/cyclopts/issues/113 |
| 2191 |
""" |
| 2192 |
app.help_format = "markdown" |
| 2193 |
|
| 2194 |
@app.command |
| 2195 |
def cmd(): |
| 2196 |
"""[bold]Short description[/bold].""" |
| 2197 |
|
| 2198 |
with console.capture() as capture: |
| 2199 |
app.help_print([], console=console) |
| 2200 |
|
| 2201 |
actual_help = capture.get() |
| 2202 |
|
| 2203 |
|
| 2204 |
actual_help = next(x for x in actual_help.split("\n") if "Short description" in x) |
| 2205 |
actual_help = actual_help[5:-1].strip() |
| 2206 |
|
| 2207 |
with console.capture() as capture: |
| 2208 |
app.help_print(["cmd"], console=console) |
| 2209 |
|
| 2210 |
|
| 2211 |
actual_cmd_help = capture.get() |
| 2212 |
actual_cmd_help = next(x for x in actual_cmd_help.split("\n") if "Short description" in x) |
| 2213 |
actual_cmd_help = actual_cmd_help.strip() |
| 2214 |
|
| 2215 |
assert actual_help == actual_cmd_help |
| 2216 |
|
| 2217 |
|
| 2218 |
def test_help_help_on_error(app, console): |
| 2219 |
app.help = "This is the App's Help." |
| 2220 |
app.help_on_error = True |
| 2221 |
|
| 2222 |
@app.command |
| 2223 |
def foo(count: int): |
| 2224 |
"""This is Foo's Help.""" |
| 2225 |
pass |
| 2226 |
|
| 2227 |
with console.capture() as capture, pytest.raises(CoercionError): |
| 2228 |
app(["foo", "bar"], error_console=console, exit_on_error=False) |
| 2229 |
|
| 2230 |
actual = capture.get() |
| 2231 |
expected = dedent( |
| 2232 |
"""\ |
| 2233 |
Usage: app foo COUNT |
| 2234 |
|
| 2235 |
This is Foo's Help. |
| 2236 |
|
| 2237 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2238 |
│ * COUNT --count [required] │ |
| 2239 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2240 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 2241 |
│ Invalid value for COUNT: unable to convert "bar" into int. │ |
| 2242 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2243 |
""" |
| 2244 |
) |
| 2245 |
assert actual == expected |
| 2246 |
|
| 2247 |
|
| 2248 |
def test_issue_373_help_space_with_meta_app(app, console): |
| 2249 |
@app.default |
| 2250 |
def default(value: str): |
| 2251 |
print(f"{value=}") |
| 2252 |
|
| 2253 |
@app.meta.default |
| 2254 |
def meta( |
| 2255 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 2256 |
meta_value: int = 3, |
| 2257 |
): |
| 2258 |
app(tokens) |
| 2259 |
|
| 2260 |
with console.capture() as capture: |
| 2261 |
app("--help", console=console, exit_on_error=False) |
| 2262 |
|
| 2263 |
actual = capture.get() |
| 2264 |
expected = dedent( |
| 2265 |
"""\ |
| 2266 |
Usage: app [OPTIONS] VALUE |
| 2267 |
|
| 2268 |
App Help String Line 1. |
| 2269 |
|
| 2270 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2271 |
│ --help (-h) Display this message and exit. │ |
| 2272 |
│ --version Display application version. │ |
| 2273 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2274 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2275 |
│ * VALUE --value [required] │ |
| 2276 |
│ --meta-value [default: 3] │ |
| 2277 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2278 |
""" |
| 2279 |
) |
| 2280 |
assert actual == expected |
| 2281 |
|
| 2282 |
|
| 2283 |
def test_format_plain_formatter(console): |
| 2284 |
"""Test that PlainFormatter produces correct plain text output.""" |
| 2285 |
app = App( |
| 2286 |
name="test_app", |
| 2287 |
help="Test application for PlainFormatter", |
| 2288 |
help_formatter=PlainFormatter(), |
| 2289 |
) |
| 2290 |
|
| 2291 |
@app.command |
| 2292 |
def cmd1(arg: str = "default"): |
| 2293 |
"""First test command.""" |
| 2294 |
print(f"cmd1 with {arg}") |
| 2295 |
|
| 2296 |
@app.command |
| 2297 |
def cmd2(required: int, optional: bool = False): |
| 2298 |
"""Second test command.""" |
| 2299 |
print(f"cmd2 with {required} and {optional}") |
| 2300 |
|
| 2301 |
@app.default |
| 2302 |
def main(verbose: bool = False): |
| 2303 |
"""Main function with a parameter.""" |
| 2304 |
print(f"verbose={verbose}") |
| 2305 |
|
| 2306 |
|
| 2307 |
with console.capture() as capture: |
| 2308 |
try: |
| 2309 |
app(["--help"], console=console) |
| 2310 |
except SystemExit: |
| 2311 |
|
| 2312 |
pass |
| 2313 |
|
| 2314 |
actual = capture.get() |
| 2315 |
|
| 2316 |
expected = dedent( |
| 2317 |
"""\ |
| 2318 |
Usage: test_app COMMAND [ARGS] |
| 2319 |
|
| 2320 |
Test application for PlainFormatter |
| 2321 |
|
| 2322 |
Commands: |
| 2323 |
cmd1: First test command. |
| 2324 |
cmd2: Second test command. |
| 2325 |
--help, -h: Display this message and exit. |
| 2326 |
--version: Display application version. |
| 2327 |
|
| 2328 |
Parameters: |
| 2329 |
VERBOSE, --verbose, --no-verbose: [default: False] |
| 2330 |
|
| 2331 |
""" |
| 2332 |
) |
| 2333 |
assert actual == expected |
| 2334 |
|
| 2335 |
|
| 2336 |
def test_help_format_group_parameters_sphinx_only_directives(capture_format_group_parameters): |
| 2337 |
"""Test parameters with only directives and no other text.""" |
| 2338 |
|
| 2339 |
def cmd(foo: str): |
| 2340 |
""" |
| 2341 |
Command with directive-only description. |
| 2342 |
|
| 2343 |
Parameters |
| 2344 |
---------- |
| 2345 |
foo: str |
| 2346 |
.. versionadded:: 1.0 |
| 2347 |
""" |
| 2348 |
|
| 2349 |
actual = capture_format_group_parameters(cmd) |
| 2350 |
expected = dedent( |
| 2351 |
"""\ |
| 2352 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2353 |
│ * FOO --foo [Added in v1.0] [required] │ |
| 2354 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2355 |
""" |
| 2356 |
) |
| 2357 |
assert actual == expected |
| 2358 |
|
| 2359 |
|
| 2360 |
def test_help_format_group_parameters_sphinx_nested_indentation(capture_format_group_parameters): |
| 2361 |
"""Test directives with multi-level indented content.""" |
| 2362 |
|
| 2363 |
def cmd(foo: str): |
| 2364 |
""" |
| 2365 |
Command with nested indentation in directive. |
| 2366 |
|
| 2367 |
Parameters |
| 2368 |
---------- |
| 2369 |
foo: str |
| 2370 |
Foo parameter |
| 2371 |
|
| 2372 |
.. note:: |
| 2373 |
This is a long note |
| 2374 |
that spans multiple lines |
| 2375 |
with consistent indentation |
| 2376 |
""" |
| 2377 |
|
| 2378 |
actual = capture_format_group_parameters(cmd) |
| 2379 |
expected = dedent( |
| 2380 |
"""\ |
| 2381 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2382 |
│ * FOO --foo Foo parameter │ |
| 2383 |
│ │ |
| 2384 |
│ Note: This is a long note that spans multiple lines │ |
| 2385 |
│ with consistent indentation [required] │ |
| 2386 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2387 |
""" |
| 2388 |
) |
| 2389 |
assert actual == expected |
| 2390 |
|
| 2391 |
|
| 2392 |
def test_help_format_group_parameters_sphinx_inline_directive_content(capture_format_group_parameters): |
| 2393 |
"""Test directive with inline content on same line.""" |
| 2394 |
|
| 2395 |
def cmd(foo: str): |
| 2396 |
""" |
| 2397 |
Command with inline directive content. |
| 2398 |
|
| 2399 |
Parameters |
| 2400 |
---------- |
| 2401 |
foo: str |
| 2402 |
Foo parameter |
| 2403 |
|
| 2404 |
.. note:: Inline note content |
| 2405 |
""" |
| 2406 |
|
| 2407 |
actual = capture_format_group_parameters(cmd) |
| 2408 |
expected = dedent( |
| 2409 |
"""\ |
| 2410 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2411 |
│ * FOO --foo Foo parameter │ |
| 2412 |
│ │ |
| 2413 |
│ Note: Inline note content [required] │ |
| 2414 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2415 |
""" |
| 2416 |
) |
| 2417 |
assert actual == expected |
| 2418 |
|
| 2419 |
|
| 2420 |
def test_help_flag_after_end_of_options_delimiter(app): |
| 2421 |
"""Help flags after '--' should not trigger help display.""" |
| 2422 |
|
| 2423 |
@app.default |
| 2424 |
def main(*args: str): |
| 2425 |
"""Main function that accepts variadic arguments.""" |
| 2426 |
return args |
| 2427 |
|
| 2428 |
result = app(["--", "--help"]) |
| 2429 |
assert result == ("--help",) |
| 2430 |
|
| 2431 |
result = app(["foo", "--", "--help", "-h"]) |
| 2432 |
assert result == ("foo", "--help", "-h") |
| 2433 |
|
| 2434 |
|
| 2435 |
def test_help_prologue_basic(app, console): |
| 2436 |
"""Test basic prologue rendering.""" |
| 2437 |
app.help_prologue = "This is a prologue." |
| 2438 |
with console.capture() as capture: |
| 2439 |
app([], console=console) |
| 2440 |
|
| 2441 |
actual = capture.get() |
| 2442 |
expected = dedent( |
| 2443 |
"""\ |
| 2444 |
This is a prologue. |
| 2445 |
|
| 2446 |
Usage: app |
| 2447 |
|
| 2448 |
App Help String Line 1. |
| 2449 |
|
| 2450 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2451 |
│ --help (-h) Display this message and exit. │ |
| 2452 |
│ --version Display application version. │ |
| 2453 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2454 |
""" |
| 2455 |
) |
| 2456 |
assert actual == expected |
| 2457 |
|
| 2458 |
|
| 2459 |
def test_help_prologue_multiline(app, console): |
| 2460 |
"""Test prologue with multiple lines using markdown double newlines.""" |
| 2461 |
app.help_prologue = "Line 1\n\nLine 2\n\nLine 3" |
| 2462 |
with console.capture() as capture: |
| 2463 |
app([], console=console) |
| 2464 |
|
| 2465 |
actual = capture.get() |
| 2466 |
|
| 2467 |
assert "Line 1" in actual |
| 2468 |
assert "Line 2" in actual |
| 2469 |
assert "Line 3" in actual |
| 2470 |
|
| 2471 |
|
| 2472 |
def test_help_prologue_none(app, console): |
| 2473 |
"""Test that None prologue doesn't add extra lines.""" |
| 2474 |
app.help_prologue = None |
| 2475 |
with console.capture() as capture: |
| 2476 |
app([], console=console) |
| 2477 |
|
| 2478 |
actual = capture.get() |
| 2479 |
expected = dedent( |
| 2480 |
"""\ |
| 2481 |
Usage: app |
| 2482 |
|
| 2483 |
App Help String Line 1. |
| 2484 |
|
| 2485 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2486 |
│ --help (-h) Display this message and exit. │ |
| 2487 |
│ --version Display application version. │ |
| 2488 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2489 |
""" |
| 2490 |
) |
| 2491 |
assert actual == expected |
| 2492 |
|
| 2493 |
|
| 2494 |
def test_help_prologue_empty_string(app, console): |
| 2495 |
"""Test that empty string prologue doesn't render.""" |
| 2496 |
app.help_prologue = "" |
| 2497 |
with console.capture() as capture: |
| 2498 |
app([], console=console) |
| 2499 |
|
| 2500 |
actual = capture.get() |
| 2501 |
expected = dedent( |
| 2502 |
"""\ |
| 2503 |
Usage: app |
| 2504 |
|
| 2505 |
App Help String Line 1. |
| 2506 |
|
| 2507 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2508 |
│ --help (-h) Display this message and exit. │ |
| 2509 |
│ --version Display application version. │ |
| 2510 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2511 |
""" |
| 2512 |
) |
| 2513 |
assert actual == expected |
| 2514 |
|
| 2515 |
|
| 2516 |
def test_help_prologue_markdown(app, console): |
| 2517 |
"""Test prologue with markdown formatting.""" |
| 2518 |
app.help_prologue = "**Version 1.0.0**\n\nFor more info, visit *https://example.com*" |
| 2519 |
app.help_format = "markdown" |
| 2520 |
with console.capture() as capture: |
| 2521 |
app([], console=console) |
| 2522 |
|
| 2523 |
actual = capture.get() |
| 2524 |
assert "Version 1.0.0" in actual |
| 2525 |
assert "https://example.com" in actual |
| 2526 |
|
| 2527 |
|
| 2528 |
def test_help_prologue_subcommand(app, console): |
| 2529 |
"""Test that subcommands can have their own prologue.""" |
| 2530 |
subapp = App( |
| 2531 |
name="sub", |
| 2532 |
help="Subcommand help.", |
| 2533 |
help_prologue="Subcommand prologue.", |
| 2534 |
) |
| 2535 |
app.command(subapp) |
| 2536 |
|
| 2537 |
with console.capture() as capture: |
| 2538 |
app(["sub"], console=console) |
| 2539 |
|
| 2540 |
actual = capture.get() |
| 2541 |
assert "Subcommand prologue." in actual |
| 2542 |
assert actual.startswith("Subcommand prologue.\n") |
| 2543 |
|
| 2544 |
|
| 2545 |
def test_help_prologue_with_parameters(app, console): |
| 2546 |
"""Test prologue with parameters in help output.""" |
| 2547 |
|
| 2548 |
@app.default |
| 2549 |
def main(verbose: bool = False, count: int = 1): |
| 2550 |
"""Main function.""" |
| 2551 |
pass |
| 2552 |
|
| 2553 |
app.help_prologue = "Header text here." |
| 2554 |
|
| 2555 |
with console.capture() as capture: |
| 2556 |
app(["--help"], console=console) |
| 2557 |
|
| 2558 |
actual = capture.get() |
| 2559 |
assert "verbose" in actual.lower() |
| 2560 |
assert "count" in actual.lower() |
| 2561 |
assert "Header text here." in actual |
| 2562 |
|
| 2563 |
assert "Header text here." in actual |
| 2564 |
|
| 2565 |
|
| 2566 |
def test_help_prologue_inheritance(console): |
| 2567 |
"""Test that prologue inherits from parent to child (like help_format).""" |
| 2568 |
parent = App( |
| 2569 |
name="parent", |
| 2570 |
help="Parent help.", |
| 2571 |
help_prologue="Parent prologue.", |
| 2572 |
result_action="return_value", |
| 2573 |
) |
| 2574 |
|
| 2575 |
child = App( |
| 2576 |
name="child", |
| 2577 |
help="Child help.", |
| 2578 |
|
| 2579 |
) |
| 2580 |
|
| 2581 |
parent.command(child) |
| 2582 |
|
| 2583 |
|
| 2584 |
with console.capture() as capture: |
| 2585 |
parent(["--help"], console=console) |
| 2586 |
|
| 2587 |
actual = capture.get() |
| 2588 |
assert "Parent prologue." in actual |
| 2589 |
|
| 2590 |
|
| 2591 |
with console.capture() as capture: |
| 2592 |
parent(["child", "--help"], console=console) |
| 2593 |
|
| 2594 |
actual = capture.get() |
| 2595 |
assert "Parent prologue." in actual |
| 2596 |
assert "Child help." in actual |
| 2597 |
|
| 2598 |
|
| 2599 |
def test_help_prologue_override(console): |
| 2600 |
"""Test that child can override parent's prologue.""" |
| 2601 |
parent = App( |
| 2602 |
name="parent", |
| 2603 |
help="Parent help.", |
| 2604 |
help_prologue="Parent prologue.", |
| 2605 |
result_action="return_value", |
| 2606 |
) |
| 2607 |
|
| 2608 |
child = App( |
| 2609 |
name="child", |
| 2610 |
help="Child help.", |
| 2611 |
help_prologue="Child prologue (overrides parent).", |
| 2612 |
) |
| 2613 |
|
| 2614 |
parent.command(child) |
| 2615 |
|
| 2616 |
|
| 2617 |
with console.capture() as capture: |
| 2618 |
parent(["--help"], console=console) |
| 2619 |
|
| 2620 |
actual = capture.get() |
| 2621 |
assert "Parent prologue." in actual |
| 2622 |
assert "Child prologue" not in actual |
| 2623 |
|
| 2624 |
|
| 2625 |
with console.capture() as capture: |
| 2626 |
parent(["child", "--help"], console=console) |
| 2627 |
|
| 2628 |
actual = capture.get() |
| 2629 |
assert "Child prologue (overrides parent)." in actual |
| 2630 |
assert "Parent prologue." not in actual |
| 2631 |
|
| 2632 |
|
| 2633 |
def test_help_epilogue_basic(app, console): |
| 2634 |
"""Test basic epilogue rendering.""" |
| 2635 |
app.help_epilogue = "This is an epilogue." |
| 2636 |
with console.capture() as capture: |
| 2637 |
app([], console=console) |
| 2638 |
|
| 2639 |
actual = capture.get() |
| 2640 |
expected = dedent( |
| 2641 |
"""\ |
| 2642 |
Usage: app |
| 2643 |
|
| 2644 |
App Help String Line 1. |
| 2645 |
|
| 2646 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2647 |
│ --help (-h) Display this message and exit. │ |
| 2648 |
│ --version Display application version. │ |
| 2649 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2650 |
|
| 2651 |
This is an epilogue. |
| 2652 |
""" |
| 2653 |
) |
| 2654 |
assert actual == expected |
| 2655 |
|
| 2656 |
|
| 2657 |
def test_help_epilogue_multiline(app, console): |
| 2658 |
"""Test epilogue with multiple lines using markdown double newlines.""" |
| 2659 |
app.help_epilogue = "Line 1\n\nLine 2\n\nLine 3" |
| 2660 |
with console.capture() as capture: |
| 2661 |
app([], console=console) |
| 2662 |
|
| 2663 |
actual = capture.get() |
| 2664 |
|
| 2665 |
assert "Line 1" in actual |
| 2666 |
assert "Line 2" in actual |
| 2667 |
assert "Line 3" in actual |
| 2668 |
|
| 2669 |
|
| 2670 |
def test_help_epilogue_none(app, console): |
| 2671 |
"""Test that None epilogue doesn't add extra lines.""" |
| 2672 |
app.help_epilogue = None |
| 2673 |
with console.capture() as capture: |
| 2674 |
app([], console=console) |
| 2675 |
|
| 2676 |
actual = capture.get() |
| 2677 |
expected = dedent( |
| 2678 |
"""\ |
| 2679 |
Usage: app |
| 2680 |
|
| 2681 |
App Help String Line 1. |
| 2682 |
|
| 2683 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2684 |
│ --help (-h) Display this message and exit. │ |
| 2685 |
│ --version Display application version. │ |
| 2686 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2687 |
""" |
| 2688 |
) |
| 2689 |
assert actual == expected |
| 2690 |
|
| 2691 |
|
| 2692 |
def test_help_epilogue_empty_string(app, console): |
| 2693 |
"""Test that empty string epilogue doesn't render.""" |
| 2694 |
app.help_epilogue = "" |
| 2695 |
with console.capture() as capture: |
| 2696 |
app([], console=console) |
| 2697 |
|
| 2698 |
actual = capture.get() |
| 2699 |
expected = dedent( |
| 2700 |
"""\ |
| 2701 |
Usage: app |
| 2702 |
|
| 2703 |
App Help String Line 1. |
| 2704 |
|
| 2705 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2706 |
│ --help (-h) Display this message and exit. │ |
| 2707 |
│ --version Display application version. │ |
| 2708 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2709 |
""" |
| 2710 |
) |
| 2711 |
assert actual == expected |
| 2712 |
|
| 2713 |
|
| 2714 |
def test_help_epilogue_markdown(app, console): |
| 2715 |
"""Test epilogue with markdown formatting.""" |
| 2716 |
app.help_epilogue = "**Version 1.0.0**\n\nFor more info, visit *https://example.com*" |
| 2717 |
app.help_format = "markdown" |
| 2718 |
with console.capture() as capture: |
| 2719 |
app([], console=console) |
| 2720 |
|
| 2721 |
actual = capture.get() |
| 2722 |
assert "Version 1.0.0" in actual |
| 2723 |
assert "https://example.com" in actual |
| 2724 |
|
| 2725 |
|
| 2726 |
def test_help_epilogue_subcommand(app, console): |
| 2727 |
"""Test that subcommands can have their own epilogue.""" |
| 2728 |
subapp = App( |
| 2729 |
name="sub", |
| 2730 |
help="Subcommand help.", |
| 2731 |
help_epilogue="Subcommand epilogue.", |
| 2732 |
) |
| 2733 |
app.command(subapp) |
| 2734 |
|
| 2735 |
with console.capture() as capture: |
| 2736 |
app(["sub"], console=console) |
| 2737 |
|
| 2738 |
actual = capture.get() |
| 2739 |
assert "Subcommand epilogue." in actual |
| 2740 |
assert actual.endswith("Subcommand epilogue.\n") |
| 2741 |
|
| 2742 |
|
| 2743 |
def test_help_epilogue_with_parameters(app, console): |
| 2744 |
"""Test epilogue with parameters in help output.""" |
| 2745 |
|
| 2746 |
@app.default |
| 2747 |
def main(verbose: bool = False, count: int = 1): |
| 2748 |
"""Main function.""" |
| 2749 |
pass |
| 2750 |
|
| 2751 |
app.help_epilogue = "Footer text here." |
| 2752 |
|
| 2753 |
with console.capture() as capture: |
| 2754 |
app(["--help"], console=console) |
| 2755 |
|
| 2756 |
actual = capture.get() |
| 2757 |
assert "verbose" in actual.lower() |
| 2758 |
assert "count" in actual.lower() |
| 2759 |
assert "Footer text here." in actual |
| 2760 |
|
| 2761 |
assert "Footer text here." in actual |
| 2762 |
|
| 2763 |
|
| 2764 |
def test_help_epilogue_inheritance(console): |
| 2765 |
"""Test that epilogue inherits from parent to child (like help_format).""" |
| 2766 |
parent = App( |
| 2767 |
name="parent", |
| 2768 |
help="Parent help.", |
| 2769 |
help_epilogue="Parent epilogue.", |
| 2770 |
result_action="return_value", |
| 2771 |
) |
| 2772 |
|
| 2773 |
child = App( |
| 2774 |
name="child", |
| 2775 |
help="Child help.", |
| 2776 |
|
| 2777 |
) |
| 2778 |
|
| 2779 |
parent.command(child) |
| 2780 |
|
| 2781 |
|
| 2782 |
with console.capture() as capture: |
| 2783 |
parent(["--help"], console=console) |
| 2784 |
|
| 2785 |
actual = capture.get() |
| 2786 |
assert "Parent epilogue." in actual |
| 2787 |
|
| 2788 |
|
| 2789 |
with console.capture() as capture: |
| 2790 |
parent(["child", "--help"], console=console) |
| 2791 |
|
| 2792 |
actual = capture.get() |
| 2793 |
assert "Parent epilogue." in actual |
| 2794 |
assert "Child help." in actual |
| 2795 |
|
| 2796 |
|
| 2797 |
def test_help_epilogue_override(console): |
| 2798 |
"""Test that child can override parent's epilogue.""" |
| 2799 |
parent = App( |
| 2800 |
name="parent", |
| 2801 |
help="Parent help.", |
| 2802 |
help_epilogue="Parent epilogue.", |
| 2803 |
result_action="return_value", |
| 2804 |
) |
| 2805 |
|
| 2806 |
child = App( |
| 2807 |
name="child", |
| 2808 |
help="Child help.", |
| 2809 |
help_epilogue="Child epilogue (overrides parent).", |
| 2810 |
) |
| 2811 |
|
| 2812 |
parent.command(child) |
| 2813 |
|
| 2814 |
|
| 2815 |
with console.capture() as capture: |
| 2816 |
parent(["--help"], console=console) |
| 2817 |
|
| 2818 |
actual = capture.get() |
| 2819 |
assert "Parent epilogue." in actual |
| 2820 |
assert "Child epilogue" not in actual |
| 2821 |
|
| 2822 |
|
| 2823 |
with console.capture() as capture: |
| 2824 |
parent(["child", "--help"], console=console) |
| 2825 |
|
| 2826 |
actual = capture.get() |
| 2827 |
assert "Child epilogue (overrides parent)." in actual |
| 2828 |
assert "Parent epilogue." not in actual |
| 2829 |
|
| 2830 |
|
| 2831 |
def test_help_boolean_alias_on_positive(app, console, assert_parse_args): |
| 2832 |
"""Test that boolean flag aliases are displayed with the positive variant. |
| 2833 |
|
| 2834 |
When an alias is specified for a boolean parameter, it should be shown |
| 2835 |
alongside the positive flag (--dry-run -n), not the negative flag. |
| 2836 |
""" |
| 2837 |
|
| 2838 |
@app.default |
| 2839 |
def foo(dry_run: Annotated[bool, Parameter(alias=["-n"])] = False): |
| 2840 |
"""Test command. |
| 2841 |
|
| 2842 |
Parameters |
| 2843 |
---------- |
| 2844 |
dry_run |
| 2845 |
Enable dry run mode. |
| 2846 |
""" |
| 2847 |
pass |
| 2848 |
|
| 2849 |
|
| 2850 |
assert_parse_args(foo, "-n", dry_run=True) |
| 2851 |
|
| 2852 |
with console.capture() as capture: |
| 2853 |
app(["--help"], console=console) |
| 2854 |
|
| 2855 |
actual = capture.get() |
| 2856 |
expected = dedent( |
| 2857 |
"""\ |
| 2858 |
Usage: app [ARGS] |
| 2859 |
|
| 2860 |
App Help String Line 1. |
| 2861 |
|
| 2862 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2863 |
│ --help (-h) Display this message and exit. │ |
| 2864 |
│ --version Display application version. │ |
| 2865 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2866 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2867 |
│ DRY-RUN --dry-run -n Enable dry run mode. [default: False] │ |
| 2868 |
│ --no-dry-run │ |
| 2869 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2870 |
""" |
| 2871 |
) |
| 2872 |
assert actual == expected |
| 2873 |
|
| 2874 |
|
| 2875 |
def test_help_uppercase_short_negative_flag(app, console): |
| 2876 |
"""Uppercase short negative flags like -N should display correctly. |
| 2877 |
|
| 2878 |
https://github.com/BrianPugh/cyclopts/issues/747 |
| 2879 |
""" |
| 2880 |
|
| 2881 |
@app.default |
| 2882 |
def main( |
| 2883 |
dry_run: Annotated[ |
| 2884 |
bool, |
| 2885 |
Parameter( |
| 2886 |
name=["-n", "--dry-run"], |
| 2887 |
negative=["-N", "--no-dry-run"], |
| 2888 |
), |
| 2889 |
] = True, |
| 2890 |
): |
| 2891 |
pass |
| 2892 |
|
| 2893 |
with console.capture() as capture: |
| 2894 |
app(["--help"], console=console) |
| 2895 |
|
| 2896 |
actual = capture.get() |
| 2897 |
expected = dedent( |
| 2898 |
"""\ |
| 2899 |
Usage: app [ARGS] |
| 2900 |
|
| 2901 |
App Help String Line 1. |
| 2902 |
|
| 2903 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2904 |
│ --help (-h) Display this message and exit. │ |
| 2905 |
│ --version Display application version. │ |
| 2906 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2907 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2908 |
│ DRY-RUN --dry-run -n [default: True] │ |
| 2909 |
│ --no-dry-run -N │ |
| 2910 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2911 |
""" |
| 2912 |
) |
| 2913 |
assert actual == expected |
| 2914 |
|
| 2915 |
|
| 2916 |
def test_help_pydantic_dict_of_basemodels(app, console): |
| 2917 |
"""Help for a command whose config has a dict[str, BaseModel] field annotated with a custom help string.""" |
| 2918 |
|
| 2919 |
class SubConfig(BaseModel): |
| 2920 |
path: str = Field(description="path to model data") |
| 2921 |
|
| 2922 |
class TopConfig(BaseModel): |
| 2923 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 2924 |
|
| 2925 |
@app.default |
| 2926 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 2927 |
if cfg is None: |
| 2928 |
cfg = TopConfig() |
| 2929 |
print(cfg.model_dump_json(indent=2)) |
| 2930 |
|
| 2931 |
with console.capture() as capture: |
| 2932 |
app(["--help"], console=console) |
| 2933 |
|
| 2934 |
actual = capture.get() |
| 2935 |
expected = dedent( |
| 2936 |
"""\ |
| 2937 |
Usage: app [ARGS] |
| 2938 |
|
| 2939 |
App Help String Line 1. |
| 2940 |
|
| 2941 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2942 |
│ --help (-h) Display this message and exit. │ |
| 2943 |
│ --version Display application version. │ |
| 2944 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2945 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2946 |
│ --models.{NAME}.path path to model data │ |
| 2947 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2948 |
""" |
| 2949 |
) |
| 2950 |
assert actual == expected |
| 2951 |
|
| 2952 |
|
| 2953 |
def test_help_pydantic_req_dict_of_basemodels(app, console): |
| 2954 |
"""Help for a command whose config has a dict[str, BaseModel] field annotated with a custom help string.""" |
| 2955 |
|
| 2956 |
class SubConfig(BaseModel): |
| 2957 |
path: str = Field(description="path to model data") |
| 2958 |
|
| 2959 |
class TopConfig(BaseModel): |
| 2960 |
models: dict[str, SubConfig] |
| 2961 |
|
| 2962 |
@app.default |
| 2963 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")]): |
| 2964 |
print(cfg.model_dump_json(indent=2)) |
| 2965 |
|
| 2966 |
with console.capture() as capture: |
| 2967 |
app(["--help"], console=console) |
| 2968 |
|
| 2969 |
actual = capture.get() |
| 2970 |
expected = dedent( |
| 2971 |
"""\ |
| 2972 |
Usage: app MODELS |
| 2973 |
|
| 2974 |
App Help String Line 1. |
| 2975 |
|
| 2976 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 2977 |
│ --help (-h) Display this message and exit. │ |
| 2978 |
│ --version Display application version. │ |
| 2979 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2980 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 2981 |
│ * --models.{NAME}.path path to model data [required] │ |
| 2982 |
╰────────────────────────────────────────────────────────────────────╯ |
| 2983 |
""" |
| 2984 |
) |
| 2985 |
assert actual == expected |
| 2986 |
|
| 2987 |
|
| 2988 |
def test_help_pydantic_dict_nested_basemodel(app, console): |
| 2989 |
"""Nested BaseModel fields inside a dict value are recursively expanded.""" |
| 2990 |
|
| 2991 |
class Inner(BaseModel): |
| 2992 |
value: int = Field(description="some value") |
| 2993 |
|
| 2994 |
class SubConfig(BaseModel): |
| 2995 |
path: str = Field(description="path to data") |
| 2996 |
inner: Inner = Field(description="inner config") |
| 2997 |
|
| 2998 |
class TopConfig(BaseModel): |
| 2999 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3000 |
|
| 3001 |
@app.default |
| 3002 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3003 |
pass |
| 3004 |
|
| 3005 |
with console.capture() as capture: |
| 3006 |
app(["--help"], console=console) |
| 3007 |
|
| 3008 |
actual = capture.get() |
| 3009 |
expected = dedent( |
| 3010 |
"""\ |
| 3011 |
Usage: app [ARGS] |
| 3012 |
|
| 3013 |
App Help String Line 1. |
| 3014 |
|
| 3015 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3016 |
│ --help (-h) Display this message and exit. │ |
| 3017 |
│ --version Display application version. │ |
| 3018 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3019 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3020 |
│ --models.{NAME}.path path to data │ |
| 3021 |
│ --models.{NAME}.inner.val some value │ |
| 3022 |
│ ue │ |
| 3023 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3024 |
""" |
| 3025 |
) |
| 3026 |
assert actual == expected |
| 3027 |
|
| 3028 |
|
| 3029 |
def test_help_pydantic_dict_nested_dict_of_basemodels(app, console): |
| 3030 |
"""dict[str, BaseModel] inside a dict value's BaseModel adds another {NAME} level.""" |
| 3031 |
|
| 3032 |
class Inner(BaseModel): |
| 3033 |
score: float = Field(description="model score") |
| 3034 |
|
| 3035 |
class SubConfig(BaseModel): |
| 3036 |
path: str = Field(description="path to data") |
| 3037 |
children: dict[str, Inner] = Field(default_factory=dict, description="child models") |
| 3038 |
|
| 3039 |
class TopConfig(BaseModel): |
| 3040 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3041 |
|
| 3042 |
@app.default |
| 3043 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3044 |
pass |
| 3045 |
|
| 3046 |
with console.capture() as capture: |
| 3047 |
app(["--help"], console=console) |
| 3048 |
|
| 3049 |
actual = capture.get() |
| 3050 |
expected = dedent( |
| 3051 |
"""\ |
| 3052 |
Usage: app [ARGS] |
| 3053 |
|
| 3054 |
App Help String Line 1. |
| 3055 |
|
| 3056 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3057 |
│ --help (-h) Display this message and exit. │ |
| 3058 |
│ --version Display application version. │ |
| 3059 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3060 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3061 |
│ --models.{NAME}.path path to data │ |
| 3062 |
│ --models.{NAME}.children. model score │ |
| 3063 |
│ {NAME}.score │ |
| 3064 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3065 |
""" |
| 3066 |
) |
| 3067 |
assert actual == expected |
| 3068 |
|
| 3069 |
|
| 3070 |
def test_help_pydantic_dict_optional_basemodel_field(app, console): |
| 3071 |
"""Optional[BaseModel] fields are unwrapped and recursively expanded.""" |
| 3072 |
|
| 3073 |
class Inner(BaseModel): |
| 3074 |
value: int = Field(description="inner value") |
| 3075 |
|
| 3076 |
class SubConfig(BaseModel): |
| 3077 |
path: str = Field(description="path to data") |
| 3078 |
extra: Optional[Inner] = Field(default=None, description="optional inner") |
| 3079 |
|
| 3080 |
class TopConfig(BaseModel): |
| 3081 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3082 |
|
| 3083 |
@app.default |
| 3084 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3085 |
pass |
| 3086 |
|
| 3087 |
with console.capture() as capture: |
| 3088 |
app(["--help"], console=console) |
| 3089 |
|
| 3090 |
actual = capture.get() |
| 3091 |
expected = dedent( |
| 3092 |
"""\ |
| 3093 |
Usage: app [ARGS] |
| 3094 |
|
| 3095 |
App Help String Line 1. |
| 3096 |
|
| 3097 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3098 |
│ --help (-h) Display this message and exit. │ |
| 3099 |
│ --version Display application version. │ |
| 3100 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3101 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3102 |
│ --models.{NAME}.path path to data │ |
| 3103 |
│ --models.{NAME}.extra.val inner value │ |
| 3104 |
│ ue │ |
| 3105 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3106 |
""" |
| 3107 |
) |
| 3108 |
assert actual == expected |
| 3109 |
|
| 3110 |
|
| 3111 |
def test_help_pydantic_dict_list_basemodel_is_leaf(app, console): |
| 3112 |
"""list[BaseModel] fields are shown as leaves, not recursively expanded.""" |
| 3113 |
|
| 3114 |
class Inner(BaseModel): |
| 3115 |
value: int = Field(description="item value") |
| 3116 |
|
| 3117 |
class SubConfig(BaseModel): |
| 3118 |
path: str = Field(description="path to data") |
| 3119 |
items: list[Inner] = Field(default_factory=list, description="list of items") |
| 3120 |
|
| 3121 |
class TopConfig(BaseModel): |
| 3122 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3123 |
|
| 3124 |
@app.default |
| 3125 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3126 |
pass |
| 3127 |
|
| 3128 |
with console.capture() as capture: |
| 3129 |
app(["--help"], console=console) |
| 3130 |
|
| 3131 |
actual = capture.get() |
| 3132 |
expected = dedent( |
| 3133 |
"""\ |
| 3134 |
Usage: app [ARGS] |
| 3135 |
|
| 3136 |
App Help String Line 1. |
| 3137 |
|
| 3138 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3139 |
│ --help (-h) Display this message and exit. │ |
| 3140 |
│ --version Display application version. │ |
| 3141 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3142 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3143 |
│ --models.{NAME}.path path to data │ |
| 3144 |
│ --models.{NAME}.items --m list of items │ |
| 3145 |
│ odels.{NAME}.empty-item │ |
| 3146 |
│ s │ |
| 3147 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3148 |
""" |
| 3149 |
) |
| 3150 |
assert actual == expected |
| 3151 |
|
| 3152 |
|
| 3153 |
def test_help_pydantic_dict_circular_reference(app, console): |
| 3154 |
"""Circular model references terminate without infinite recursion. |
| 3155 |
|
| 3156 |
The self-referencing ``children`` field should still appear as a leaf |
| 3157 |
entry (with its description) rather than being silently dropped. |
| 3158 |
""" |
| 3159 |
|
| 3160 |
class Node(BaseModel): |
| 3161 |
label: str = Field(description="node label") |
| 3162 |
children: dict[str, "Node"] = Field(default_factory=dict, description="child nodes") |
| 3163 |
|
| 3164 |
Node.model_rebuild() |
| 3165 |
|
| 3166 |
class TopConfig(BaseModel): |
| 3167 |
root: dict[str, Node] = Field(default_factory=dict) |
| 3168 |
|
| 3169 |
@app.default |
| 3170 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3171 |
pass |
| 3172 |
|
| 3173 |
with console.capture() as capture: |
| 3174 |
app(["--help"], console=console) |
| 3175 |
|
| 3176 |
actual = capture.get() |
| 3177 |
expected = dedent( |
| 3178 |
"""\ |
| 3179 |
Usage: app [ARGS] |
| 3180 |
|
| 3181 |
App Help String Line 1. |
| 3182 |
|
| 3183 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3184 |
│ --help (-h) Display this message and exit. │ |
| 3185 |
│ --version Display application version. │ |
| 3186 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3187 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3188 |
│ --root.{NAME}.label node label │ |
| 3189 |
│ --root.{NAME}.children.{N child nodes │ |
| 3190 |
│ AME} │ |
| 3191 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3192 |
""" |
| 3193 |
) |
| 3194 |
assert actual == expected |
| 3195 |
|
| 3196 |
|
| 3197 |
def test_help_dict_of_dataclass(app, console): |
| 3198 |
"""dict[str, dataclass] fields are expanded with defaults rendered.""" |
| 3199 |
|
| 3200 |
@dataclass |
| 3201 |
class SubConfig: |
| 3202 |
path: str = "/tmp" |
| 3203 |
count: int = 3 |
| 3204 |
|
| 3205 |
class TopConfig(BaseModel): |
| 3206 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3207 |
|
| 3208 |
@app.default |
| 3209 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3210 |
pass |
| 3211 |
|
| 3212 |
with console.capture() as capture: |
| 3213 |
app(["--help"], console=console) |
| 3214 |
|
| 3215 |
actual = capture.get() |
| 3216 |
expected = dedent( |
| 3217 |
"""\ |
| 3218 |
Usage: app [ARGS] |
| 3219 |
|
| 3220 |
App Help String Line 1. |
| 3221 |
|
| 3222 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3223 |
│ --help (-h) Display this message and exit. │ |
| 3224 |
│ --version Display application version. │ |
| 3225 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3226 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3227 |
│ --models.{NAME}.path [default: /tmp] │ |
| 3228 |
│ --models.{NAME}.count [default: 3] │ |
| 3229 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3230 |
""" |
| 3231 |
) |
| 3232 |
assert actual == expected |
| 3233 |
|
| 3234 |
|
| 3235 |
def test_help_dict_of_attrs(app, console): |
| 3236 |
"""dict[str, attrs-class] fields are expanded with defaults rendered.""" |
| 3237 |
|
| 3238 |
@attrs.define |
| 3239 |
class SubConfig: |
| 3240 |
path: str = "/tmp" |
| 3241 |
|
| 3242 |
@app.default |
| 3243 |
def main(models: dict[str, SubConfig] | None = None): |
| 3244 |
pass |
| 3245 |
|
| 3246 |
with console.capture() as capture: |
| 3247 |
app(["--help"], console=console) |
| 3248 |
|
| 3249 |
actual = capture.get() |
| 3250 |
expected = dedent( |
| 3251 |
"""\ |
| 3252 |
Usage: app [ARGS] |
| 3253 |
|
| 3254 |
App Help String Line 1. |
| 3255 |
|
| 3256 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3257 |
│ --help (-h) Display this message and exit. │ |
| 3258 |
│ --version Display application version. │ |
| 3259 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3260 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3261 |
│ --models.{NAME}.path [default: /tmp] │ |
| 3262 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3263 |
""" |
| 3264 |
) |
| 3265 |
assert actual == expected |
| 3266 |
|
| 3267 |
|
| 3268 |
def test_help_dict_of_typeddict(app, console): |
| 3269 |
"""dict[str, TypedDict] fields are expanded.""" |
| 3270 |
|
| 3271 |
class SubConfig(TypedDict): |
| 3272 |
path: str |
| 3273 |
count: int |
| 3274 |
|
| 3275 |
@app.default |
| 3276 |
def main(models: dict[str, SubConfig] | None = None): |
| 3277 |
pass |
| 3278 |
|
| 3279 |
with console.capture() as capture: |
| 3280 |
app(["--help"], console=console) |
| 3281 |
|
| 3282 |
actual = capture.get() |
| 3283 |
expected = dedent( |
| 3284 |
"""\ |
| 3285 |
Usage: app [ARGS] |
| 3286 |
|
| 3287 |
App Help String Line 1. |
| 3288 |
|
| 3289 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3290 |
│ --help (-h) Display this message and exit. │ |
| 3291 |
│ --version Display application version. │ |
| 3292 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3293 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3294 |
│ --models.{NAME}.path │ |
| 3295 |
│ --models.{NAME}.count │ |
| 3296 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3297 |
""" |
| 3298 |
) |
| 3299 |
assert actual == expected |
| 3300 |
|
| 3301 |
|
| 3302 |
def test_help_pydantic_dict_literal_choices(app, console): |
| 3303 |
"""Literal fields inside a dict-valued BaseModel render [choices: ...].""" |
| 3304 |
|
| 3305 |
class SubConfig(BaseModel): |
| 3306 |
level: Literal["low", "med", "high"] = Field("low", description="verbosity level") |
| 3307 |
|
| 3308 |
class TopConfig(BaseModel): |
| 3309 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3310 |
|
| 3311 |
@app.default |
| 3312 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3313 |
pass |
| 3314 |
|
| 3315 |
with console.capture() as capture: |
| 3316 |
app(["--help"], console=console) |
| 3317 |
|
| 3318 |
actual = capture.get() |
| 3319 |
expected = dedent( |
| 3320 |
"""\ |
| 3321 |
Usage: app [ARGS] |
| 3322 |
|
| 3323 |
App Help String Line 1. |
| 3324 |
|
| 3325 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3326 |
│ --help (-h) Display this message and exit. │ |
| 3327 |
│ --version Display application version. │ |
| 3328 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3329 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3330 |
│ --models.{NAME}.level verbosity level [choices: low, med, high] │ |
| 3331 |
│ [default: low] │ |
| 3332 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3333 |
""" |
| 3334 |
) |
| 3335 |
assert actual == expected |
| 3336 |
|
| 3337 |
|
| 3338 |
def test_help_pydantic_dict_required_propagation(app, console): |
| 3339 |
"""An outer required dict + inner defaulted field → inner is NOT marked required.""" |
| 3340 |
|
| 3341 |
class SubConfig(BaseModel): |
| 3342 |
req_path: str = Field(description="required") |
| 3343 |
opt_path: str = Field("/tmp", description="defaulted") |
| 3344 |
|
| 3345 |
class TopConfig(BaseModel): |
| 3346 |
models: dict[str, SubConfig] |
| 3347 |
|
| 3348 |
@app.default |
| 3349 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")]): |
| 3350 |
pass |
| 3351 |
|
| 3352 |
with console.capture() as capture: |
| 3353 |
app(["--help"], console=console) |
| 3354 |
|
| 3355 |
actual = capture.get() |
| 3356 |
expected = dedent( |
| 3357 |
"""\ |
| 3358 |
Usage: app MODELS |
| 3359 |
|
| 3360 |
App Help String Line 1. |
| 3361 |
|
| 3362 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3363 |
│ --help (-h) Display this message and exit. │ |
| 3364 |
│ --version Display application version. │ |
| 3365 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3366 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3367 |
│ * --models.{NAME}.req-path required [required] │ |
| 3368 |
│ --models.{NAME}.opt-path defaulted [default: /tmp] │ |
| 3369 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3370 |
""" |
| 3371 |
) |
| 3372 |
assert actual == expected |
| 3373 |
|
| 3374 |
|
| 3375 |
def test_help_pydantic_dict_parameter_help_precedence(app, console): |
| 3376 |
"""Parameter(help=...) on a nested field overrides pydantic Field(description=...).""" |
| 3377 |
|
| 3378 |
class SubConfig(BaseModel): |
| 3379 |
path: Annotated[str, Parameter(help="cyclopts help")] = Field(description="pydantic description") |
| 3380 |
|
| 3381 |
class TopConfig(BaseModel): |
| 3382 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3383 |
|
| 3384 |
@app.default |
| 3385 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3386 |
pass |
| 3387 |
|
| 3388 |
with console.capture() as capture: |
| 3389 |
app(["--help"], console=console) |
| 3390 |
|
| 3391 |
actual = capture.get() |
| 3392 |
expected = dedent( |
| 3393 |
"""\ |
| 3394 |
Usage: app [ARGS] |
| 3395 |
|
| 3396 |
App Help String Line 1. |
| 3397 |
|
| 3398 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3399 |
│ --help (-h) Display this message and exit. │ |
| 3400 |
│ --version Display application version. │ |
| 3401 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3402 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3403 |
│ --models.{NAME}.path cyclopts help │ |
| 3404 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3405 |
""" |
| 3406 |
) |
| 3407 |
assert actual == expected |
| 3408 |
|
| 3409 |
|
| 3410 |
def test_help_pydantic_dict_name_transform_kebab(app, console): |
| 3411 |
"""snake_case pydantic field names are rendered as kebab-case in help.""" |
| 3412 |
|
| 3413 |
class SubConfig(BaseModel): |
| 3414 |
my_field: str = Field(description="a snake field") |
| 3415 |
|
| 3416 |
class TopConfig(BaseModel): |
| 3417 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3418 |
|
| 3419 |
@app.default |
| 3420 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3421 |
pass |
| 3422 |
|
| 3423 |
with console.capture() as capture: |
| 3424 |
app(["--help"], console=console) |
| 3425 |
|
| 3426 |
actual = capture.get() |
| 3427 |
expected = dedent( |
| 3428 |
"""\ |
| 3429 |
Usage: app [ARGS] |
| 3430 |
|
| 3431 |
App Help String Line 1. |
| 3432 |
|
| 3433 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 3434 |
│ --help (-h) Display this message and exit. │ |
| 3435 |
│ --version Display application version. │ |
| 3436 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3437 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 3438 |
│ --models.{NAME}.my-field a snake field │ |
| 3439 |
╰────────────────────────────────────────────────────────────────────╯ |
| 3440 |
""" |
| 3441 |
) |
| 3442 |
assert actual == expected |
| 3443 |
|
| 3444 |
|
| 3445 |
def test_parse_pydantic_dict_nested_kebab_name(app): |
| 3446 |
"""The CLI accepts the kebab-case form that help advertises for dict-nested fields.""" |
| 3447 |
|
| 3448 |
class SubConfig(BaseModel): |
| 3449 |
my_field: str = Field(description="snake_case field") |
| 3450 |
|
| 3451 |
class TopConfig(BaseModel): |
| 3452 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3453 |
|
| 3454 |
@app.default |
| 3455 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3456 |
if cfg is None: |
| 3457 |
cfg = TopConfig() |
| 3458 |
return cfg.model_dump() |
| 3459 |
|
| 3460 |
result = app(["--models.foo.my-field", "hello"], exit_on_error=False) |
| 3461 |
assert result == {"models": {"foo": {"my_field": "hello"}}} |
| 3462 |
|
| 3463 |
|
| 3464 |
def test_parse_pydantic_dict_nested_snake_still_accepted(app): |
| 3465 |
"""Backward-compat: raw snake_case names still parse for dict-nested fields.""" |
| 3466 |
|
| 3467 |
class SubConfig(BaseModel): |
| 3468 |
my_field: str = Field(description="snake_case field") |
| 3469 |
|
| 3470 |
class TopConfig(BaseModel): |
| 3471 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3472 |
|
| 3473 |
@app.default |
| 3474 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3475 |
if cfg is None: |
| 3476 |
cfg = TopConfig() |
| 3477 |
return cfg.model_dump() |
| 3478 |
|
| 3479 |
result = app(["--models.foo.my_field", "hello"], exit_on_error=False) |
| 3480 |
assert result == {"models": {"foo": {"my_field": "hello"}}} |
| 3481 |
|
| 3482 |
|
| 3483 |
def test_parse_dict_of_dict_of_basemodel_kebab(app): |
| 3484 |
"""Double-dynamic: dict[str, dict[str, SubConfig]] — first two segments are dict keys.""" |
| 3485 |
|
| 3486 |
class SubConfig(BaseModel): |
| 3487 |
my_field: str |
| 3488 |
|
| 3489 |
class TopConfig(BaseModel): |
| 3490 |
groups: dict[str, dict[str, SubConfig]] = Field(default_factory=dict) |
| 3491 |
|
| 3492 |
@app.default |
| 3493 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3494 |
if cfg is None: |
| 3495 |
cfg = TopConfig() |
| 3496 |
return cfg.model_dump() |
| 3497 |
|
| 3498 |
result = app(["--groups.a.b.my-field", "hello"], exit_on_error=False) |
| 3499 |
assert result == {"groups": {"a": {"b": {"my_field": "hello"}}}} |
| 3500 |
|
| 3501 |
|
| 3502 |
def test_parse_pydantic_dict_nested_struct_in_struct_kebab(app): |
| 3503 |
"""Nested struct inside a dict value: dict[str, SubConfig] where SubConfig has inner: Inner.""" |
| 3504 |
|
| 3505 |
class Inner(BaseModel): |
| 3506 |
nested_field: str |
| 3507 |
|
| 3508 |
class SubConfig(BaseModel): |
| 3509 |
inner: Inner |
| 3510 |
|
| 3511 |
class TopConfig(BaseModel): |
| 3512 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3513 |
|
| 3514 |
@app.default |
| 3515 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3516 |
if cfg is None: |
| 3517 |
cfg = TopConfig() |
| 3518 |
return cfg.model_dump() |
| 3519 |
|
| 3520 |
result = app(["--models.foo.inner.nested-field", "hello"], exit_on_error=False) |
| 3521 |
assert result == {"models": {"foo": {"inner": {"nested_field": "hello"}}}} |
| 3522 |
|
| 3523 |
|
| 3524 |
def test_parse_pydantic_dict_nested_alias(app): |
| 3525 |
"""Pydantic aliases: both the alias and the Python name are accepted.""" |
| 3526 |
|
| 3527 |
class SubConfig(BaseModel): |
| 3528 |
model_config = {"populate_by_name": True} |
| 3529 |
my_field: str = Field(alias="my_alias") |
| 3530 |
|
| 3531 |
class TopConfig(BaseModel): |
| 3532 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3533 |
|
| 3534 |
@app.default |
| 3535 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3536 |
if cfg is None: |
| 3537 |
cfg = TopConfig() |
| 3538 |
return cfg.model_dump(by_alias=True) |
| 3539 |
|
| 3540 |
|
| 3541 |
result = app(["--models.foo.my-alias", "via-alias"], exit_on_error=False) |
| 3542 |
assert result == {"models": {"foo": {"my_alias": "via-alias"}}} |
| 3543 |
|
| 3544 |
|
| 3545 |
def test_parse_pydantic_dict_nested_forward_ref_passthrough(app): |
| 3546 |
"""Unresolved forward-ref in dict value: segments pass through unchanged.""" |
| 3547 |
|
| 3548 |
class Node(BaseModel): |
| 3549 |
label: str |
| 3550 |
children: dict[str, "Node"] = Field(default_factory=dict) |
| 3551 |
|
| 3552 |
Node.model_rebuild() |
| 3553 |
|
| 3554 |
class TopConfig(BaseModel): |
| 3555 |
root: dict[str, Node] = Field(default_factory=dict) |
| 3556 |
|
| 3557 |
@app.default |
| 3558 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3559 |
if cfg is None: |
| 3560 |
cfg = TopConfig() |
| 3561 |
return cfg.model_dump() |
| 3562 |
|
| 3563 |
|
| 3564 |
result = app(["--root.a.label", "top", "--root.a.children.b.label", "child"], exit_on_error=False) |
| 3565 |
assert result == { |
| 3566 |
"root": { |
| 3567 |
"a": { |
| 3568 |
"label": "top", |
| 3569 |
"children": {"b": {"label": "child", "children": {}}}, |
| 3570 |
} |
| 3571 |
} |
| 3572 |
} |
| 3573 |
|
| 3574 |
|
| 3575 |
def test_parse_dict_key_preserved_when_matching_kebab_field_name(app): |
| 3576 |
"""A dict key that literally matches a kebab field name must pass through |
| 3577 |
unchanged (not get canonicalized to the field's python name). |
| 3578 |
""" |
| 3579 |
|
| 3580 |
class SubConfig(BaseModel): |
| 3581 |
my_field: str |
| 3582 |
|
| 3583 |
class TopConfig(BaseModel): |
| 3584 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3585 |
|
| 3586 |
@app.default |
| 3587 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3588 |
if cfg is None: |
| 3589 |
cfg = TopConfig() |
| 3590 |
return cfg.model_dump() |
| 3591 |
|
| 3592 |
|
| 3593 |
|
| 3594 |
result = app(["--models.my-field.my-field", "hello"], exit_on_error=False) |
| 3595 |
assert result == {"models": {"my-field": {"my_field": "hello"}}} |
| 3596 |
|
| 3597 |
|
| 3598 |
def test_parse_dict_key_preserved_with_hyphens_and_underscores(app): |
| 3599 |
"""Arbitrary characters in dict keys (hyphens, underscores) pass through unchanged.""" |
| 3600 |
|
| 3601 |
class SubConfig(BaseModel): |
| 3602 |
my_field: str |
| 3603 |
|
| 3604 |
class TopConfig(BaseModel): |
| 3605 |
models: dict[str, SubConfig] = Field(default_factory=dict) |
| 3606 |
|
| 3607 |
@app.default |
| 3608 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3609 |
if cfg is None: |
| 3610 |
cfg = TopConfig() |
| 3611 |
return cfg.model_dump() |
| 3612 |
|
| 3613 |
r1 = app(["--models.some-key.my-field", "a"], exit_on_error=False) |
| 3614 |
assert r1 == {"models": {"some-key": {"my_field": "a"}}} |
| 3615 |
|
| 3616 |
r2 = app(["--models.some_key.my-field", "b"], exit_on_error=False) |
| 3617 |
assert r2 == {"models": {"some_key": {"my_field": "b"}}} |
| 3618 |
|
| 3619 |
|
| 3620 |
def test_parse_double_dynamic_dict_keys_preserved(app): |
| 3621 |
"""``dict[str, dict[str, SubConfig]]`` — both outer and inner dict keys pass |
| 3622 |
through unchanged, even if they look like kebab'd field names. |
| 3623 |
""" |
| 3624 |
|
| 3625 |
class SubConfig(BaseModel): |
| 3626 |
my_field: str |
| 3627 |
|
| 3628 |
class TopConfig(BaseModel): |
| 3629 |
groups: dict[str, dict[str, SubConfig]] = Field(default_factory=dict) |
| 3630 |
|
| 3631 |
@app.default |
| 3632 |
def main(cfg: Annotated[TopConfig, Parameter(name="*")] | None = None): |
| 3633 |
if cfg is None: |
| 3634 |
cfg = TopConfig() |
| 3635 |
return cfg.model_dump() |
| 3636 |
|
| 3637 |
|
| 3638 |
result = app(["--groups.my-field.my-field.my-field", "hello"], exit_on_error=False) |
| 3639 |
assert result == {"groups": {"my-field": {"my-field": {"my_field": "hello"}}}} |
| 3640 |
|
| 3641 |
|
| 3642 |
def test_normalize_trailing_keys_var_keyword_seed(): |
| 3643 |
"""``_normalize_trailing_keys`` seeds from ``self.hint`` so VAR_KEYWORD args |
| 3644 |
(whose ``field_info.annotation`` is the value type but whose ``hint`` is the |
| 3645 |
wrapping ``dict[str, ValueType]``) route the first segment as a dict key. |
| 3646 |
""" |
| 3647 |
|
| 3648 |
class SubConfig(BaseModel): |
| 3649 |
my_field: str |
| 3650 |
|
| 3651 |
def main(**opts: SubConfig): |
| 3652 |
pass |
| 3653 |
|
| 3654 |
coll = ArgumentCollection._from_callable(main) |
| 3655 |
kw_arg = next(a for a in coll if a.field_info.kind == a.field_info.VAR_KEYWORD) |
| 3656 |
|
| 3657 |
assert kw_arg._normalize_trailing_keys(("foo", "my-field")) == ("foo", "my_field") |
| 3658 |
assert kw_arg._normalize_trailing_keys(("foo", "my_field")) == ("foo", "my_field") |
| 3659 |
h. |
| 3660 |
assert kw_arg._normalize_trailing_keys(("my-field",)) == ("my-field",) |