Skip to content

Commit 7218bfe

Browse files
committed
fix(cli): escape backticks in zsh completion descriptions
Prevent generated zsh completion scripts from treating command examples in descriptions as command substitutions.
1 parent d228b0d commit 7218bfe

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎src/cli/completion-cli.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ describe("completion-cli", () => {
3131
expect(script).toContain("--force[Force the action]");
3232
});
3333

34+
it("escapes zsh descriptions that contain backticks", () => {
35+
const program = new Command();
36+
program.name("openclaw");
37+
program
38+
.command("models")
39+
.description("Model commands")
40+
.option("--status-json", "Output JSON (alias for `models status --json`)");
41+
program
42+
.command("hooks")
43+
.description("Hook commands")
44+
.command("install")
45+
.description("Deprecated: install via `openclaw plugins install`");
46+
47+
const script = getCompletionScript("zsh", program);
48+
49+
expect(script).toContain("--status-json[Output JSON (alias for \\`models status --json\\`)]");
50+
expect(script).toContain("install[Deprecated: install via \\`openclaw plugins install\\`]");
51+
});
52+
3453
it("defers zsh registration until compinit is available", async () => {
3554
if (process.platform === "win32") {
3655
return;

‎src/cli/completion-cli.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function generateZshArgs(cmd: Command): string {
195195
const short = flags.find((f) => f.startsWith("-") && !f.startsWith("--"));
196196
const desc = opt.description
197197
.replace(/\\/g, "\\\\")
198+
.replace(/`/g, "\\`")
198199
.replace(/"/g, '\\"')
199200
.replace(/'/g, "'\\''")
200201
.replace(/\[/g, "\\[")
@@ -213,6 +214,7 @@ function generateZshSubcmdList(cmd: Command): string {
213214
const desc = c
214215
.description()
215216
.replace(/\\/g, "\\\\")
217+
.replace(/`/g, "\\`")
216218
.replace(/'/g, "'\\''")
217219
.replace(/\[/g, "\\[")
218220
.replace(/\]/g, "\\]");

0 commit comments

Comments
 (0)