优雅的 CLI 构建器
util.parseArgs) - 智能解析值,支持类型转换和布尔缩写 - 可嵌套的子命令,支持延迟加载和异步加载 - 可插入和组合的 API,支持自动生成使用说明 - 使用方法 ```shnpx nypm add -D citty
…
sh
node index.mjs john
- 子命令 子命令可以递归嵌套。对于大型 CLI,使用延迟导入来避免一次加载所有命令。js
import { defineCommand, runMain } from "citty";
const sub = defineCommand({
meta: { name: "sub", description: "Sub command" },
args: {
name: { type: "positional", description: "Your name", required: true },
},
run({ args }) {
console.log(Hello ${args.name}!);
},
});
const main = defineCommand({ meta: { name: "hello", version: "1.0.0", description: "My Awesome CLI App" }, subCommands: { sub }, });
runMain(main);
子命令支持 `meta.alias`(例如, `["i", "add"]`) 和 `meta.hidden: true` 来隐藏在帮助输出中。 - 延迟加载命令 对于大型 CLI,延迟加载子命令,以便只导入执行的命令:js
const main = defineCommand({
meta: { name: "hello", version: "1.0.0", description: "My Awesome CLI App" },
subCommands: {
sub: () => import("./sub.mjs").then((m) => m.default),
},
});
`meta`,`args`和`subCommands`都接受`Resolvable`值 - 一个值、Promise、函数或异步函数 - 从而支持延迟和动态解析。暂无开放 Issues,或尚未同步最近议题。