Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI subcommand union and alias support #380

Merged
Prev Previous commit
Next Next commit
Py 3.8 union fixes.
  • Loading branch information
kschwab committed Sep 3, 2024
commit 700dbb2a569ce2b897c2ab9cf7256fad6b6e226d
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ positional arguments, aliasing will change the CLI help text displayed for the f

```py
import sys
from typing import Union

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -860,7 +861,7 @@ class Gamma(BaseModel):


class Root(BaseSettings, cli_parse_args=True, cli_exit_on_error=False):
subcommand: CliSubCommand[Alpha | Beta] = Field(alias='sub-command')
subcommand: CliSubCommand[Union[Alpha, Beta]] = Field(alias='sub-command')
gamma: CliSubCommand[Gamma] = Field(alias='gamma-cmd')


Expand Down
6 changes: 3 additions & 3 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3166,7 +3166,7 @@ class GammaCmd(BaseModel):
class Root1(BaseSettings):
"""Root Help"""

subcommand: CliSubCommand[AlphaCmd | BetaCmd | GammaCmd] = Field(description='Field Help')
subcommand: CliSubCommand[Union[AlphaCmd, BetaCmd, GammaCmd]] = Field(description='Field Help')

alpha = Root1(_cli_parse_args=['AlphaCmd', '-a=alpha'])
assert get_subcommand(alpha).model_dump() == {'a': 'alpha'}
Expand Down Expand Up @@ -3226,7 +3226,7 @@ class Root1(BaseSettings):
class Root2(BaseSettings):
"""Root Help"""

subcommand: CliSubCommand[AlphaCmd | GammaCmd] = Field(description='Field Help')
subcommand: CliSubCommand[Union[AlphaCmd, GammaCmd]] = Field(description='Field Help')
beta: CliSubCommand[BetaCmd] = Field(description='Field Beta Help')

alpha = Root2(_cli_parse_args=['AlphaCmd', '-a=alpha'])
Expand Down Expand Up @@ -3288,7 +3288,7 @@ class Root3(BaseSettings):
"""Root Help"""

beta: CliSubCommand[BetaCmd] = Field(description='Field Beta Help')
subcommand: CliSubCommand[AlphaCmd | GammaCmd] = Field(description='Field Help')
subcommand: CliSubCommand[Union[AlphaCmd, GammaCmd]] = Field(description='Field Help')

alpha = Root3(_cli_parse_args=['AlphaCmd', '-a=alpha'])
assert get_subcommand(alpha).model_dump() == {'a': 'alpha'}
Expand Down
Loading