国 际
使用Typescript规范工作国家清单的公用事业.
- 规范子命令、参数和操作方法
- 包括子命令和参数的"前缀匹配",所以用户只需匹配一个子命令或参数即可提供部分名称
- 允许用户以环境变量默认参数
- 自动为用户生成帮助文本
- 提供解析参数的安全响应类型
- 没有依赖关系
- 用于字符串、数字、布尔、enum、数组和地图类型的内置参数类型
- 简单自定义参数类型以创建自己的参数类型
- Async 参数验证和版本检查
- 自动检查更新到您的 CLI
我写这封信是因为我正在构建几个CLI应用程序, 想要对用户可以信赖的参数进行标准解析.
我灵感来自 https://github.com/lirantal/nodejs-cli-apps-best-practices 互联网档案馆的存檔,存档日期2013-12-22 财务报告和审定财务报表 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1 chap12.html 页面存档备份,存于互联网档案馆 遵循接受论据和环境变量的最佳做法,同时使我的范围极为有限,没有依赖性.
目录
文档
核心指南
- 开始 - 为新用户提供完整的辅导
- 分析参考 - 详细解析规则和力学
- 操作指南 - 处理立场辩论和预期操作
- 子命令指南 - 建立有分指令的CLI
- 自定义参数 - 创建有验证的自定义参数类型
- 环境变量 - 环境可变一体化
- 版本处理 - 版本管理和更新检查
- API 参考 - API文档
实例
- 基本国家行动倡议 - 带有版本处理的最小CLI设置
- 参数演示 - 所有参数类型,包括自定义日期参数
- 操作演示 - 处理立场辩论(操作)
- 无操作演示 - 仅接受旗帜和选项的国别学习水平
- 子命令Demo - 具有多个子命令和参数的CLI
- 版本处理 - 版本检查和更新通知
- 从 stdin 读取 - 处理 Stdin 输入
示例
import {
parseCliArguments,
stringArgument,
stringArrayArgument,
booleanArgument,
numberArgument,
enumArgument
} from '@cloud-copilot/cli'
const cli = await parseCliArguments(
'my-command',
// optional subcommands
{
init: {
description: 'Initialize the project',
// Subcommand Specific Arguments
arguments: {
s3: booleanArgument({
description: 'Use S3 as the storage',
character: 's'
}),
language: enumArgument({
description: 'The language to use',
validValues: ['typescript', 'javascript']
})
}
},
download: {
description: 'Download a file',
// Subcommand Specific Arguments
arguments: {
url: stringArgument({
description: 'The URL to download from'
})
}
}
},
// Global Arguments that apply to all subcommands
{
regions: stringArrayArgument({
description: 'Regions to deploy to'
}),
account: stringArgument({
description: 'AWS account to deploy to'
}),
ssl: booleanArgument({
description: 'Enable SSL',
character: 's'
}),
port: numberArgument({
description: 'Port to use'
})
},
{}
)
这将自动从命令行中拉出参数并验证它们 。 如果任何参数无效,它会随错误的信息一起出错. 如果所有参数和子命令都有效,它将返回一个带有类型安全响应的对象.
cli.subcommand // 'init' | 'download' | undefined
cli.args.regions // string[] | undefined
cli.args.account // string | undefined
cli.args.ssl // boolean
cli.args.port // number | undefined
cli.anyValues // boolean, whether any values were provided on the CLI
if (cli.subcommand === 'init') {
// Type Checked command specific arguments only in the return type if the subcommand is selected
cli.args.s3 // boolean
cli.args.language // 'typescript' | 'javascript' | undefined
}
if (cli.subcommand === 'download') {
cli.args.url // string | undefined
}
如果用户提供 --help 将用可用的子命令和参数打印帮助信息.
安装
npm install @cloud-copilot/cli