Skip to content

Commit 64b931f

Browse files
committed
Keep format of readMe the same as before refactor
1 parent 428a11b commit 64b931f

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

clai/README.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,27 @@ Either way, running `clai` will start an interactive session where you can chat
5454
## Help
5555

5656
```
57-
Usage: clai [OPTIONS] [PROMPT]
58-
59-
Options:
60-
-m, --model TEXT Model to use, in format "<provider>:<model>" e.g.
61-
"openai:gpt-4.1" or "anthropic:claude-sonnet-4-0".
62-
Defaults to "openai:gpt-4.1".
63-
-a, --agent TEXT Custom Agent to use, in format "module:variable",
64-
e.g. "mymodule.submodule:my_agent"
65-
-l, --list-models List all available models and exit
66-
-t, --code-theme TEXT Which colors to use for code, can be "dark", "light"
67-
or any theme from pygments.org/styles/. Defaults to
68-
"dark" which works well on dark terminals.
69-
--no-stream Disable streaming from the model
70-
--version Show version and exit
71-
-h, --help Show this message and exit.
57+
usage: clai [-h] [-m [MODEL]] [-a AGENT] [-l] [-t [CODE_THEME]] [--no-stream] [--version] [prompt]
58+
59+
PydanticAI CLI v0.3.5
60+
61+
Special prompts:
62+
* `/exit` - exit the interactive mode (ctrl-c and ctrl-d also work)
63+
* `/markdown` - show the last markdown output of the last question
64+
* `/multiline` - toggle multiline mode
65+
66+
positional arguments:
67+
prompt AI Prompt, if omitted fall into interactive mode
68+
69+
options:
70+
-h, --help show this help message and exit
71+
-m [MODEL], --model [MODEL]
72+
Model to use, in format "<provider>:<model>" e.g. "openai:gpt-4o" or "anthropic:claude-3-7-sonnet-latest". Defaults to "openai:gpt-4o".
73+
-a AGENT, --agent AGENT
74+
Custom Agent to use, in format "module:variable", e.g. "mymodule.submodule:my_agent"
75+
-l, --list-models List all available models and exit
76+
-t [CODE_THEME], --code-theme [CODE_THEME]
77+
Which colors to use for code, can be "dark", "light" or any theme from pygments.org/styles/. Defaults to "dark" which works well on dark terminals.
78+
--no-stream Disable streaming from the model
79+
--version Show version and exit
7280
```

pydantic_ai_slim/pydantic_ai/_cli.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
try:
2525
import click
26+
from click import HelpFormatter
2627
from prompt_toolkit import PromptSession
2728
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory, Suggestion
2829
from prompt_toolkit.buffer import Buffer
@@ -87,6 +88,34 @@ def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderR
8788
)
8889

8990

91+
class ArgparseStyleCommand(click.Command):
92+
"""Command subclass that keeps the argparse-style output."""
93+
94+
def get_usage(self, ctx: click.Context) -> str:
95+
# exact synopsis argparse produced previously (without the program name)
96+
return '[-h] [-m [MODEL]] [-a AGENT] [-l] [-t [CODE_THEME]] [--no-stream] [--version] [PROMPT]'
97+
98+
# override usage line
99+
def format_usage(self, ctx: click.Context, formatter: HelpFormatter) -> None:
100+
formatter.write_usage(ctx.command_path, self.get_usage(ctx))
101+
102+
# Keep the positional prompt argument details in the README
103+
def format_help(self, ctx: click.Context, formatter: HelpFormatter) -> None:
104+
# usage
105+
self.format_usage(ctx, formatter)
106+
formatter.write_paragraph()
107+
108+
if self.help:
109+
with formatter.section('Description'):
110+
formatter.write_text(self.help)
111+
112+
# positional argument description
113+
with formatter.section('positional arguments'):
114+
formatter.write_dl([('prompt', 'AI Prompt, if omitted fall into interactive mode')])
115+
116+
self.format_options(ctx, formatter)
117+
118+
90119
cli_agent = Agent()
91120

92121

@@ -115,7 +144,10 @@ def cli(
115144
"""Run the CLI and return the exit code for the process."""
116145

117146
# Create click command for parsing
118-
@click.command(context_settings={'help_option_names': ['-h', '--help']})
147+
@click.command(
148+
cls=ArgparseStyleCommand,
149+
context_settings={'help_option_names': ['-h', '--help']},
150+
)
119151
@click.argument('prompt', required=False)
120152
@click.option(
121153
'-m',
@@ -169,18 +201,16 @@ def click_cli(
169201
default_model=default_model,
170202
)
171203

172-
# Check if this is a help or version request that should raise SystemExit
173-
should_exit = args_list and any(arg in ['--help', '-h', '--version'] for arg in args_list)
204+
# Detect if the user explicitly asked for help (should mimic argparse behaviour)
205+
help_requested = args_list and any(arg in ('--help', '-h') for arg in args_list)
174206

175-
# Invoke click command with appropriate mode
176207
try:
177-
if should_exit:
178-
# Use standalone_mode=True for --help/--version to get SystemExit behavior
179-
click_cli.main(args_list, standalone_mode=True, prog_name=prog_name)
180-
else:
181-
# Use standalone_mode=False for normal operations
182-
result = click_cli.main(args_list, standalone_mode=False, prog_name=prog_name)
183-
return result if result is not None else 0
208+
result = click_cli.main(args_list, standalone_mode=False, prog_name=prog_name)
209+
210+
if help_requested:
211+
raise SystemExit(0)
212+
213+
return result if result is not None else 0
184214
except click.ClickException as e:
185215
e.show()
186216
return 1

0 commit comments

Comments
 (0)