Skip to content

Commit 6f8b460

Browse files
steipetewalker1211
andcommitted
fix(cli): keep zsh subcommand descriptions literal
Reuse the canonical description encoder for the double-quoted zsh arguments specification while retaining its single-quote layer. Remove the duplicate escaping path and cover built-in, root, and nested commands through real zsh parsing. Fixes #135089 Related: #76126, #64490 Co-authored-by: walker <[email protected]>
1 parent e865955 commit 6f8b460

2 files changed

Lines changed: 69 additions & 6 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
runGeneratedBashCompletion,
1515
runGeneratedFishCompletion,
1616
} from "./completion-cli.test-support.js";
17+
import { registerModelsCli } from "./models-cli.js";
1718

1819
const powerShellCompletion = new PowerShellCompletionRunner();
1920

@@ -86,6 +87,73 @@ describe("completion-cli", () => {
8687
expect(script).not.toContain("John'\\''s");
8788
});
8889

90+
it.skipIf(process.platform === "win32").each(["built-in", "root", "nested"] as const)(
91+
"keeps %s command descriptions literal through real zsh parsing",
92+
(scope) => {
93+
const program = new Command().name("openclaw");
94+
let describedCommand: Command;
95+
let completionFunction: string;
96+
if (scope === "built-in") {
97+
registerModelsCli(program);
98+
const auth = program.commands
99+
.find((command) => command.name() === "models")
100+
?.commands.find((command) => command.name() === "auth");
101+
const logout = auth?.commands.find((command) => command.name() === "logout");
102+
if (!logout) {
103+
throw new Error("Models auth logout command is unavailable");
104+
}
105+
describedCommand = logout;
106+
completionFunction = "_openclaw_models_auth";
107+
} else {
108+
const parent = scope === "nested" ? program.command("parent") : program;
109+
describedCommand = parent
110+
.command("inspect")
111+
.alias("review")
112+
.description(
113+
'Show John\'s "literal" $OPENCLAW_COMPLETION_LITERAL with `models auth list`',
114+
);
115+
completionFunction = scope === "nested" ? "_openclaw_parent" : "_openclaw_root_completion";
116+
}
117+
118+
const result = spawnSync(
119+
"zsh",
120+
[
121+
"-fc",
122+
`${getCompletionScript("zsh", program)}
123+
OPENCLAW_COMPLETION_LITERAL=expanded-value
124+
models() { printf '%s\\n' "OPENCLAW_COMPLETION_DESCRIPTION_EVALUATED:$*" >&2; }
125+
_arguments() {
126+
local spec
127+
for spec in "$@"; do
128+
if [[ "$spec" == "1: :"* ]]; then
129+
local -a action
130+
eval "action=( \${spec#1: :} )"
131+
printf '%s\\0' "\${action[@]}"
132+
fi
133+
done
134+
}
135+
${completionFunction}
136+
`,
137+
],
138+
{ encoding: "utf8", timeout: 10_000 },
139+
);
140+
if (result.error) {
141+
if ("code" in result.error && result.error.code === "ENOENT") {
142+
return;
143+
}
144+
throw result.error;
145+
}
146+
147+
expect(result.stderr).toBe("");
148+
expect(result.status).toBe(0);
149+
const action = result.stdout.split("\0").filter(Boolean);
150+
expect(action.slice(0, 2)).toEqual(["_values", "command"]);
151+
for (const name of [describedCommand.name(), ...describedCommand.aliases()]) {
152+
expect(action).toContain(`${name}[${describedCommand.description()}]`);
153+
}
154+
},
155+
);
156+
89157
it("marks zsh option arguments and completes validated shell choices", () => {
90158
const script = getCompletionScript("zsh", createDocumentedCompletionProgram());
91159

‎src/cli/completion-cli.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,7 @@ function escapeZshCompletionChoice(choice: string): string {
324324
function generateZshSubcmdList(cmd: Command): string {
325325
const list = visibleCompletionCommands(cmd)
326326
.flatMap((c) => {
327-
const desc = c
328-
.description()
329-
.replace(/\\/g, "\\\\")
330-
.replace(/'/g, "'\\''")
331-
.replace(/\[/g, "\\[")
332-
.replace(/\]/g, "\\]");
327+
const desc = escapeZshDoubleQuotedDescription(c.description()).replace(/'/g, "'\\''");
333328
return commandNameVariants(c).map((name) => `'${name}[${desc}]'`);
334329
})
335330
.join(" ");

0 commit comments

Comments
 (0)