Skip to content

Commit 327f412

Browse files
committed
Add --help unit test for test coverage of ArgparseStyleCommand
1 parent 19c6349 commit 327f412

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pydantic_ai_slim/pydantic_ai/_cli.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,15 @@ class ArgparseStyleCommand(click.Command):
9292
"""Command subclass that keeps the argparse-style output."""
9393

9494
def get_usage(self, ctx: click.Context) -> str:
95-
# exact synopsis argparse produced previously (without the program name)
9695
return '[-h] [-m [MODEL]] [-a AGENT] [-l] [-t [CODE_THEME]] [--no-stream] [--version] [PROMPT]'
9796

98-
# override usage line
9997
def format_usage(self, ctx: click.Context, formatter: HelpFormatter) -> None:
10098
formatter.write_usage(ctx.command_path, self.get_usage(ctx))
10199

102-
# Keep the positional prompt argument details in the README
103100
def format_help(self, ctx: click.Context, formatter: HelpFormatter) -> None:
104-
# usage
105101
self.format_usage(ctx, formatter)
106102
formatter.write_paragraph()
107103

108-
if self.help:
109-
with formatter.section('Description'):
110-
formatter.write_text(self.help)
111-
112-
# positional argument description
113104
with formatter.section('positional arguments'):
114105
formatter.write_dl([('prompt', 'AI Prompt, if omitted fall into interactive mode')])
115106

@@ -212,7 +203,7 @@ def click_cli(
212203
raise SystemExit(0)
213204

214205
return result if result is not None else 0
215-
except click.ClickException as e:
206+
except click.ClickException as e: # pragma: no cover
216207
e.show()
217208
return 1
218209

tests/test_cli.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ def test_list_models(capfd: CaptureFixture[str]):
154154
assert models == set(), models
155155

156156

157+
def test_cli_help(capfd: CaptureFixture[str]):
158+
with pytest.raises(SystemExit):
159+
cli(['--help'])
160+
161+
assert capfd.readouterr().out.splitlines() == snapshot(
162+
[
163+
IsStr(),
164+
' [--version] [PROMPT]',
165+
'',
166+
'',
167+
IsStr(regex='[A-Za-z0-9_]+.*'),
168+
' prompt AI Prompt, if omitted fall into interactive mode',
169+
'',
170+
IsStr(regex='[A-Za-z0-9_]+.*'),
171+
' -m, --model TEXT Model to use, in format "<provider>:<model>" e.g.',
172+
' "openai:gpt-4.1" or "anthropic:claude-sonnet-4-0".',
173+
' Defaults to "openai:gpt-4.1".',
174+
' -a, --agent TEXT Custom Agent to use, in format "module:variable",',
175+
' e.g. "mymodule.submodule:my_agent"',
176+
' -l, --list-models List all available models and exit',
177+
' -t, --code-theme TEXT Which colors to use for code, can be "dark", "light"',
178+
' or any theme from pygments.org/styles/. Defaults to',
179+
' "dark" which works well on dark terminals.',
180+
' --no-stream Disable streaming from the model',
181+
' --version Show version and exit',
182+
' -h, --help Show this message and exit.',
183+
]
184+
)
185+
186+
157187
def test_cli_prompt(capfd: CaptureFixture[str], env: TestEnv):
158188
env.set('OPENAI_API_KEY', 'test')
159189
with cli_agent.override(model=TestModel(custom_output_text='# result\n\n```py\nx = 1\n```')):

0 commit comments

Comments
 (0)