Skip to content

Commit 6ce9e0d

Browse files
committed
fix(cli): keep completion and Twitch races bounded
1 parent e9bf111 commit 6ce9e0d

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ describe("completion-cli", () => {
110110
expect(script).toContain(
111111
"complete -c openclaw -n \"__openclaw_command_path_matches gateway status\" -l json -d 'JSON output'",
112112
);
113-
expect(script).toContain("set -l root_boolean_options -v --verbose");
113+
expect(script).toContain("set -l value_options -t --token");
114+
expect(script).toContain("if contains -- $flag $value_options");
114115
});
115116

116117
it("generates Bash completions without comma-suffixed short flags", () => {

‎src/cli/completion-cli.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,40 @@ function fishOptionFlags(options: Command["options"], wantsValue: boolean): stri
5656
});
5757
}
5858

59+
function collectFishOptionFlags(program: Command, wantsValue: boolean): string[] {
60+
const flags = new Set<string>();
61+
const visit = (cmd: Command) => {
62+
for (const flag of fishOptionFlags(cmd.options, wantsValue)) {
63+
flags.add(flag);
64+
}
65+
for (const child of cmd.commands) {
66+
visit(child);
67+
}
68+
};
69+
visit(program);
70+
return [...flags];
71+
}
72+
5973
function generateFishPathHelper(program: Command, rootCmd: string): string {
60-
const rootValueOptions = fishOptionFlags(program.options, true);
61-
const rootBooleanOptions = fishOptionFlags(program.options, false);
74+
const valueOptions = collectFishOptionFlags(program, true);
6275
return `
6376
function __${rootCmd}_command_path_matches
6477
set -l tokens (commandline -opc)
6578
set -e tokens[1]
66-
set -l root_value_options ${fishWords(rootValueOptions)}
67-
set -l root_boolean_options ${fishWords(rootBooleanOptions)}
79+
set -l value_options ${fishWords(valueOptions)}
6880
set -l command_tokens
6981
set -l skip_next 0
7082
for token in $tokens
7183
if test $skip_next -eq 1
7284
set skip_next 0
7385
continue
7486
end
75-
if test (count $command_tokens) -eq 0
76-
set -l flag (string split -m1 "=" -- $token)[1]
77-
if contains -- $flag $root_boolean_options
78-
continue
79-
end
80-
if contains -- $flag $root_value_options
81-
if not string match -q -- "*=*" $token
82-
set skip_next 1
83-
end
84-
continue
87+
set -l flag (string split -m1 "=" -- $token)[1]
88+
if contains -- $flag $value_options
89+
if not string match -q -- "*=*" $token
90+
set skip_next 1
8591
end
92+
continue
8693
end
8794
if string match -q -- "-*" $token
8895
continue

0 commit comments

Comments
 (0)