Skip to content

Commit 5338740

Browse files
committed
Keep format of readMe the same as before refactor
1 parent 293b729 commit 5338740

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
@@ -53,19 +53,27 @@ Either way, running `clai` will start an interactive session where you can chat
5353
## Help
5454

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

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)