|
1 |
| - |
2 | 1 | ## Installation
|
3 | 2 |
|
4 | 3 | Installation is as simple as:
|
@@ -1172,7 +1171,7 @@ CliApp.run(Git, cli_args=['clone', 'repo', 'dir']).model_dump() == {
|
1172 | 1171 | }
|
1173 | 1172 | ```
|
1174 | 1173 |
|
1175 |
| -When executing a subcommand with an asynchronous cli_cmd, Pydantic settings automatically detects whether the current thread already has an active event loop. If so, the async command is run in a fresh thread to avoid conflicts. Otherwise, it uses asyncio.run() in the current thread. This handling ensures your asynchronous subcommands “just work” without additional manual setup. |
| 1174 | +When executing a subcommand with an asynchronous cli_cmd, Pydantic settings automatically detects whether the current thread already has an active event loop. If so, the async command is run in a fresh thread to avoid conflicts. Otherwise, it uses asyncio.run() in the current thread. This handling ensures your asynchronous subcommands "just work" without additional manual setup. |
1176 | 1175 |
|
1177 | 1176 | ### Mutually Exclusive Groups
|
1178 | 1177 |
|
@@ -1633,6 +1632,62 @@ options:
|
1633 | 1632 | """
|
1634 | 1633 | ```
|
1635 | 1634 |
|
| 1635 | +#### CLI Shortcuts for Arguments |
| 1636 | + |
| 1637 | +Add alternative CLI argument names (shortcuts) for fields using the `cli_shortcuts` option in `SettingsConfigDict`. This allows you to define additional names for CLI arguments, which can be especially useful for providing more user-friendly or shorter aliases for deeply nested or verbose field names. |
| 1638 | + |
| 1639 | +The `cli_shortcuts` option takes a dictionary mapping the target field name (using dot notation for nested fields) to one or more shortcut names. If multiple fields share the same shortcut, the first matching field will take precedence. |
| 1640 | + |
| 1641 | +**Flat Example:** |
| 1642 | + |
| 1643 | +```py |
| 1644 | +from pydantic import Field |
| 1645 | + |
| 1646 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 1647 | + |
| 1648 | + |
| 1649 | +class Settings(BaseSettings): |
| 1650 | + option: str = Field(default='foo') |
| 1651 | + list_option: str = Field(default='fizz') |
| 1652 | + |
| 1653 | + model_config = SettingsConfigDict(cli_shortcuts={'option': 'option2', 'list_option': ['list_option2']}) |
| 1654 | + |
| 1655 | + |
| 1656 | +# Now you can use the shortcuts on the CLI: |
| 1657 | +# --option2 sets 'option', --list_option2 sets 'list_option' |
| 1658 | +``` |
| 1659 | + |
| 1660 | +**Nested Example:** |
| 1661 | + |
| 1662 | +```py |
| 1663 | +from pydantic import BaseModel, Field |
| 1664 | + |
| 1665 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 1666 | + |
| 1667 | + |
| 1668 | +class TwiceNested(BaseModel): |
| 1669 | + option: str = Field(default='foo') |
| 1670 | + |
| 1671 | + |
| 1672 | +class Nested(BaseModel): |
| 1673 | + twice_nested_option: TwiceNested = TwiceNested() |
| 1674 | + option: str = Field(default='foo') |
| 1675 | + |
| 1676 | + |
| 1677 | +class Settings(BaseSettings): |
| 1678 | + nested: Nested = Nested() |
| 1679 | + model_config = SettingsConfigDict( |
| 1680 | + cli_shortcuts={'nested.option': 'option2', 'nested.twice_nested_option.option': 'twice_nested_option'} |
| 1681 | + ) |
| 1682 | + |
| 1683 | + |
| 1684 | +# Now you can use --option2 to set nested.option and --twice_nested_option to set nested.twice_nested_option.option |
| 1685 | +``` |
| 1686 | + |
| 1687 | +If a shortcut collides (is mapped to multiple fields), it will apply to the first matching field in the model. |
| 1688 | + |
| 1689 | +See the [test cases](../tests/test_source_cli.py) for more advanced usage and edge cases. |
| 1690 | + |
1636 | 1691 | ### Integrating with Existing Parsers
|
1637 | 1692 |
|
1638 | 1693 | A CLI settings source can be integrated with existing parsers by overriding the default CLI settings source with a user
|
|
0 commit comments