fix(proxy): parse flag options before profile args

This commit is contained in:
Wooseong Kim
2026-04-20 15:44:20 +09:00
parent afcb1abf9e
commit 6928d8bb65
2 changed files with 12 additions and 2 deletions
+6 -2
View File
@@ -28,7 +28,8 @@ function hasHelpFlag(args: string[]): boolean {
export function findPositionalArg( export function findPositionalArg(
args: string[], args: string[],
optionsWithValues: string[] = [] optionsWithValues: string[] = [],
flagOptions: string[] = []
): string | undefined { ): string | undefined {
for (let i = 0; i < args.length; i += 1) { for (let i = 0; i < args.length; i += 1) {
const arg = args[i]; const arg = args[i];
@@ -36,6 +37,9 @@ export function findPositionalArg(
return i + 1 < args.length ? args[i + 1] : undefined; return i + 1 < args.length ? args[i + 1] : undefined;
} }
if (arg.startsWith('-')) { if (arg.startsWith('-')) {
if (flagOptions.includes(arg)) {
continue;
}
if (optionsWithValues.includes(arg)) { if (optionsWithValues.includes(arg)) {
i += 1; i += 1;
} }
@@ -90,7 +94,7 @@ async function handleStart(args: string[]): Promise<number> {
if (hasHelpFlag(args)) { if (hasHelpFlag(args)) {
return showHelp(); return showHelp();
} }
const profileName = findPositionalArg(args, ['--port', '--host']); const profileName = findPositionalArg(args, ['--port', '--host'], ['--insecure']);
if (!profileName) { if (!profileName) {
console.error( console.error(
fail('Usage: ccs proxy start <profile> [--port <n>] [--host <addr>] [--insecure]') fail('Usage: ccs proxy start <profile> [--port <n>] [--host <addr>] [--insecure]')
@@ -6,6 +6,12 @@ describe('findPositionalArg', () => {
expect(findPositionalArg(['--port', '3456', 'ccg'], ['--port'])).toBe('ccg'); expect(findPositionalArg(['--port', '3456', 'ccg'], ['--port'])).toBe('ccg');
}); });
it('skips flag options that do not take values', () => {
expect(findPositionalArg(['--insecure', 'ccg'], ['--port', '--host'], ['--insecure'])).toBe(
'ccg'
);
});
it('treats arguments after -- as positional', () => { it('treats arguments after -- as positional', () => {
expect(findPositionalArg(['--', '--port', '3456'], ['--port'])).toBe('--port'); expect(findPositionalArg(['--', '--port', '3456'], ['--port'])).toBe('--port');
}); });