Repository navigation
Expand file tree
/
Copy pathargs.ts
More file actions
66 lines (54 loc) · 1.79 KB
/
Copy pathargs.ts
File metadata and controls
66 lines (54 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* CLI 인자 파싱 및 URL 조작 유틸리티
*/
import { DEFAULT_BASE_URL } from './constants.js';
export function parseCliArgs(args: string[]): { positionals: string[]; options: Record<string, string> } {
const positionals: string[] = [];
const options: Record<string, string> = {};
for (let index = 0; index < args.length; index += 1) {
const token = args[index];
if (!token.startsWith('--')) {
positionals.push(token);
continue;
}
const withoutPrefix = token.slice(2);
const equalIndex = withoutPrefix.indexOf('=');
if (equalIndex >= 0) {
const key = withoutPrefix.slice(0, equalIndex);
const value = withoutPrefix.slice(equalIndex + 1);
options[key] = value;
continue;
}
const key = withoutPrefix;
const nextValue = args[index + 1];
if (!nextValue || nextValue.startsWith('--')) {
options[key] = 'true';
continue;
}
options[key] = nextValue;
index += 1;
}
return { positionals, options };
}
export function toUrl(pathOrUrl: string): URL {
if (pathOrUrl.startsWith('http://') || pathOrUrl.startsWith('https://')) {
return new URL(pathOrUrl);
}
const normalizedPath = pathOrUrl.startsWith('/') ? pathOrUrl : `/${pathOrUrl}`;
return new URL(normalizedPath, DEFAULT_BASE_URL);
}
export function applyOptionsToQuery(url: URL, options: Record<string, string>): void {
for (const [key, value] of Object.entries(options)) {
url.searchParams.set(key, value);
}
}
export function toQueryOptions(options: Record<string, string>): Record<string, string> {
const queryOptions: Record<string, string> = {};
for (const [key, value] of Object.entries(options)) {
if (key === 'help' || key === 'json') {
continue;
}
queryOptions[key] = value;
}
return queryOptions;
}