core/cli: fix /help command usage line and help Tab completion

Use a standalone Click context so /help compact shows Usage: /compact,
not nested under help. Complete /help <cmd> from the slash registry.
This commit is contained in:
2026-07-15 13:09:24 +08:00
parent 972bcd78ee
commit 5e016c012b
4 changed files with 50 additions and 13 deletions
+14 -11
View File
@@ -60,17 +60,20 @@ def build_completer(state: ReplState) -> Callable[[str, int], str | None]:
def _argument_options(state: ReplState, command: str, text: str) -> list[str]:
if command == "/provider":
return filter_prefix(text, sorted(state.config.providers.keys()))
if command == "/model":
return filter_prefix(text, sorted(state.provider.models.keys()))
if command in {"/tools", "/stream", "/verbose"}:
return filter_prefix(text, list(_ON_OFF_ARGS))
if command == "/export":
return filter_prefix(text, list(_EXPORT_ARGS))
if command == "/resume":
return []
return []
candidates: list[str] = []
if command == "/help":
# Second token: command name without leading slash (also accept /name).
candidates = [n.removeprefix("/") for n in slash_commands()]
text = text.lstrip("/")
elif command == "/provider":
candidates = sorted(state.config.providers.keys())
elif command == "/model":
candidates = sorted(state.provider.models.keys())
elif command in {"/tools", "/stream", "/verbose"}:
candidates = list(_ON_OFF_ARGS)
elif command == "/export":
candidates = list(_EXPORT_ARGS)
return filter_prefix(text, candidates)
def bind_tab_complete(readline_mod: object) -> None:
+2 -2
View File
@@ -112,8 +112,8 @@ def help_cmd(ctx: click.Context, command: str | None) -> None:
if cmd is None:
click.echo(f"unknown command /{name}; try /help")
return
# Build a child context so get_help includes usage.
with click.Context(cmd, info_name=name, parent=ctx) as sub:
# Standalone context (no parent=help) so usage is "/compact …" not "help … compact".
with click.Context(cmd, info_name=f"/{name}") as sub:
click.echo(cmd.get_help(sub))
return
click.echo(slash.get_help(ctx))