(capsys, monkeypatch)
| 1200 | |
| 1201 | |
| 1202 | def test_cli_subcommand_union(capsys, monkeypatch): |
| 1203 | class AlphaCmd(BaseModel): |
| 1204 | """Alpha Help""" |
| 1205 | |
| 1206 | a: str |
| 1207 | |
| 1208 | class BetaCmd(BaseModel): |
| 1209 | """Beta Help""" |
| 1210 | |
| 1211 | b: str |
| 1212 | |
| 1213 | class GammaCmd(BaseModel): |
| 1214 | """Gamma Help""" |
| 1215 | |
| 1216 | g: str |
| 1217 | |
| 1218 | class Root1(BaseSettings, cli_prog_name='example.py'): |
| 1219 | """Root Help""" |
| 1220 | |
| 1221 | subcommand: CliSubCommand[AlphaCmd | BetaCmd | GammaCmd] = Field(description='Field Help') |
| 1222 | |
| 1223 | alpha = CliApp.run(Root1, cli_args=['AlphaCmd', '-a=alpha']) |
| 1224 | assert get_subcommand(alpha).model_dump() == {'a': 'alpha'} |
| 1225 | assert alpha.model_dump() == {'subcommand': {'a': 'alpha'}} |
| 1226 | beta = CliApp.run(Root1, cli_args=['BetaCmd', '-b=beta']) |
| 1227 | assert get_subcommand(beta).model_dump() == {'b': 'beta'} |
| 1228 | assert beta.model_dump() == {'subcommand': {'b': 'beta'}} |
| 1229 | gamma = CliApp.run(Root1, cli_args=['GammaCmd', '-g=gamma']) |
| 1230 | assert get_subcommand(gamma).model_dump() == {'g': 'gamma'} |
| 1231 | assert gamma.model_dump() == {'subcommand': {'g': 'gamma'}} |
| 1232 | |
| 1233 | with monkeypatch.context() as m: |
| 1234 | m.setattr(sys, 'argv', ['example.py', '--help']) |
| 1235 | |
| 1236 | with pytest.raises(SystemExit): |
| 1237 | CliApp.run(Root1) |
| 1238 | assert ( |
| 1239 | capsys.readouterr().out |
| 1240 | == f"""usage: example.py [-h] {{AlphaCmd,BetaCmd,GammaCmd}} ... |
| 1241 | |
| 1242 | Root Help |
| 1243 | |
| 1244 | {ARGPARSE_OPTIONS_TEXT}: |
| 1245 | -h, --help show this help message and exit |
| 1246 | |
| 1247 | subcommands: |
| 1248 | Field Help |
| 1249 | |
| 1250 | {{AlphaCmd,BetaCmd,GammaCmd}} |
| 1251 | AlphaCmd |
| 1252 | BetaCmd |
| 1253 | GammaCmd |
| 1254 | """ |
| 1255 | ) |
| 1256 | |
| 1257 | with pytest.raises(SystemExit): |
| 1258 | Root1(_cli_parse_args=True, _cli_use_class_docs_for_groups=True) |
| 1259 | assert ( |
nothing calls this directly
no test coverage detected
searching dependent graphs…