@cli-schema/spec

中文文档
0.2.0发布于 2026/07/14MITTypeScriptES Module构建来源已验证近 30 天下载 9043

TypeScript types, meta-schema, and validator for the CLI Schema specification (https://github.com/cli-schema/cli-schema)

原始使用文档
文档目录10 节
  1. Quickstart
  2. Purpose
  3. Features
  4. Usage
  5. Building a document by hand
  6. Validating an untrusted document
  7. Reading the raw meta-schema
  8. Deriving intent from an HTTP verb
  9. Related packages
  10. License

@cli-schema/spec

TypeScript types, the vendored meta-schema, and a validator for the CLI Schema specification v1 — the open standard for describing command-line interfaces in a language-agnostic, machine-readable way.

This is the only package in the cli-schema-js monorepo with no CLI-framework dependency. It defines the shape of a CLI Schema document; @cli-schema/commander is what actually produces one from a real CLI.

Quickstart

npm install @cli-schema/spec
import { validate, SCHEMA_VERSION, type CliSchema } from '@cli-schema/spec'

const doc: CliSchema = {
  schemaVersion: SCHEMA_VERSION,
  name: 'mytool',
  version: '1.0.0',
}

const result = validate(doc)
if (!result.valid) throw new Error(JSON.stringify(result.errors, null, 2))

That's the minimum: a document only needs schemaVersion, name, and version to be valid — everything else (commands, parameters, environment, intent, ...) is optional enrichment.

Purpose

CLI help text is written for humans, completion scripts are hand-authored per shell, and AI agents guess flags. CLI Schema fixes this by defining one JSON document format that describes a program's commands, parameters, intent, auth requirements, environment dependencies, and output capabilities — so agents, IDEs, shell-completion generators, and docs tooling can all read the same document instead of re-deriving it.

@cli-schema/spec is the compile-time and runtime contract for that format: the types you build a document against, and the validator you check it with before trusting it.

Features

  • Full v1 type coverage — every object in the spec has a corresponding TypeScript interface: CliSchema (root), Command, Namespace, Parameter, Constraint, Intent, Output, Environment, EnvVar, ConfigFile, DefaultHandler, Shortcut, Deprecation.
  • The normative meta-schema, vendored — schema/cli-schema.meta-schema.json from the spec repo, re-exported at @cli-schema/spec/meta-schema.json for tools that want the raw JSON Schema directly (e.g. to validate in a non-JS pipeline, or feed to a codegen tool).
  • A ready-to-use validator — validate() / assertValid(), backed by ajv's 2020-12 dialect support. No need to wire up ajv yourself.
  • inferIntentFromHttp — a small pure helper for CLIs that wrap HTTP APIs: derives a reasonable {@link Intent} from an HTTP verb (GET/HEAD → safe & idempotent, PUT → idempotent, DELETE → destructive & idempotent, POST/PATCH → unset, since their semantics are endpoint-specific).
  • Vendor extensions typed — every object that permits x- prefixed fields (spec §14) is typed to accept them ([extension: \x-${string}`]: unknown), so x-my-tool: {...}type-checks withoutas any`.

Usage

Building a document by hand

The types double as documentation of the format — reach for the spec itself for full field semantics, but a well-populated document looks like:

import type { CliSchema } from '@cli-schema/spec'

const doc: CliSchema = {
  schemaVersion: 1,
  name: 'gh',
  version: '2.45.0',
  description: 'GitHub CLI — bring GitHub to your terminal',
  requiresAuth: true,
  authCommands: ['auth login', 'auth logout'],
  reservedMetaCommands: ['__schema'],
  commands: [
    {
      name: 'status',
      summary: 'Print information about relevant issues, pull requests, and notifications',
      intent: { destructive: false, idempotent: true, requiresAuth: true },
    },
  ],
  namespaces: [
    {
      segment: 'repo',
      commands: [
        {
          name: 'delete',
          path: ['repo'],
          parameters: [
            { role: 'positional', name: 'repository', type: 'string', required: true },
            { role: 'confirmationSkip', name: 'yes', type: 'boolean', required: false },
          ],
          intent: { destructive: true, idempotent: true, scope: 'global', requiresConfirmation: true },
        },
      ],
    },
  ],
}

In practice you'll usually generate this from a live CLI rather than write it by hand — see @cli-schema/commander.

Validating an untrusted document

Per spec §15, consumers should treat schema documents as untrusted input:

import { validate, assertValid } from '@cli-schema/spec'

const { valid, errors } = validate(untrustedDoc)
// errors is an ajv ErrorObject[] — empty when valid

assertValid(untrustedDoc) // throws with a formatted message if invalid

Reading the raw meta-schema

import metaSchema from '@cli-schema/spec/meta-schema.json' with { type: 'json' }

Useful if you want to validate documents with your own ajv instance, a different validator entirely, or a non-JS tool.

Deriving intent from an HTTP verb

import { inferIntentFromHttp } from '@cli-schema/spec'

inferIntentFromHttp('DELETE') // { destructive: true, idempotent: true, scope: 'global' }
inferIntentFromHttp('POST')   // undefined — annotate explicitly, semantics vary per endpoint

License

MIT

版本与发布记录

@cli-schema/spec 共发布 3 个版本,首次发布于 2026/07/14;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/07/143 个月前
0.1.12026/07/143 个月前
0.1.02026/07/143 个月前

关于 @cli-schema/spec 的常见问题

@cli-schema/spec 是什么?

TypeScript types, meta-schema, and validator for the CLI Schema specification (https://github.com/cli-schema/cli-schema)

如何安装 @cli-schema/spec?

在项目目录运行 npm install @cli-schema/spec,或使用 pnpm add / yarn add / bun add。

@cli-schema/spec 支持 TypeScript 吗?

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

@cli-schema/spec 支持 ESM 还是 CommonJS?

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

@cli-schema/spec 可以商用吗?

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

@cli-schema/spec 的下载量有多少?

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

@cli-schema/spec 还在维护吗?

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