Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.3.1 - 2026-11-15

### Fixed

- `--json` 且无命令时只向 stderr 输出 `{ error }`,不再向 stdout 写 usage,与“`--json` 错误只写 stderr”的契约一致。

## 0.3.0 - 2026-11-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ultime/cli",
"version": "0.3.0",
"version": "0.3.1",
"description": "Ultime · cli:通用命令行设施(参数 / 命令注册 / 上下文 / 退出码),零领域依赖",
"keywords": [
"argument-parser",
Expand Down
13 changes: 12 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,18 @@ export async function runCli(spec: CliSpec, argv: string[], ctx: CliContext = pr
return EXIT_OK
}
if (!args.command) {
ctx.out(spec.usage)
if (json) {
reportError(
ctx,
new CliError('缺少命令', {
code: 'USAGE',
hint: `可用命令:${Object.keys(spec.commands).join(' / ')}`,
}),
true,
)
} else {
ctx.out(spec.usage)
}
return EXIT_USAGE
}

Expand Down
10 changes: 10 additions & 0 deletions tests/infra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ describe('cli · 运行时', () => {
expect(out).toEqual(['demo <command>'])
})

it('--json 下无命令只写 stderr', async () => {
const { ctx, err, out } = collector()
expect(await runCli(spec, ['--json'], ctx)).toBe(2)
expect(out).toEqual([])
const payload = JSON.parse(err.join('\n')) as { error: { code: string; message: string; hint?: string } }
expect(payload.error.code).toBe('USAGE')
expect(payload.error.message).toBe('缺少命令')
expect(payload.error.hint).toContain('ping')
})

it('未知命令返回 2,并给最近候选', async () => {
const { ctx, err, out } = collector()
expect(await runCli(spec, ['statsu'], ctx)).toBe(2)
Expand Down
Loading