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.5.1 - 2026-11-27

### Changed

- README 改为入口卡:定位、最小示例、出口与边界留在入口;参数声明、默认命令、错误与退出码下沉到 `USAGE.md`。API 与退出码不变。

## 0.5.0 - 2026-11-22

### Added
Expand Down
77 changes: 7 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,78 +47,14 @@ process.exitCode = await runCli(spec, process.argv.slice(2), processContext())

`parseArgs` 只认 `command`、`--flag`、`--key value` / `--key=value`;`-h/--help`、`-v/--version` 与 `--json` 是保留 flag。`--` 之后全部按 positional 处理,`--key ""` 保留空字符串。

## 声明参数
## 文档

给命令加 `flags` 后,参数语义由声明决定,未知参数会在执行前被拒绝:

```ts
commands: {
deploy: {
flags: { 'dry-run': 'boolean', config: 'string' },
run: (args, ctx) => 0,
},
}
```

- `boolean`:永不消费下一个 token,只接受 `--dry-run`;
- `string`:消费下一个 token,支持 `--config path` / `--config=path` / `--config ''`;
- 未声明:传了 schema 时按布尔解析,再由 `runCli` 以 `USAGE` 拒绝;不传 schema 时保持旧的自由解析;
- 同一 flag 在所有命令中的类型必须一致;命令要么全部声明 `flags`,要么全部省略。

## 默认命令

命令面有默认动作时(例如检查工具的 `run`),在 `CliSpec` 设置 `defaultCommand`:

```ts
const spec = {
name: 'my-cli',
version: '0.1.0',
usage: 'my-cli [command]',
defaultCommand: 'run',
commands: {
run: { flags: {}, run: (args, ctx) => 0 },
},
}
```

无命令时执行默认命令;设置默认命令后,未注册的首个 token 也作为它的 positional(显式注册的命令优先),不再报未知命令。有默认命令时 `--` 之后全部作为 positional。`--help` / `--version` 仍走全局短路,不会误执行默认命令。

## 错误与退出码

命令用 `CliError` 抛机器可读错误;普通异常退化成 `code: 'ERROR'`:

```ts
import { CliError } from '@ultime/cli'

throw new CliError('缺少 --config', {
code: 'USAGE',
path: '--config',
hint: '补上 --config <path>',
})
```

| 情况 | 退出码 |
| 文件 | 回答什么 |
|---|---|
| 版本 / 帮助 | 0 |
| 命令返回的码 | 原样透传 |
| 命令抛普通异常 | 1,`code: 'ERROR'` |
| `CliError` | `exitCode`,默认 2(用法或配置错误) |
| 未知命令、未知参数、布尔参数带值、带值参数缺值 | 2 |

`runCli` 是异步的:版本 / 帮助 / 无命令等分支也会返回 `Promise<number>`。

- 未知命令给最近候选;没有候选时列出可用命令。
- `--json` 时错误只写 stderr:

```json
{ "error": { "code": "USAGE", "message": "缺少 --config", "path": "--config", "hint": "补上 --config <path>" } }
```

- 人类文案与 JSON 同源,由 `toErrorShape` 统一转换。
- 文本错误首行显示稳定错误码:`✗ [USAGE] 缺少 --config`;普通异常的 `ERROR` 不显示,避免噪音。`details` 只进 JSON,不在文本里猜结构。
- 颜色由 `CliContext.color` 控制;`processContext()` 按 stderr TTY、`FORCE_COLOR`、`NO_COLOR` / `NODE_DISABLE_COLORS` 与 `TERM` 计算,测试和管道输出默认无色。

AI 入口:[`llms.txt`](./llms.txt)(路由)与 [`SKILLS.md`](./SKILLS.md)(能力表)随包发布。
| 本页 | 定位、最小示例、出口、边界与版本 |
| [USAGE](./USAGE.md) | 参数声明、默认命令、错误与退出码、目录约定 |
| [SKILLS](./SKILLS.md) | AI 可调用的入口、类型与退出码 |
| [llms.txt](./llms.txt) | AI 索引:能力、入口与文档路由 |

## 边界

Expand All @@ -142,6 +78,7 @@ tests/ 参数、运行时、声明校验与包内容边界

## 版本与兼容

- 0.5.1:README 改为入口卡,参数与错误细节下沉到 [USAGE.md](./USAGE.md);API 与退出码不变。
- 当前 `0.5.x`;0.x 阶段 API 仍可能调整。
- 0.5 新增:文本错误显示 `[CODE]`,新增 `CliContext.color` 与 `style` / `supportsColor` 导出;`--json` 结构不变。
- 退出码语义:0 成功 / 1 命令执行失败 / 2 用法或配置错误;这是 0.2 起的新约定,升级时按本节目迁移。
Expand Down
78 changes: 78 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 使用说明

> 定位:`@ultime/cli` 的完整用法:参数声明、默认命令、错误与退出码。
> 适用版本:0.5.x
> 关系:定位与最小示例见 [README](./README.md);AI 入口见 [`SKILLS.md`](./SKILLS.md)。

## 声明参数

给命令加 `flags` 后,参数语义由声明决定,未知参数会在执行前被拒绝:

```ts
commands: {
deploy: {
flags: { 'dry-run': 'boolean', config: 'string' },
run: (args, ctx) => 0,
},
}
```

- `boolean`:永不消费下一个 token,只接受 `--dry-run`;
- `string`:消费下一个 token,支持 `--config path` / `--config=path` / `--config ''`;
- 未声明:传了 schema 时按布尔解析,再由 `runCli` 以 `USAGE` 拒绝;不传 schema 时保持旧的自由解析;
- 同一 flag 在所有命令中的类型必须一致;命令要么全部声明 `flags`,要么全部省略。

## 默认命令

命令面有默认动作时(例如检查工具的 `run`),在 `CliSpec` 设置 `defaultCommand`:

```ts
const spec = {
name: 'my-cli',
version: '0.1.0',
usage: 'my-cli [command]',
defaultCommand: 'run',
commands: {
run: { flags: {}, run: (args, ctx) => 0 },
},
}
```

无命令时执行默认命令;设置默认命令后,未注册的首个 token 也作为它的 positional(显式注册的命令优先),不再报未知命令。有默认命令时 `--` 之后全部作为 positional。`--help` / `--version` 仍走全局短路,不会误执行默认命令。

## 错误与退出码

命令用 `CliError` 抛机器可读错误;普通异常退化成 `code: 'ERROR'`:

```ts
import { CliError } from '@ultime/cli'

throw new CliError('缺少 --config', {
code: 'USAGE',
path: '--config',
hint: '补上 --config <path>',
})
```

| 情况 | 退出码 |
|---|---|
| 版本 / 帮助 | 0 |
| 命令返回的码 | 原样透传 |
| 命令抛普通异常 | 1,`code: 'ERROR'` |
| `CliError` | `exitCode`,默认 2(用法或配置错误) |
| 未知命令、未知参数、布尔参数带值、带值参数缺值 | 2 |

`runCli` 是异步的:版本 / 帮助 / 无命令等分支也会返回 `Promise<number>`。

- 未知命令给最近候选;没有候选时列出可用命令。
- `--json` 时错误只写 stderr:

```json
{ "error": { "code": "USAGE", "message": "缺少 --config", "path": "--config", "hint": "补上 --config <path>" } }
```

- 人类文案与 JSON 同源,由 `toErrorShape` 统一转换。
- 文本错误首行显示稳定错误码:`✗ [USAGE] 缺少 --config`;普通异常的 `ERROR` 不显示,避免噪音。`details` 只进 JSON,不在文本里猜结构。
- 颜色由 `CliContext.color` 控制;`processContext()` 按 stderr TTY、`FORCE_COLOR`、`NO_COLOR` / `NODE_DISABLE_COLORS` 与 `TERM` 计算,测试和管道输出默认无色。

AI 入口:[`llms.txt`](./llms.txt)(路由)与 [`SKILLS.md`](./SKILLS.md)(能力表)随包发布。
17 changes: 16 additions & 1 deletion checks.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ export default {
warn: ['以前|上一版|旧值|下一轮|待补|待实现|未实现|不再用|TODO|FIXME'],
},
},
{ use: 'doc-set', severity: 'error', options: { packages: ['.'] } },
{
use: 'doc-set',
severity: 'error',
options: {
packages: ['.'],
allowRootDocs: [
'README.md',
'USAGE.md',
'AGENTS.md',
'SKILLS.md',
'CHANGELOG.md',
'CONTRIBUTING.md',
'SECURITY.md',
],
},
},
],
}
2 changes: 1 addition & 1 deletion llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## 去哪找

- 怎么用:[README.md](./README.md)
- 怎么用:[README.md](./README.md)(入口)→ [USAGE.md](./USAGE.md)(参数与错误)
- 可调用能力:[SKILLS.md](./SKILLS.md)

## 机器入口
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ultime/cli",
"version": "0.5.0",
"version": "0.5.1",
"description": "Ultime · cli:通用命令行设施(参数 / 命令注册 / 上下文 / 退出码),零领域依赖",
"keywords": [
"argument-parser",
Expand All @@ -23,6 +23,7 @@
"files": [
"dist",
"README.md",
"USAGE.md",
"llms.txt",
"SKILLS.md",
"CHANGELOG.md",
Expand Down Expand Up @@ -58,7 +59,7 @@
"size-limit": "^14.3.0",
"typescript": "^5.9.3",
"unplugin-unused": "^0.6.0",
"vite-plus": "1.6.0"
"vite-plus": "1.8.0"
},
"size-limit": [
{
Expand Down
Loading
Loading