openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
145 lines (144 loc) • 4.68 kB
JavaScript
//#region cli-root-options.mjs
/** CLI token that stops root option scanning and leaves following args positional. */
const FLAG_TERMINATOR = "--";
const ROOT_BOOLEAN_FLAGS = /* @__PURE__ */ new Set(["--dev", "--no-color"]);
const ROOT_VALUE_FLAGS = /* @__PURE__ */ new Set([
"--profile",
"--log-level",
"--container"
]);
/** Returns whether a token can be consumed as a root option value. */
function isValueToken(e) {
if (!e || e === "--") return false;
if (!e.startsWith("-")) return true;
return /^-\d+(?:\.\d+)?$/.test(e);
}
/** Count root-option tokens conservatively for route matching. */
function consumeRootOptionToken(e, t) {
const n = e[t];
if (!n) return 0;
if (ROOT_BOOLEAN_FLAGS.has(n)) return 1;
if (n.startsWith("--profile=") || n.startsWith("--log-level=") || n.startsWith("--container=")) return 1;
if (ROOT_VALUE_FLAGS.has(n)) return isValueToken(e[t + 1]) ? 2 : 1;
return 0;
}
/** Consume required root values by their Commander role before startup policy is selected. */
function consumeRootCommandOptionToken(e, t) {
return consumeKnownOptionToken(e, t, ROOT_BOOLEAN_FLAGS, ROOT_VALUE_FLAGS, "command-path");
}
/** Read positional command tokens while accepting root options at any pre-terminator position. */
function getRootOptionAwareCommandPath(e, t) {
const n = e.slice(2);
const r = [];
let i = false;
for (let e = 0; e < n.length; e += 1) {
const a = n[e];
if (!a) break;
if (!i && a === "--") {
if (r.length > 0) break;
i = true;
continue;
}
const o = i ? 0 : consumeRootCommandOptionToken(n, e);
if (o > 0) {
e += o - 1;
continue;
}
if (!i && a.startsWith("-")) continue;
r.push(a);
if (r.length >= t) break;
}
return r;
}
function rewriteUpdateFlagArgv(e) {
const t = e.indexOf("--update");
if (t === -1) return e;
for (let n = 2; n < e.length; n++) {
const r = e[n];
if (!r || r === "--") return e;
if (n === t) return [
...e.slice(0, t),
"update",
...e.slice(t + 1)
];
const i = consumeRootOptionToken(e, n);
if (i > 0) {
n += i - 1;
continue;
}
if (!r.startsWith("-")) return e;
}
return e;
}
/** Launchers choose their signal budget before the CLI parses, so accept both spellings. */
function isUpdateCommandArgv(e) {
return getRootOptionAwareCommandPath(rewriteUpdateFlagArgv(e), 1)[0] === "update";
}
function consumeKnownOptionToken(e, t, n, r, i) {
const a = e[t];
if (!a || a === "--" || !a.startsWith("-")) return 0;
const o = a.indexOf("=");
const s = o === -1 ? a : a.slice(0, o);
if (n.has(s)) return o === -1 ? 1 : 0;
if (!r.has(s)) return 0;
if (o !== -1) return i === "command-path" || a.slice(o + 1).trim() ? 1 : 0;
if (i === "command-path") return e[t + 1] !== void 0 ? 2 : 0;
return isValueToken(e[t + 1]) ? 2 : 0;
}
/** Parse command positionals while consuming known root and command options. */
function getCommandPositionalsWithRootOptions(e, t) {
return parseCommandArgsWithRootOptions(e, t, false);
}
/** Preserve the leaf's raw arguments after consuming its root and parent options. */
function getCommandArgsWithRootOptions(e, t) {
return parseCommandArgsWithRootOptions(e, t, true);
}
function parseCommandArgsWithRootOptions(e, t, n, r = false) {
const i = e.slice(2);
const a = new Set(t.booleanFlags ?? []);
const o = new Set(t.valueFlags ?? []);
const s = [];
const c = [];
const l = [];
let u = 0;
let d = false;
for (let e = 0; e < i.length; e += 1) {
const f = i[e];
if (f === void 0) break;
if (!d && f === "--") {
if (t.mode !== "command-path") break;
d = true;
continue;
}
const p = d ? 0 : t.mode === "command-path" ? consumeRootCommandOptionToken(i, e) : consumeRootOptionToken(i, e);
if (p > 0) {
(u > 0 && a.has(f) ? l : c).push(...i.slice(e, e + p));
e += p - 1;
continue;
}
if (!d && f.startsWith("-")) {
const n = consumeKnownOptionToken(i, e, a, o, t.mode);
if (n === 0 || u === 0) return null;
l.push(f);
e += n - 1;
continue;
}
if (u < t.commandPath.length) {
if (f !== t.commandPath[u]) return null;
u += 1;
if (n && u === t.commandPath.length) {
const t = i.slice(e + 1);
return d ? ["--", ...t] : t;
}
continue;
}
s.push(f);
if (!r && s.length === t.maxPositionals) return s;
}
return u < t.commandPath.length ? null : r ? {
rootOptions: c,
commandOptions: l
} : s;
}
//#endregion
export { getCommandArgsWithRootOptions as a, isUpdateCommandArgv as c, consumeRootOptionToken as i, isValueToken as l, consumeKnownOptionToken as n, getCommandPositionalsWithRootOptions as o, consumeRootCommandOptionToken as r, getRootOptionAwareCommandPath as s, FLAG_TERMINATOR as t, rewriteUpdateFlagArgv as u };