Skip to content

Commit 1afec5a

Browse files
lishengzxcclaude
andcommitted
refactor: infer flags type from options definition in defineCommand
Generify CommandSpec and defineCommand so the `run` callback's `flags` parameter is typed as `GlobalFlags & InferFlags<Options>`, where InferFlags derives typed properties from the command's `options` array. This removes ~150 manual `as string` / `as number` / `as string[]` type assertions across 46 command files. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 60cad10 commit 1afec5a

47 files changed

Lines changed: 248 additions & 308 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/cli/src/args.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,12 @@ export function parseFlags(argv: string[], options: OptionDef[]): GlobalFlags {
147147
);
148148
}
149149

150-
// Switch-style flags (--quiet, --dry-run): no value. Value flags need a non-flag next token.
151150
if (schema.booleans.has(camelKey)) {
152151
(flags as Record<string, unknown>)[camelKey] = true;
153152
i++;
154153
continue;
155154
}
156155

157-
// --prompt <text>, --watermark <bool>, …
158156
if (value === undefined) {
159157
i++;
160158
const next = argv[i];

packages/cli/src/commands/advisor/recommend.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {
22
analyzeIntent,
33
buildDocLink,
4-
type Config,
54
defineCommand,
65
detectOutputFormat,
76
type GetModelsOptions,
8-
type GlobalFlags,
97
getModels,
108
type IntentProfile,
119
isInteractive,
@@ -241,9 +239,9 @@ export default defineCommand({
241239
'bl advisor recommend --message "Long document summarization" --dry-run',
242240
"bl advisor recommend # Interactive input",
243241
],
244-
async run(config: Config, flags: GlobalFlags) {
242+
async run(config, flags) {
245243
const positional = ((flags as Record<string, unknown>)._positional as string[]) ?? [];
246-
let userInput = (flags.message as string) || positional.join(" ");
244+
let userInput = flags.message || positional.join(" ");
247245

248246
if (!userInput.trim()) {
249247
if (isInteractive({ nonInteractive: config.nonInteractive })) {

packages/cli/src/commands/app/call.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
appCompletionEndpoint,
66
parseSSE,
77
detectOutputFormat,
8-
type Config,
9-
type GlobalFlags,
108
type AppCompletionRequest,
119
type AppStreamChunk,
1210
type AppCompletionResponse,
@@ -42,11 +40,11 @@ export default defineCommand({
4240
'bl app call --app-id abc123 --prompt "搜索资料" --pipeline-ids pipe1,pipe2',
4341
'bl app call --app-id abc123 --prompt "开始" --biz-params \'{"key":"value"}\'',
4442
],
45-
async run(config: Config, flags: GlobalFlags) {
46-
const appId = flags.appId as string;
43+
async run(config, flags) {
44+
const appId = flags.appId;
4745
if (!appId) failIfMissing("app-id", "bl app call --app-id <id> --prompt <text>");
4846

49-
const prompt = flags.prompt as string;
47+
const prompt = flags.prompt;
5048
if (!prompt) failIfMissing("prompt", "bl app call --app-id <id> --prompt <text>");
5149

5250
const shouldStream =
@@ -61,17 +59,17 @@ export default defineCommand({
6159
};
6260

6361
if (flags.sessionId) {
64-
body.input.session_id = flags.sessionId as string;
62+
body.input.session_id = flags.sessionId;
6563
}
6664

6765
// Pass image URLs via image_list
68-
const imageUrls = flags.image as string[] | undefined;
66+
const imageUrls = flags.image;
6967
if (imageUrls && imageUrls.length > 0) {
7068
body.input.image_list = imageUrls;
7169
}
7270

7371
// Pass pre-uploaded file IDs
74-
const fileIds = flags.fileId as string[] | undefined;
72+
const fileIds = flags.fileId;
7573
if (fileIds && fileIds.length > 0) {
7674
body.input.file_ids = fileIds;
7775
}
@@ -81,20 +79,20 @@ export default defineCommand({
8179
}
8280

8381
if (flags.pipelineIds) {
84-
const ids = (flags.pipelineIds as string)
82+
const ids = flags.pipelineIds
8583
.split(",")
8684
.map((s) => s.trim())
8785
.filter(Boolean);
8886
body.parameters!.rag_options = { pipeline_ids: ids };
8987
}
9088

9189
if (flags.memoryId) {
92-
body.parameters!.memory_id = flags.memoryId as string;
90+
body.parameters!.memory_id = flags.memoryId;
9391
}
9492

9593
if (flags.bizParams) {
9694
try {
97-
body.input.biz_params = JSON.parse(flags.bizParams as string);
95+
body.input.biz_params = JSON.parse(flags.bizParams);
9896
} catch {
9997
process.stderr.write("Error: --biz-params must be valid JSON\n");
10098
process.exit(1);

packages/cli/src/commands/app/list.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
callConsoleGateway,
44
resolveConsoleGatewayCredential,
55
detectOutputFormat,
6-
type Config,
7-
type GlobalFlags,
86
} from "bailian-cli-core";
97
import { emitResult } from "../../output/output.ts";
108

@@ -46,10 +44,10 @@ export default defineCommand({
4644
"bl app list --page 2 --page-size 10",
4745
"bl app list --output json",
4846
],
49-
async run(config: Config, flags: GlobalFlags) {
50-
const name = (flags.name as string) || "";
51-
const pageNo = (flags.page as number) || 1;
52-
const pageSize = (flags.pageSize as number) || 30;
47+
async run(config, flags) {
48+
const name = flags.name || "";
49+
const pageNo = flags.page || 1;
50+
const pageSize = flags.pageSize || 30;
5351
const format = detectOutputFormat(config.output);
5452

5553
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/auth/login.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {
44
maskToken,
55
readConfigFile,
66
writeConfigFile,
7-
type Config,
8-
type GlobalFlags,
97
} from "bailian-cli-core";
108
import { printQuickStart } from "../../output/banner.ts";
119
import { emitBare } from "../../output/output.ts";
@@ -34,7 +32,7 @@ export default defineCommand({
3432
},
3533
],
3634
examples: ["bl auth login --api-key sk-xxxxx", "bl auth login --console"],
37-
async run(config: Config, flags: GlobalFlags) {
35+
async run(config, flags) {
3836
if (flags.console) {
3937
if (config.dryRun) {
4038
emitBare(
@@ -66,13 +64,13 @@ export default defineCommand({
6664
}
6765
}
6866

69-
const key = (flags.apiKey as string) || config.apiKey;
67+
const key = flags.apiKey || config.apiKey;
7068
if (!key) {
7169
printCurrentCommandHelp(process.stderr);
7270
process.exit(0);
7371
}
7472

75-
const baseUrl = (flags.baseUrl as string) || undefined;
73+
const baseUrl = flags.baseUrl || undefined;
7674
const effectiveConfig = baseUrl ? { ...config, baseUrl } : config;
7775

7876
if (!config.dryRun) {

packages/cli/src/commands/auth/logout.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {
44
readConfigFile,
55
writeConfigFile,
66
getConfigPath,
7-
type Config,
8-
type GlobalFlags,
97
} from "bailian-cli-core";
108
import { emitBare } from "../../output/output.ts";
119

@@ -35,7 +33,7 @@ export default defineCommand({
3533
"bl auth logout --dry-run",
3634
"bl auth logout --yes",
3735
],
38-
async run(config: Config, flags: GlobalFlags) {
36+
async run(config, flags) {
3937
const file = readConfigFile();
4038

4139
if (flags.console) {

packages/cli/src/commands/auth/status.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
detectOutputFormat,
66
maskToken,
77
type Config,
8-
type GlobalFlags,
98
type ResolvedCredential,
109
} from "bailian-cli-core";
1110
import { emitResult, emitBare } from "../../output/output.ts";
@@ -155,7 +154,7 @@ export default defineCommand({
155154
},
156155
],
157156
examples: ["bl auth status", "bl auth status --output json"],
158-
async run(config: Config, _flags: GlobalFlags) {
157+
async run(config, _flags) {
159158
const format = detectOutputFormat(config.output);
160159
const status = await buildStatus(config);
161160

packages/cli/src/commands/config/export-schema.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { defineCommand, generateToolSchema } from "bailian-cli-core";
2-
import type { Config } from "bailian-cli-core";
3-
import type { GlobalFlags } from "bailian-cli-core";
42
import { BailianError } from "bailian-cli-core";
53
import { ExitCode } from "bailian-cli-core";
64

@@ -21,9 +19,9 @@ export default defineCommand({
2119
},
2220
],
2321
examples: ["bl config export-schema", 'bl config export-schema --command "video generate"'],
24-
async run(config: Config, flags: GlobalFlags) {
22+
async run(config, flags) {
2523
const { commands } = await import("../catalog.ts");
26-
const targetCommand = flags.command as string | undefined;
24+
const targetCommand = flags.command;
2725

2826
if (targetCommand) {
2927
const command = commands[targetCommand];

packages/cli/src/commands/config/set.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
readConfigFile,
66
writeConfigFile,
77
BailianError,
8-
type Config,
9-
type GlobalFlags,
108
ExitCode,
119
} from "bailian-cli-core";
1210
import { emitResult } from "../../output/output.ts";
@@ -67,9 +65,9 @@ export default defineCommand({
6765
"bl config set --key timeout --value 600",
6866
"bl config set --key base_url --value https://dashscope.aliyuncs.com",
6967
],
70-
async run(config: Config, flags: GlobalFlags) {
71-
const key = flags.key as string | undefined;
72-
const value = flags.value as string | undefined;
68+
async run(config, flags) {
69+
const key = flags.key;
70+
const value = flags.value;
7371

7472
if (!key || value === undefined) {
7573
throw new BailianError(

packages/cli/src/commands/config/show.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {
44
getConfigPath,
55
detectOutputFormat,
66
maskToken,
7-
type Config,
8-
type GlobalFlags,
97
} from "bailian-cli-core";
108
import { emitResult } from "../../output/output.ts";
119

@@ -14,7 +12,7 @@ export default defineCommand({
1412
description: "Display current configuration",
1513
usage: "bl config show",
1614
examples: ["bl config show", "bl config show --output json"],
17-
async run(config: Config, _flags: GlobalFlags) {
15+
async run(config, _flags) {
1816
const file = loadConfigFile();
1917
const format = detectOutputFormat(config.output);
2018

0 commit comments

Comments
 (0)