Skip to content

Commit e3d4cd7

Browse files
committed
Add --help unit test for test coverage of ArgparseStyleCommand
1 parent 6eb429b commit e3d4cd7

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
@@ -155,6 +155,36 @@ def test_list_models(capfd: CaptureFixture[str]):
155155
assert models == set(), models
156156

157157

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

0 commit comments

Comments
 (0)