Repository navigation
Expand file tree
/
Copy pathcli-boolean-flags.ts
More file actions
79 lines (75 loc) · 2.16 KB
/
Copy pathcli-boolean-flags.ts
File metadata and controls
79 lines (75 loc) · 2.16 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
67
68
69
70
71
72
73
74
75
76
77
78
79
// Boolean flags accept `--flag`, `--flag=true`, or `--flag false`.
// They must not consume the following positional command
// (`--use-oauth mr list` is `mr`, not `list`).
export const BOOLEAN_CLI_FLAG_NAMES = new Set([
"use-oauth",
"is-old",
"read-only",
"masking-enabled",
"use-wiki",
"use-milestone",
"use-pipeline",
"disable-version-check",
"sse",
"streamable-http",
"remote-auth",
"mcp-oauth",
"allow-unauthenticated-tool-discovery",
"mcp-trust-proxy",
"oauth-callback-proxy",
"enable-dynamic-api-url",
"enable-dynamic-project-scope",
"enable-strict-project-scope",
"oauth-stateless-mode",
"compact-results",
]);
const BOOLEAN_FLAG_LITERALS = new Set(["true", "false", "1", "0"]);
export function isBooleanFlagLiteral(value: string | undefined): value is string {
if (value === undefined || value.startsWith("-")) {
return false;
}
return BOOLEAN_FLAG_LITERALS.has(value.toLowerCase());
}
export function nextBooleanFlagValue(nextToken: string | undefined): {
readonly value: string;
readonly consumeNext: boolean;
} {
if (isBooleanFlagLiteral(nextToken)) {
return { value: nextToken, consumeNext: true };
}
return { value: "true", consumeNext: false };
}
/** Same flag parse as startup config: CLI values only, positionals ignored. */
export function parseCliArgs(argv: readonly string[]): Record<string, string> {
const parsed: Record<string, string> = {};
for (let index = 0; index < argv.length; index++) {
const arg = argv[index];
if (!arg.startsWith("--")) {
continue;
}
const eqIndex = arg.indexOf("=");
if (eqIndex !== -1) {
const key = arg.slice(2, eqIndex);
const value = arg.slice(eqIndex + 1);
if (value) {
parsed[key] = value;
}
continue;
}
const key = arg.slice(2);
if (BOOLEAN_CLI_FLAG_NAMES.has(key)) {
const next = nextBooleanFlagValue(argv[index + 1]);
parsed[key] = next.value;
if (next.consumeNext) {
index += 1;
}
continue;
}
const next = argv[index + 1];
if (next !== undefined && !next.startsWith("--")) {
index += 1;
parsed[key] = next;
}
}
return parsed;
}