mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 04:18:05 +00:00
fix(proxy): parse flag options before profile args
This commit is contained in:
@@ -28,7 +28,8 @@ function hasHelpFlag(args: string[]): boolean {
|
||||
|
||||
export function findPositionalArg(
|
||||
args: string[],
|
||||
optionsWithValues: string[] = []
|
||||
optionsWithValues: string[] = [],
|
||||
flagOptions: string[] = []
|
||||
): string | undefined {
|
||||
for (let i = 0; i < args.length; i += 1) {
|
||||
const arg = args[i];
|
||||
@@ -36,6 +37,9 @@ export function findPositionalArg(
|
||||
return i + 1 < args.length ? args[i + 1] : undefined;
|
||||
}
|
||||
if (arg.startsWith('-')) {
|
||||
if (flagOptions.includes(arg)) {
|
||||
continue;
|
||||
}
|
||||
if (optionsWithValues.includes(arg)) {
|
||||
i += 1;
|
||||
}
|
||||
@@ -90,7 +94,7 @@ async function handleStart(args: string[]): Promise<number> {
|
||||
if (hasHelpFlag(args)) {
|
||||
return showHelp();
|
||||
}
|
||||
const profileName = findPositionalArg(args, ['--port', '--host']);
|
||||
const profileName = findPositionalArg(args, ['--port', '--host'], ['--insecure']);
|
||||
if (!profileName) {
|
||||
console.error(
|
||||
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');
|
||||
});
|
||||
|
||||
it('skips flag options that do not take values', () => {
|
||||
expect(findPositionalArg(['--insecure', 'ccg'], ['--port', '--host'], ['--insecure'])).toBe(
|
||||
'ccg'
|
||||
);
|
||||
});
|
||||
|
||||
it('treats arguments after -- as positional', () => {
|
||||
expect(findPositionalArg(['--', '--port', '3456'], ['--port'])).toBe('--port');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user