()
| 1395 | |
| 1396 | |
| 1397 | def test_cli_subcommand_with_positionals(): |
| 1398 | @pydantic_dataclasses.dataclass |
| 1399 | class FooPlugin: |
| 1400 | my_feature: bool = False |
| 1401 | |
| 1402 | @pydantic_dataclasses.dataclass |
| 1403 | class BarPlugin: |
| 1404 | my_feature: bool = False |
| 1405 | |
| 1406 | bar = BarPlugin() |
| 1407 | with pytest.raises(SystemExit, match='Error: CLI subcommand is required but no subcommands were found.'): |
| 1408 | get_subcommand(bar) |
| 1409 | with pytest.raises(SettingsError, match='Error: CLI subcommand is required but no subcommands were found.'): |
| 1410 | get_subcommand(bar, cli_exit_on_error=False) |
| 1411 | |
| 1412 | @pydantic_dataclasses.dataclass |
| 1413 | class Plugins: |
| 1414 | foo: CliSubCommand[FooPlugin] |
| 1415 | bar: CliSubCommand[BarPlugin] |
| 1416 | |
| 1417 | class Clone(BaseModel): |
| 1418 | repository: CliPositionalArg[str] |
| 1419 | directory: CliPositionalArg[str] |
| 1420 | local: bool = False |
| 1421 | shared: bool = False |
| 1422 | |
| 1423 | class Init(BaseModel): |
| 1424 | directory: CliPositionalArg[str] |
| 1425 | quiet: bool = False |
| 1426 | bare: bool = False |
| 1427 | |
| 1428 | class Git(BaseSettings): |
| 1429 | clone: CliSubCommand[Clone] |
| 1430 | init: CliSubCommand[Init] |
| 1431 | plugins: CliSubCommand[Plugins] |
| 1432 | |
| 1433 | git = CliApp.run(Git, cli_args=[]) |
| 1434 | assert git.model_dump() == { |
| 1435 | 'clone': None, |
| 1436 | 'init': None, |
| 1437 | 'plugins': None, |
| 1438 | } |
| 1439 | assert get_subcommand(git, is_required=False) is None |
| 1440 | with pytest.raises(SystemExit, match='Error: CLI subcommand is required {clone, init, plugins}'): |
| 1441 | get_subcommand(git) |
| 1442 | with pytest.raises(SettingsError, match='Error: CLI subcommand is required {clone, init, plugins}'): |
| 1443 | get_subcommand(git, cli_exit_on_error=False) |
| 1444 | |
| 1445 | git = CliApp.run(Git, cli_args=['init', '--quiet', 'true', 'dir/path']) |
| 1446 | assert git.model_dump() == { |
| 1447 | 'clone': None, |
| 1448 | 'init': {'directory': 'dir/path', 'quiet': True, 'bare': False}, |
| 1449 | 'plugins': None, |
| 1450 | } |
| 1451 | assert get_subcommand(git) == git.init |
| 1452 | assert get_subcommand(git, is_required=False) == git.init |
| 1453 | |
| 1454 | git = CliApp.run(Git, cli_args=['clone', 'repo', '.', '--shared', 'true']) |
nothing calls this directly
no test coverage detected
searching dependent graphs…