Skip to content

Commit 746c6f6

Browse files
committed
update
1 parent ca48675 commit 746c6f6

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/cac.ts

Lines changed: 6 additions & 4 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
}
@@ -81,10 +84,9 @@ export default async function tab(
8184

8285
// Check if option takes a value (has <> or [] in rawName, or is marked as required)
8386
const takesValue =
84-
option.required || /<[^>]+>|\[[^\]]+\]/.test(option.rawName);
87+
option.required || VALUE_OPTION_RE.test(option.rawName);
8588

8689
if (handler) {
87-
// Has custom handler → value option
8890
if (shortFlag) {
8991
targetCommand.option(
9092
argName,
@@ -96,7 +98,7 @@ export default async function tab(
9698
targetCommand.option(argName, option.description || '', handler);
9799
}
98100
} else if (takesValue) {
99-
// Takes value but no custom handler value option with no completions
101+
// Takes value but no custom handler = value option with no completions
100102
if (shortFlag) {
101103
targetCommand.option(
102104
argName,
@@ -112,7 +114,7 @@ export default async function tab(
112114
);
113115
}
114116
} else {
115-
// No custom handler and doesn't take value boolean flag
117+
// No custom handler and doesn't take value = boolean flag
116118
if (shortFlag) {
117119
targetCommand.option(argName, option.description || '', shortFlag);
118120
} else {

test-regex-constant.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { cac } from 'cac';
2+
import tab from './dist/src/cac.js';
3+
4+
const cli = cac('test-cli');
5+
6+
cli
7+
.command('dev', 'Start development server')
8+
.option('--port <port>', 'Port number')
9+
.option('--verbose', 'Verbose output');
10+
11+
const completion = await tab(cli);
12+
13+
const devCommand = completion.commands.get('dev');
14+
const portOption = devCommand?.options.get('port');
15+
const verboseOption = devCommand?.options.get('verbose');
16+
17+
if (portOption) {
18+
portOption.handler = (complete) => {
19+
complete('3000', 'Development port');
20+
complete('8080', 'Production port');
21+
};
22+
}
23+
24+
console.log('Port option (should be value-taking):', portOption?.isBoolean);
25+
console.log('Verbose option (should be boolean):', verboseOption?.isBoolean);
26+
27+
cli.parse();

0 commit comments

Comments
 (0)