MCPcopy Create free account
hub / github.com/tiann/hapi / normalizeCliArgs

Function normalizeCliArgs

cli/src/utils/cliArgs.ts:37–74  ·  view source on GitHub ↗
(rawArgv: string[])

Source from the content-addressed store, hash-verified

35}
36
37export function normalizeCliArgs(rawArgv: string[]): string[] {
38 if (!Array.isArray(rawArgv) || rawArgv.length === 0) {
39 return [];
40 }
41
42 const execPath = process.execPath;
43 const execBase = basename(execPath);
44 const bunMain = globalThis.Bun?.main ?? '';
45 const dashIndex = rawArgv.indexOf('--');
46 let argv = rawArgv.slice();
47 if (dashIndex >= 0) {
48 const preArgs = rawArgv.slice(0, dashIndex);
49 const postArgs = rawArgv.slice(dashIndex + 1);
50 argv = hasRuntimeWrapper(preArgs, execPath, execBase, bunMain)
51 ? postArgs
52 : [...preArgs, ...postArgs];
53 }
54
55 let startIndex = 0;
56 while (startIndex < argv.length) {
57 const value = argv[startIndex] || '';
58 const nextValue = argv[startIndex + 1] || '';
59 if (
60 value === 'bun' &&
61 (nextValue === bunMain || nextValue === execPath || nextValue === execBase || isEntrypointPath(nextValue, bunMain))
62 ) {
63 startIndex += 2;
64 continue;
65 }
66 if (value === execPath || value === execBase || isEntrypointPath(value, bunMain)) {
67 startIndex += 1;
68 continue;
69 }
70 break;
71 }
72
73 return argv.slice(startIndex);
74}
75
76export function getCliArgs(): string[] {
77 return normalizeCliArgs(resolveRawArgv());

Callers 1

getCliArgsFunction · 0.85

Calls 3

basenameFunction · 0.85
hasRuntimeWrapperFunction · 0.85
isEntrypointPathFunction · 0.85

Tested by

no test coverage detected