2.0.2 • Published 6 months ago
@aiot-toolkit/commander v2.0.2
commander
仅需配置即可生成命令行工具,支持
- 默认命令
- 命令参数
- 询问式参数
- 常驻式操作
询问式参数与命令参数的关系
- 询问式参数是普通参数的子集,询问式参数一定可以直接在命令中输入,以确保工具可自动化运行
- 某参数在命令中填写后,不会在询问中出现
快速开始
- 创建配置内容
// bin.ts
import { Command, IProgram } from '@aiot-toolkit/commander'
const config: IProgram = {
name: 'myTool',
description: 'myTool is ...',
version: '1.0.1',
// 命令列表
commandList: [
{
// 命令名称
name: 'start',
// 命令描述
description: 'start command is ...',
// 参数列表
paramList: [
{
// 参数名称
name: 'path',
// 参数描述
description: 'path is ...',
// 是否可询问(true 表示未输入时,则询问)
enableInquirer: true
},
{
name: 'path2',
description: 'path2 is ...',
enableInquirer: true
}
],
// 命令对应的方法
action: (option: any) => {
console.log(option)
},
// 常驻式操作
waiter: new PersistentCommand({
description: 'start 的常驻式操作',
options: [
{
// 按键
key: 'a',
description: 'a 的描述',
// 按下 a 执行的方法
action: () => {}
}
]
})
}
]
}
Command.registeProgram(config)
- 执行命令
- 执行
ts-node bin.ts start --path /Documents/temp
- 此时,会询问 path2 的值;输入后,进入 action,输出 option
- 命令执行完成后,会显示常驻操作菜单
配置
IProgram
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
name | 工具名称 | string | true |
description | 工具描述 | string | true |
version | 版本号 | string | true |
defaultCommand | 默认命令,无命令名称时,执行此命令 | ICommand | false |
commandList | 命令列表 | ICommand[] | false |
ICommand
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
name | 命令名称 | string | true |
description | 命令描述 | string | true |
argumentList | 无名称参数, tool arg1 arg2 | string[] | false |
paramList | 有名称参数 | ParamType | false |
action | 命令的执行函数,action:(arg1, arg2, options)=>{} | true |
ParamType
ParamType 分为4类,继承自IParam
IParam
参数基础结构
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
name | 参数名称 | string | true |
description | 参数描述 | string | true |
defaultValue | 默认值 | any | false |
enableInquirer | 是否启用交互式询问 | boolean | false |
1. InputParam
文本式参数,在IParam的基础上,增加
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
type | 固定值 string | 'string' | true |
validate | 校验输入合法性的方法 | Function | false |
2. SelectParam
单选参数,在 IParam 的基础上,增加
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
type | 固定值 select | 'select' | true |
choices | 选项列表 | {value:选项值(必填), name:选项名称(可选),description: 描述(可选)}[] | true |
3. CheckboxParam
多选参数,在SelectParam 基础上,修改
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
type | 固定值 checkbox | 'checkbox' | true |
4. ConfirmParam
boolean 参数,在 IParam 的基础上,增加
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
type | 固定值 confirm | 'confirm' | true |
waiter
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
description | 常驻命令描述 | string | true |
options | 常驻命令列表 | IPersistentCommandItem[] | true |
IPersistentCommandItem
属性 | 描述 | 类型 | 必填 |
---|---|---|---|
key | 按键,只能是一个字符 | string | true |
description | 描述 | string | true |
action | 按键执行的方法 | () => Promise, ()=>void | true |
2.0.3-beta.4
6 months ago
2.0.3-beta.3
6 months ago
2.0.3-beta.2
7 months ago
2.0.3-beta.1
7 months ago
2.0.2
8 months ago
2.0.2-beta.17
8 months ago
2.0.2-beta.16
8 months ago
2.0.2-beta.15
8 months ago
2.0.2-beta.14
8 months ago
2.0.2-beta.19
8 months ago
2.0.2-beta.18
8 months ago
2.0.2-beta.13
10 months ago
2.0.2-beta.12
10 months ago
2.0.2-beta.11
10 months ago
2.0.2-beta.10
12 months ago
2.0.2-batchmanifest-beta.1
12 months ago
2.0.2-beta.9
12 months ago
2.0.2-beta.8
1 year ago
2.0.2-beta.7
1 year ago
2.0.2-beta.6
1 year ago
2.0.2-beta.5
1 year ago
2.0.2-beta.4
1 year ago
2.0.2-beta.2
1 year ago
2.0.2-beta.3
1 year ago
2.0.2-beta.1
1 year ago
2.0.2-dev.8
1 year ago
2.0.2-dev.7
1 year ago
2.0.2-dev.5
1 year ago
2.0.2-dev.6
1 year ago
2.0.2-dev.4
1 year ago
2.0.2-dev.3
1 year ago
2.0.2-dev.2
1 year ago
2.0.2-dev.1
1 year ago
2.0.1-alpha.13
1 year ago
2.0.1-alpha.11
1 year ago
2.0.1-alpha.12
1 year ago
2.0.1-alpha.10
1 year ago
2.0.1-alpha.9
1 year ago
2.0.1-alpha.8
1 year ago
2.0.1-alpha.6
1 year ago
2.0.1-alpha.7
1 year ago
2.0.1-alpha.5
1 year ago
2.0.1-alpha.4
1 year ago
2.0.1-alpha.3
1 year ago
2.0.1-alpha.2
1 year ago
2.0.1-alpha.1
1 year ago
2.0.1-alpha.0
1 year ago