mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-03 18:16:29 +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(
|
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');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user