@clidoc/core

中文文档
1.1.3发布于 2026/11/07MITTypeScriptES Module构建来源已验证近 30 天下载 3231

OpenCLI types, offline validation, and Markdown reference generation for CLI documentation

原始使用文档
文档目录8 节
  1. Getting Started
  2. Where it fits
  3. Why clidoc
  4. Adding author-supplied metadata
  5. Shell completion generation
  6. Contribution
  7. License
  8. Author

clidoc logo


npm package npm downloads coverage discord chat documentation


@clidoc/core

OpenCLI types, offline validation, and Markdown rendering for CLI docs.

  • 📦 Schema Bundled for Offline Use
  • 🔎 JSON and YAML Parsing
  • 📝 Markdown and Page Generation
  • 🧩 Merge Author Metadata
  • 🐚 Completion Script Generation

Parse, validate and render CLI documentation from one portable OpenCLI 1.0.0-alpha.14 document. This is the library underneath every clidoc package: use it directly if you already have an OpenCLI document, or pair it with a framework package to generate one from your CLI.

Read the Docs to Learn More.

Getting Started

npm install @clidoc/core
import { parse, validate, renderMarkdown, generatePages } from '@clidoc/core';

const document = parse(sourceText); // JSON or YAML; throws on invalid input
const result = validate(document); // { valid, errors }
const markdown = renderMarkdown(document);
const pages = generatePages(document, { basePath: '/cli' });

The published package bundles the exact schema pinned in upstream/opencli/spec.schema.json, so validation works offline.

Where it fits

You want toUse
Generate a document from your CLI@clidoc/yargs, @clidoc/commander or @clidoc/oclif
Validate, render Markdown, completions or MCP tools from the shell@clidoc/cli
Publish reference pages in your docs site@clidoc/docusaurus or @clidoc/vitepress

See the API reference and the runnable demos.

Why clidoc

  • Compared to hand-written reference pages: the document comes from the same command definitions your CLI runs, so options and arguments can't drift from --help. Hand-written guides still sit alongside the generated pages.
  • Compared to --help output alone: you get a validated, structured document (JSON/YAML) that also renders to Markdown, shell completions and MCP tool definitions, not just terminal text.
  • Compared to oclif readme: that command targets oclif projects and writes into a README. clidoc's OpenCLI document is framework-neutral, so yargs, Commander and oclif CLIs share one pipeline and one set of docs-site integrations.

generatePages returns a landing page and one page per visible command. Each command page includes a generated usage synopsis: required values use <value>, optional elements use [element], and repeatable elements use .... The synopsis includes visible command flags and inherited global flags. Each page has id, title, path, and Markdown content. A command named <binary> foo-bar keeps the readable /commands/foo-bar route when its suffix is lowercase ASCII letters, digits, and single hyphens (up to 64 characters). Other names use a normalized readable prefix (up to 64 characters), a reserved ~, and a SHA-256 digest of the full command name. Each route depends only on its command name, so adding unrelated commands does not change existing URLs. The generator rejects a digest collision before returning any pages. This replaces the previous collision-dependent hash suffixes, so URLs for names outside the readable form may change. Landing links use the generated base path. The bundled JSON Schema is also available at @clidoc/core/schema.

validate checks documents against the vendored JSON Schema and, once that passes, against the logical rules ported from upstream's validate/validate.go: positional arguments must not place a required argument after an optional one, minItems/maxItems are only valid on variadic arguments and flags (with minItems <= maxItems), $FILE alternative sources must have a matching file declared in global.config, flag names and aliases must be unique per command, and group commands cannot declare args or flags. Errors use the same instancePath message shape as schema errors.

Adding author-supplied metadata

The framework packages generate a document from what your CLI's argument parser already knows, so things like examples, exitCodes, info.license/contact, install, and global config are usually missing. Add them with mergeDocument, then re-validate:

import { mergeDocument } from '@clidoc/core';

const documented = mergeDocument(document, {
  info: { license: { name: 'MIT', spdxId: 'MIT' } },
  install: [{ name: 'npm', command: 'npm i -g my-cli' }],
  commands: {
    'my-cli greet': {
      examples: [{ title: 'Basic', content: 'my-cli greet Ada' }],
      exitCodes: [{ code: 1, status: 'BAD_USER_INPUT_ERROR', summary: 'Missing name' }],
      flags: [{ name: 'language', alternativeSources: [{ type: '$ENV', property: 'LANG' }] }],
    },
  },
});

Plain objects (info, global, each command) merge recursively. Arrays replace the base array, except examples and exitCodes, which append, and flags/args, which are merged item-by-item matched by name — so you can add alternativeSources or a summary to one generated flag without repeating the rest of it. Commands not present in the generated document are added as-is. mergeDocument throws an Error listing every problem if the merged result fails schema validation.

Shell completion generation

generateCompletion(document, { shell: 'bash' | 'zsh' | 'fish', binary?: string }) validates an OpenCLI document and returns a standalone completion script. It supports nested commands, aliases, global/local flags, option and positional choices, and hidden entries, with no runtime dependency on Node or the target CLI. The optional binary overrides only the registered executable name. See the completion guide for activation, supported syntax, and limitations.

Contribution

See Contributing Guide.

License

MIT

Author

Ben Houston, Sponsored by Land of Assets.

版本与发布记录

@clidoc/core 共发布 6 个版本,首次发布于 2026/09/22;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.3latest2026/11/073 天前
1.1.22026/11/036 天前
1.1.12026/10/051 个月前
1.1.02026/09/291 个月前
1.0.02026/09/221 个月前
0.1.02026/09/221 个月前

关于 @clidoc/core 的常见问题

@clidoc/core 是什么?

OpenCLI types, offline validation, and Markdown reference generation for CLI documentation

如何安装 @clidoc/core?

在项目目录运行 npm install @clidoc/core,或使用 pnpm add / yarn add / bun add。

@clidoc/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@clidoc/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@clidoc/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@clidoc/core 可以商用吗?

@clidoc/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@clidoc/core 的下载量有多少?

根据 npm 官方下载统计,@clidoc/core 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,231 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@clidoc/core 还在维护吗?

最新版本 1.1.3 发布于 2026/11/07(3 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/09/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。