Skip to content

Commit 63f265c

Browse files
authored
refactor(scripts): share required option arguments (#133095)
* refactor(scripts): share required option arguments * test(scripts): copy option helper into OpenGrep fixture
1 parent 6425f8f commit 63f265c

21 files changed

Lines changed: 142 additions & 160 deletions

‎.github/workflows/docs-sync-publish.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- scripts/docs-sync-publish.mjs
1111
- scripts/check-docs-mdx.mjs
1212
- scripts/check-docs-mdx.mts
13+
- scripts/lib/arg-utils.runtime.mjs
1314
- scripts/lib/mintlify-accordion.mjs
1415
- scripts/lib/tsx-cli-shim.mjs
1516
- scripts/lib/local-check-runtime.mts

‎scripts/check-docs-i18n-glossary.mts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { execFileSync } from "node:child_process";
55
import fs from "node:fs";
66
import path from "node:path";
7+
import { requireOptionArgument } from "./lib/arg-utils.mts";
78

89
const ROOT = process.cwd();
910
const GLOSSARY_PATH = path.join(ROOT, "docs", ".i18n", "glossary.zh-CN.json");
@@ -20,24 +21,16 @@ type TermMatch = {
2021
term: string;
2122
};
2223

23-
function readRefOptionValue(argv: string[], index: number, optionName: string) {
24-
const value = argv[index + 1];
25-
if (value === undefined || value === "" || value.startsWith("-")) {
26-
throw new Error(`${optionName} requires a value`);
27-
}
28-
return value;
29-
}
30-
3124
export function parseArgs(argv: string[]) {
3225
const args = { base: "", head: "" };
3326
for (let i = 0; i < argv.length; i += 1) {
3427
if (argv[i] === "--base") {
35-
args.base = readRefOptionValue(argv, i, "--base");
28+
args.base = requireOptionArgument(argv, i, "--base");
3629
i += 1;
3730
continue;
3831
}
3932
if (argv[i] === "--head") {
40-
args.head = readRefOptionValue(argv, i, "--head");
33+
args.head = requireOptionArgument(argv, i, "--head");
4134
i += 1;
4235
}
4336
}

‎scripts/check-docs-mdx.mts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from "node:fs";
55
import path from "node:path";
66
import { fileURLToPath } from "node:url";
77
import { compile } from "@mdx-js/mdx";
8+
import { requireOptionArgument } from "./lib/arg-utils.runtime.mjs";
89
import {
910
checkMintlifyAccordionIndentation,
1011
MINTLIFY_ACCORDION_INDENT_MESSAGE,
@@ -100,14 +101,6 @@ function parsePositiveIntegerArg(raw: string | undefined, label: string): number
100101
return value;
101102
}
102103

103-
function readRequiredValue(argv: string[], index: number, label: string): string {
104-
const value = argv[index + 1];
105-
if (!value || value.startsWith("-")) {
106-
throw new Error(`${label} requires a value`);
107-
}
108-
return value;
109-
}
110-
111104
/**
112105
* Parses docs MDX check arguments.
113106
*/
@@ -122,13 +115,13 @@ export function parseArgs(argv: string[]) {
122115
continue;
123116
}
124117
if (part === "--json-out") {
125-
jsonOut = readRequiredValue(argv, index, "--json-out");
118+
jsonOut = requireOptionArgument(argv, index, "--json-out");
126119
index += 1;
127120
continue;
128121
}
129122
if (part === "--max-errors") {
130123
maxErrors = parsePositiveIntegerArg(
131-
readRequiredValue(argv, index, "--max-errors"),
124+
requireOptionArgument(argv, index, "--max-errors"),
132125
"--max-errors",
133126
);
134127
index += 1;

‎scripts/ci-changed-scope.mjs‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Determines CI scope from changed paths.
22
import { execFileSync } from "node:child_process";
33
import { appendFileSync, readFileSync, readdirSync } from "node:fs";
4+
import { requireOptionArgument } from "./lib/arg-utils.runtime.mjs";
45
import { getChangedPathFacts } from "./lib/changed-path-facts.mjs";
56
import { isDirectRunUrl } from "./lib/direct-run.mjs";
67
import { resolveMergeHeadDiffBase } from "./lib/merge-head-diff-base.mjs";
@@ -726,20 +727,6 @@ function isDirectRun() {
726727
return isDirectRunUrl(process.argv[1], import.meta.url);
727728
}
728729

729-
/**
730-
* @param {string[]} argv
731-
* @param {number} index
732-
* @param {string} optionName
733-
* @returns {string}
734-
*/
735-
function readRefValue(argv, index, optionName) {
736-
const value = argv[index + 1];
737-
if (value === undefined || value === "" || value.startsWith("-")) {
738-
throw new Error(`${optionName} requires a value`);
739-
}
740-
return value;
741-
}
742-
743730
/**
744731
* @param {string[]} argv
745732
* @returns {{ base: string; head: string; mergeHeadFirstParent: boolean }}
@@ -748,12 +735,12 @@ export function parseArgs(argv) {
748735
const args = { base: "", head: "HEAD", mergeHeadFirstParent: false };
749736
for (let i = 0; i < argv.length; i += 1) {
750737
if (argv[i] === "--base") {
751-
args.base = readRefValue(argv, i, "--base");
738+
args.base = requireOptionArgument(argv, i, "--base");
752739
i += 1;
753740
continue;
754741
}
755742
if (argv[i] === "--head") {
756-
args.head = readRefValue(argv, i, "--head");
743+
args.head = requireOptionArgument(argv, i, "--head");
757744
i += 1;
758745
continue;
759746
}

‎scripts/code-mode-model-matrix.ts‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type QaEvidenceSummaryJson,
1717
} from "../extensions/qa-lab/api.js";
1818
import type { AgentExecEnvelope } from "../src/commands/agent-exec.ts";
19+
import { requireOptionArgument } from "./lib/arg-utils.mts";
1920
import { previewForDevToolLog, redactJsonValueForDevToolLog } from "./lib/dev-tooling-safety.ts";
2021

2122
export { validateQaEvidenceSummaryJson };
@@ -164,14 +165,6 @@ Provider credentials are read from the environment and are never written to arti
164165
`;
165166
}
166167

167-
function readOptionValue(argv: readonly string[], index: number, flag: string): string {
168-
const value = argv[index + 1];
169-
if (!value || value.startsWith("-")) {
170-
throw new Error(`${flag} requires a value`);
171-
}
172-
return value;
173-
}
174-
175168
function parseIntegerOption(raw: string, flag: string, max?: number): number {
176169
if (!/^\d+$/u.test(raw)) {
177170
throw new Error(`${flag} must be a positive integer`);
@@ -230,7 +223,7 @@ export function parseCodeModeMatrixOptions(
230223
for (let index = 0; index < argv.length; index += 1) {
231224
const arg = argv[index];
232225
if (arg === "--model") {
233-
const value = readOptionValue(argv, index, arg).trim();
226+
const value = requireOptionArgument(argv, index, arg).trim();
234227
if (!value.includes("/")) {
235228
throw new Error(
236229
`--model must use a provider/model reference; got ${JSON.stringify(value)}`,
@@ -241,36 +234,40 @@ export function parseCodeModeMatrixOptions(
241234
continue;
242235
}
243236
if (arg === "--mode") {
244-
collectUnique(modes, parseMode(readOptionValue(argv, index, arg)), arg);
237+
collectUnique(modes, parseMode(requireOptionArgument(argv, index, arg)), arg);
245238
index += 1;
246239
continue;
247240
}
248241
if (arg === "--task") {
249-
collectUnique(tasks, parseTask(readOptionValue(argv, index, arg)), arg);
242+
collectUnique(tasks, parseTask(requireOptionArgument(argv, index, arg)), arg);
250243
index += 1;
251244
continue;
252245
}
253246
if (arg === "--repetitions") {
254247
recordOnce(arg);
255-
repetitions = parseIntegerOption(readOptionValue(argv, index, arg), arg, MAX_REPETITIONS);
248+
repetitions = parseIntegerOption(
249+
requireOptionArgument(argv, index, arg),
250+
arg,
251+
MAX_REPETITIONS,
252+
);
256253
index += 1;
257254
continue;
258255
}
259256
if (arg === "--timeout") {
260257
recordOnce(arg);
261-
timeoutSeconds = parseIntegerOption(readOptionValue(argv, index, arg), arg);
258+
timeoutSeconds = parseIntegerOption(requireOptionArgument(argv, index, arg), arg);
262259
index += 1;
263260
continue;
264261
}
265262
if (arg === "--thinking") {
266263
recordOnce(arg);
267-
thinking = readOptionValue(argv, index, arg).trim();
264+
thinking = requireOptionArgument(argv, index, arg).trim();
268265
index += 1;
269266
continue;
270267
}
271268
if (arg === "--output-dir") {
272269
recordOnce(arg);
273-
outputDir = readOptionValue(argv, index, arg);
270+
outputDir = requireOptionArgument(argv, index, arg);
274271
index += 1;
275272
continue;
276273
}

‎scripts/debug-claude-usage.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from "node:path";
77
import { pathToFileURL } from "node:url";
88
import { expectDefined } from "../packages/normalization-core/src/expect.js";
99
import { normalizeOptionalString } from "../packages/normalization-core/src/string-coerce.js";
10+
import { requireOptionArgument } from "./lib/arg-utils.mts";
1011
import { readBoundedResponseText as readBoundedResponseTextWithLimit } from "./lib/bounded-response.mjs";
1112
import {
1213
maskIdentifier,
@@ -47,7 +48,7 @@ const parseArgs = (args = process.argv.slice(2)): Args => {
4748
for (let i = 0; i < args.length; i++) {
4849
const arg = expectDefined(args[i], `Claude usage argument at index ${i}`);
4950
if (arg === "--agent") {
50-
agentId = parseNonBlankArgValue(parseRequiredArgValue(args, i, "--agent"), "--agent");
51+
agentId = parseNonBlankArgValue(requireOptionArgument(args, i, "--agent"), "--agent");
5152
i += 1;
5253
continue;
5354
}
@@ -65,7 +66,7 @@ const parseArgs = (args = process.argv.slice(2)): Args => {
6566
}
6667
if (arg === "--session-key") {
6768
sessionKey = parseNonBlankArgValue(
68-
parseRequiredArgValue(args, i, "--session-key"),
69+
requireOptionArgument(args, i, "--session-key"),
6970
"--session-key",
7071
);
7172
i += 1;
@@ -84,14 +85,6 @@ const parseArgs = (args = process.argv.slice(2)): Args => {
8485
return { agentId, help, reveal, sessionKey };
8586
};
8687

87-
function parseRequiredArgValue(args: string[], index: number, label: string): string {
88-
const value = args[index + 1];
89-
if (!value || value.startsWith("-")) {
90-
throw new Error(`${label} requires a value`);
91-
}
92-
return value;
93-
}
94-
9588
function parseInlineArgValue(arg: string, label: string): string {
9689
const value = arg.slice(`${label}=`.length);
9790
if (!value) {

‎scripts/dependency-ownership-surface-report.mts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from "node:path";
77
import { pathToFileURL } from "node:url";
88
import { isRecord } from "@openclaw/normalization-core/record-coerce";
99
import { parse as parseYaml } from "yaml";
10+
import { requireOptionArgument } from "./lib/arg-utils.mts";
1011
import { pnpmLockfileDocuments } from "./lib/pnpm-lockfile-documents.mjs";
1112
import { collectRootDependencyOwnershipAudit } from "./root-dependency-ownership-audit.mts";
1213

@@ -475,14 +476,6 @@ function printTextReport(report: DependencyOwnershipReport) {
475476
process.stdout.write(renderDependencyOwnershipSurfaceMarkdownReport(report));
476477
}
477478

478-
function readArtifactPath(argv: string[], index: number, optionName: string) {
479-
const value = argv[index + 1];
480-
if (value === undefined || value === "" || value.startsWith("-")) {
481-
throw new Error(`${optionName} requires a value`);
482-
}
483-
return value;
484-
}
485-
486479
export function parseArgs(argv: string[]): ParseOptions {
487480
const options: ParseOptions = {
488481
asJson: false,
@@ -521,7 +514,7 @@ export function parseArgs(argv: string[]): ParseOptions {
521514
continue;
522515
}
523516
if (arg === "--markdown") {
524-
setOnce(arg, "markdownPath", readArtifactPath(argv, index, arg));
517+
setOnce(arg, "markdownPath", requireOptionArgument(argv, index, arg));
525518
index += 1;
526519
continue;
527520
}

‎scripts/docs-sync-publish.mjs‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import fs from "node:fs";
66
import path from "node:path";
77
import { pathToFileURL } from "node:url";
88
import { renderDocsHeadingMap } from "./docs-list.js";
9+
import { requireOptionArgument } from "./lib/arg-utils.runtime.mjs";
910
import { repairMintlifyAccordionIndentation } from "./lib/mintlify-accordion.mjs";
1011
import { resolveRepoRoot } from "./lib/repo-root.mjs";
1112

@@ -29,6 +30,10 @@ const SYNC_SUPPORT_FILES = [
2930
source: path.join(ROOT, "scripts", "check-docs-mdx.mts"),
3031
target: path.join(".openclaw-sync", "check-docs-mdx.mts"),
3132
},
33+
{
34+
source: path.join(ROOT, "scripts", "lib", "arg-utils.runtime.mjs"),
35+
target: path.join(".openclaw-sync", "lib", "arg-utils.runtime.mjs"),
36+
},
3237
{
3338
source: path.join(ROOT, "scripts", "lib", "tsx-cli-shim.mjs"),
3439
target: path.join(".openclaw-sync", "lib", "tsx-cli-shim.mjs"),
@@ -201,14 +206,6 @@ const GENERATED_LOCALES = [
201206
},
202207
];
203208

204-
function readOptionValue(argv, index, optionName) {
205-
const value = argv[index + 1];
206-
if (value === undefined || value === "" || value.startsWith("-")) {
207-
throw new Error(`${optionName} requires a value`);
208-
}
209-
return value;
210-
}
211-
212209
export function parseArgs(argv) {
213210
const args = {
214211
target: "",
@@ -224,27 +221,27 @@ export function parseArgs(argv) {
224221
const part = argv[index];
225222
switch (part) {
226223
case "--target":
227-
args.target = readOptionValue(argv, index, part);
224+
args.target = requireOptionArgument(argv, index, part);
228225
index += 1;
229226
break;
230227
case "--source-repo":
231-
args.sourceRepo = readOptionValue(argv, index, part);
228+
args.sourceRepo = requireOptionArgument(argv, index, part);
232229
index += 1;
233230
break;
234231
case "--source-sha":
235-
args.sourceSha = readOptionValue(argv, index, part);
232+
args.sourceSha = requireOptionArgument(argv, index, part);
236233
index += 1;
237234
break;
238235
case "--clawhub-repo":
239-
args.clawhubRepo = readOptionValue(argv, index, part);
236+
args.clawhubRepo = requireOptionArgument(argv, index, part);
240237
index += 1;
241238
break;
242239
case "--clawhub-source-repo":
243-
args.clawhubSourceRepo = readOptionValue(argv, index, part);
240+
args.clawhubSourceRepo = requireOptionArgument(argv, index, part);
244241
index += 1;
245242
break;
246243
case "--clawhub-source-sha":
247-
args.clawhubSourceSha = readOptionValue(argv, index, part);
244+
args.clawhubSourceSha = requireOptionArgument(argv, index, part);
248245
index += 1;
249246
break;
250247
default:

0 commit comments

Comments
 (0)