2.0.2 • Published 6 months ago

@aiot-toolkit/commander v2.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

commander

仅需配置即可生成命令行工具,支持

  1. 默认命令
  2. 命令参数
  3. 询问式参数
  4. 常驻式操作

询问式参数与命令参数的关系

  • 询问式参数是普通参数的子集,询问式参数一定可以直接在命令中输入,以确保工具可自动化运行
  • 某参数在命令中填写后,不会在询问中出现

快速开始

  1. 创建配置内容
// 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)
  1. 执行命令
  • 执行 ts-node bin.ts start --path /Documents/temp
  • 此时,会询问 path2 的值;输入后,进入 action,输出 option
  • 命令执行完成后,会显示常驻操作菜单

配置

IProgram

属性描述类型必填
name工具名称stringtrue
description工具描述stringtrue
version版本号stringtrue
defaultCommand默认命令,无命令名称时,执行此命令ICommandfalse
commandList命令列表ICommand[]false

ICommand

属性描述类型必填
name命令名称stringtrue
description命令描述stringtrue
argumentList无名称参数, tool arg1 arg2string[]false
paramList有名称参数ParamTypefalse
action命令的执行函数,action:(arg1, arg2, options)=>{}true

ParamType

ParamType 分为4类,继承自IParam

IParam

参数基础结构

属性描述类型必填
name参数名称stringtrue
description参数描述stringtrue
defaultValue默认值anyfalse
enableInquirer是否启用交互式询问booleanfalse

1. InputParam

文本式参数,在IParam的基础上,增加

属性描述类型必填
type固定值 string'string'true
validate校验输入合法性的方法Functionfalse

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常驻命令描述stringtrue
options常驻命令列表IPersistentCommandItem[]true

IPersistentCommandItem

属性描述类型必填
key按键,只能是一个字符stringtrue
description描述stringtrue
action按键执行的方法() => Promise, ()=>voidtrue
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-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