@brianmd/citty

中文文档
0.0.1发布于 2024/01/16MITTypeScriptESM + CJS近 30 天下载 8.6万

Elegant CLI Builder

原始使用文档
文档目录11 节
  1. Usage
  2. Utils
  3. defineCommand
  4. runMain
  5. createMain
  6. runCommand
  7. parseArgs
  8. renderUsage
  9. showUsage
  10. Development
  11. License

🌆 citty

npm version npm downloads bundle Codecov License

Elegant CLI Builder

  • Fast and lightweight argument parser based on mri
  • Smart value parsing with typecast, boolean shortcuts and unknown flag handling
  • Nested sub-commands
  • Lazy and Async commands
  • Plugable and composable API
  • Auto generated usage and help

🚧 This project is under heavy development. More features are coming soon!

Usage

Install package:

# npm
npm install citty

# yarn
yarn add citty

# pnpm
pnpm install citty

Import:

// ESM
import { defineCommand, runMain } from "citty";

// CommonJS
const { defineCommand, runMain } = require("citty");

Define main command to run:

import { defineCommand, runMain } from "citty";

const main = defineCommand({
  meta: {
    name: "hello",
    version: "1.0.0",
    description: "My Awesome CLI App",
  },
  args: {
    name: {
      type: "positional",
      description: "Your name",
      required: true,
    },
    friendly: {
      type: "boolean",
      description: "Use friendly greeting",
    },
  },
  run({ args }) {
    console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
  },
});

runMain(main);

Utils

defineCommand

defineCommand is a type helper for defining commands.

runMain

Runs a command with usage support and graceful error handling.

createMain

Create a wrapper around command that calls runMain when called.

runCommand

Parses input args and runs command and sub-commands (unsupervised). You can access result key from returnd/awaited value to access command's result.

parseArgs

Parses input arguments and applies defaults.

renderUsage

Renders command usage to a string value.

showUsage

Renders usage and prints to the console

Development

  • Clone this repository
  • Install latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with 💛 Published under MIT License.

Argument parser is based on lukeed/mri by Luke Edwards (@lukeed).

版本与发布记录

@brianmd/citty 共发布 1 个版本,首次发布于 2024/01/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2024/01/162 年前

关于 @brianmd/citty 的常见问题

@brianmd/citty 是什么?

Elegant CLI Builder

如何安装 @brianmd/citty?

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

@brianmd/citty 支持 TypeScript 吗?

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

@brianmd/citty 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@brianmd/citty 可以商用吗?

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

@brianmd/citty 的下载量有多少?

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

@brianmd/citty 还在维护吗?

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