From e18233fdcb9a5ea278bd8a687567df404bc5692d Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 11 Mar 2026 13:21:16 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E8=87=B3=200.1.1=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=20JSON=20?= =?UTF-8?q?=E5=92=8C=20key=3Dvalue=20=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/commander.ts | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 2f24491..8906bf0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.1.0", + "version": "0.1.1", "description": "", "type": "module", "main": "./dist/router.js", diff --git a/src/commander.ts b/src/commander.ts index 04f7f76..de23e94 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -22,8 +22,12 @@ export const parseArgs = (args: string) => { for (const pair of pairs) { const idx = pair.indexOf('='); const key = pair.slice(0, idx); - const value = pair.slice(idx + 1); - result[key] = value; + const raw = pair.slice(idx + 1); + let value: string | number | boolean = raw; + if (raw === 'true') value = true; + else if (raw === 'false') value = false; + else if (raw !== '' && !isNaN(Number(raw))) value = Number(raw); + result[key] = value as string; } return result; } @@ -77,8 +81,9 @@ export const createCommand = (opts: { app: App, program: Command }) => { const description = parseDescription(route); subProgram.command(route.key) .description(description || '') - .option('--args ', 'JSON字符串参数,传递给命令执行') - .action(async (options) => { + .option('--args ', '命令参数,支持 JSON 格式或 key=value 形式,例如: --args \'{"a":1}\' 或 --args \'a=1 b=2\'') + .argument('[args...]', '位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- \'{"a":1}\'') + .action(async (passedArgs: string[], options, _command) => { const output = (data: any) => { if (typeof data === 'object') { process.stdout.write(JSON.stringify(data, null, 2) + '\n'); @@ -87,7 +92,12 @@ export const createCommand = (opts: { app: App, program: Command }) => { } } try { - const args = options.args ? parseArgs(options.args) : {}; + let args: Record = {}; + if (options.args) { + args = parseArgs(options.args); + } else if (passedArgs.length > 0) { + args = parseArgs(passedArgs.join(' ')); + } // 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数 const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId }); if (res.code === 200) {