Repository navigation
Expand file tree
/
Copy pathcli-root-options.mjs
More file actions
226 lines (210 loc) · 6.99 KB
/
Copy pathcli-root-options.mjs
File metadata and controls
226 lines (210 loc) · 6.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/** CLI token that stops root option scanning and leaves following args positional. */
export const FLAG_TERMINATOR = "--";
const ROOT_BOOLEAN_FLAGS = new Set(["--dev", "--no-color"]);
const ROOT_VALUE_FLAGS = new Set(["--profile", "--log-level", "--container"]);
/** Returns whether a token can be consumed as a root option value. */
export function isValueToken(arg) {
if (!arg || arg === FLAG_TERMINATOR) {
return false;
}
if (!arg.startsWith("-")) {
return true;
}
return /^-\d+(?:\.\d+)?$/.test(arg);
}
/** Count root-option tokens conservatively for route matching. */
export function consumeRootOptionToken(args, index) {
const arg = args[index];
if (!arg) {
return 0;
}
if (ROOT_BOOLEAN_FLAGS.has(arg)) {
return 1;
}
if (
arg.startsWith("--profile=") ||
arg.startsWith("--log-level=") ||
arg.startsWith("--container=")
) {
return 1;
}
if (ROOT_VALUE_FLAGS.has(arg)) {
return isValueToken(args[index + 1]) ? 2 : 1;
}
return 0;
}
/** Consume required root values by their Commander role before startup policy is selected. */
export function consumeRootCommandOptionToken(args, index) {
return consumeKnownOptionToken(args, index, ROOT_BOOLEAN_FLAGS, ROOT_VALUE_FLAGS, "command-path");
}
/** Read positional command tokens while accepting root options at any pre-terminator position. */
export function getRootOptionAwareCommandPath(argv, depth) {
const args = argv.slice(2);
const path = [];
let literal = false;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (!arg) {
break;
}
if (!literal && arg === FLAG_TERMINATOR) {
// A leading terminator still leaves a command to discover; later operands belong to callers.
if (path.length > 0) {
break;
}
literal = true;
continue;
}
const consumed = literal ? 0 : consumeRootCommandOptionToken(args, index);
if (consumed > 0) {
index += consumed - 1;
continue;
}
if (!literal && arg.startsWith("-")) {
continue;
}
path.push(arg);
if (path.length >= depth) {
break;
}
}
return path;
}
export function rewriteUpdateFlagArgv(argv) {
// Preserve the old root --update spelling by rewriting before Commander registration.
// Only rewrite --update while scanning the root-option prefix; once a command
// or `--` appears, later --update tokens belong to that command's arguments.
const updateIndex = argv.indexOf("--update");
if (updateIndex === -1) {
return argv;
}
for (let i = 2; i < argv.length; i++) {
const arg = argv[i];
if (!arg || arg === FLAG_TERMINATOR) {
return argv;
}
if (i === updateIndex) {
// Old runtimes reach this through Node recovery; avoid newer array helpers.
return [...argv.slice(0, updateIndex), "update", ...argv.slice(updateIndex + 1)];
}
const consumed = consumeRootOptionToken(argv, i);
if (consumed > 0) {
i += consumed - 1;
continue;
}
if (!arg.startsWith("-")) {
return argv;
}
}
return argv;
}
/** Launchers choose their signal budget before the CLI parses, so accept both spellings. */
export function isUpdateCommandArgv(argv) {
return getRootOptionAwareCommandPath(rewriteUpdateFlagArgv(argv), 1)[0] === "update";
}
export function consumeKnownOptionToken(args, index, booleanFlags, valueFlags, mode) {
const arg = args[index];
if (!arg || arg === FLAG_TERMINATOR || !arg.startsWith("-")) {
return 0;
}
const equalsIndex = arg.indexOf("=");
const flag = equalsIndex === -1 ? arg : arg.slice(0, equalsIndex);
if (booleanFlags.has(flag)) {
return equalsIndex === -1 ? 1 : 0;
}
if (!valueFlags.has(flag)) {
return 0;
}
if (equalsIndex !== -1) {
return mode === "command-path" || arg.slice(equalsIndex + 1).trim() ? 1 : 0;
}
// Required Commander values include empty strings, flag-looking tokens, and `--`.
// Discovery must consume them before choosing startup policy; routes still validate values.
if (mode === "command-path") {
return args[index + 1] !== undefined ? 2 : 0;
}
return isValueToken(args[index + 1]) ? 2 : 0;
}
/** Parse command positionals while consuming known root and command options. */
export function getCommandPositionalsWithRootOptions(argv, options) {
return parseCommandArgsWithRootOptions(argv, options, false);
}
/** Preserve the leaf's raw arguments after consuming its root and parent options. */
export function getCommandArgsWithRootOptions(argv, options) {
return parseCommandArgsWithRootOptions(argv, options, true);
}
/** Keep option roles intact when another command must use the same root selectors. */
export function getCommandOptionsWithRootOptions(argv, options) {
return parseCommandArgsWithRootOptions(argv, options, false, true);
}
function parseCommandArgsWithRootOptions(argv, options, returnTail, returnOptions = false) {
const args = argv.slice(2);
const booleanFlags = new Set(options.booleanFlags ?? []);
const valueFlags = new Set(options.valueFlags ?? []);
const positionals = [];
const rootOptions = [];
const commandOptions = [];
let commandIndex = 0;
let literal = false;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === undefined) {
break;
}
if (!literal && arg === FLAG_TERMINATOR) {
if (options.mode !== "command-path") {
break;
}
literal = true;
continue;
}
const rootConsumed = literal
? 0
: options.mode === "command-path"
? consumeRootCommandOptionToken(args, index)
: consumeRootOptionToken(args, index);
if (rootConsumed > 0) {
// Gateway's post-command --dev bootstraps its workspace, not a root profile.
const destination = commandIndex > 0 && booleanFlags.has(arg) ? commandOptions : rootOptions;
destination.push(...args.slice(index, index + rootConsumed));
index += rootConsumed - 1;
continue;
}
if (!literal && arg.startsWith("-")) {
const optionConsumed = consumeKnownOptionToken(
args,
index,
booleanFlags,
valueFlags,
options.mode,
);
if (optionConsumed === 0 || commandIndex === 0) {
return null;
}
commandOptions.push(arg);
index += optionConsumed - 1;
continue;
}
if (commandIndex < options.commandPath.length) {
if (arg !== options.commandPath[commandIndex]) {
return null;
}
commandIndex += 1;
if (returnTail && commandIndex === options.commandPath.length) {
const tail = args.slice(index + 1);
// A downstream parser must not reactivate flags after an earlier literal boundary.
return literal ? [FLAG_TERMINATOR, ...tail] : tail;
}
continue;
}
positionals.push(arg);
if (!returnOptions && positionals.length === options.maxPositionals) {
return positionals;
}
}
return commandIndex < options.commandPath.length
? null
: returnOptions
? { rootOptions, commandOptions }
: positionals;
}