Skip to content

Commit 1080e7c

Browse files
authored
fix: cac: treat options correctly (#60)
* treat options well * update * update
1 parent e7b53f4 commit 1080e7c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/cac.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
1515

1616
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`;
1717

18+
// Regex to detect if an option takes a value (has <required> or [optional] parameters)
19+
const VALUE_OPTION_RE = /<[^>]+>|\[[^\]]+\]/;
20+
1821
function quoteIfNeeded(path: string): string {
1922
return path.includes(' ') ? `'${path}'` : path;
2023
}
@@ -78,8 +81,12 @@ export default async function tab(
7881
const targetCommand = isRootCommand ? t : command;
7982
if (targetCommand) {
8083
const handler = commandCompletionConfig?.options?.[argName];
84+
85+
// Check if option takes a value (has <> or [] in rawName, or is marked as required)
86+
const takesValue =
87+
option.required || VALUE_OPTION_RE.test(option.rawName);
88+
8189
if (handler) {
82-
// Has custom handler → value option
8390
if (shortFlag) {
8491
targetCommand.option(
8592
argName,
@@ -90,8 +97,24 @@ export default async function tab(
9097
} else {
9198
targetCommand.option(argName, option.description || '', handler);
9299
}
100+
} else if (takesValue) {
101+
// Takes value but no custom handler = value option with no completions
102+
if (shortFlag) {
103+
targetCommand.option(
104+
argName,
105+
option.description || '',
106+
async () => [], // Empty completions
107+
shortFlag
108+
);
109+
} else {
110+
targetCommand.option(
111+
argName,
112+
option.description || '',
113+
async () => []
114+
);
115+
}
93116
} else {
94-
// No custom handler boolean flag
117+
// No custom handler and doesn't take value = boolean flag
95118
if (shortFlag) {
96119
targetCommand.option(argName, option.description || '', shortFlag);
97120
} else {

0 commit comments

Comments
 (0)