MCPcopy Create free account
hub / github.com/pydantic/pydantic-settings / test_cli_subcommand_with_positionals

Function test_cli_subcommand_with_positionals

tests/test_source_cli.py:1397–1488  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1395
1396
1397def 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'])

Callers

nothing calls this directly

Calls 5

get_subcommandFunction · 0.90
BarPluginClass · 0.85
NotModelClass · 0.85
runMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…