|
23 | 23 |
|
24 | 24 | try:
|
25 | 25 | import click
|
| 26 | + from click import HelpFormatter |
26 | 27 | from prompt_toolkit import PromptSession
|
27 | 28 | from prompt_toolkit.auto_suggest import AutoSuggestFromHistory, Suggestion
|
28 | 29 | from prompt_toolkit.buffer import Buffer
|
@@ -87,6 +88,34 @@ def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderR
|
87 | 88 | )
|
88 | 89 |
|
89 | 90 |
|
| 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 | + |
90 | 119 | cli_agent = Agent()
|
91 | 120 |
|
92 | 121 |
|
@@ -115,7 +144,10 @@ def cli(
|
115 | 144 | """Run the CLI and return the exit code for the process."""
|
116 | 145 |
|
117 | 146 | # 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 | + ) |
119 | 151 | @click.argument('prompt', required=False)
|
120 | 152 | @click.option(
|
121 | 153 | '-m',
|
@@ -169,18 +201,16 @@ def click_cli(
|
169 | 201 | default_model=default_model,
|
170 | 202 | )
|
171 | 203 |
|
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) |
174 | 206 |
|
175 |
| - # Invoke click command with appropriate mode |
176 | 207 | 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 |
184 | 214 | except click.ClickException as e:
|
185 | 215 | e.show()
|
186 | 216 | return 1
|
|
0 commit comments