forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-root-options.mjs
More file actions
191 lines (178 loc) 路 5.84 KB
/
Copy pathcli-root-options.mjs
File metadata and controls
191 lines (178 loc) 路 5.84 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
/** 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;
}
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;
}