# @aiot-toolkit/commander

> A tool that generates complete commands with configuration

Latest version **2.0.5** (published 2025-07-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install @aiot-toolkit/commander
pnpm add @aiot-toolkit/commander
yarn add @aiot-toolkit/commander
bun add @aiot-toolkit/commander
```

## Health

**Score 35/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2025-07-23 |
| First published | 2023-12-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | yinhunfeixue, lewiscutey, yaotaiyang, xiaobin06, bluestoneq, susanpan, dongwa |
| Keywords | aiot, commander |

## Links

- npm: https://www.npmjs.com/package/@aiot-toolkit/commander
- npm.io page: https://npm.io/package/@aiot-toolkit/commander

## Dependencies (4)

- [chalk](https://npm.io/package/chalk.md) ^4.0.0
- [commander](https://npm.io/package/commander.md) ^11.0.0
- [@inquirer/prompts](https://npm.io/package/@inquirer/prompts.md) ^3.0.3
- [@aiot-toolkit/shared-utils](https://npm.io/package/@aiot-toolkit/shared-utils.md) 2.0.5

## Recent versions

- 2.0.5 (latest) — 2025-07-23
- 2.1.0-prender.13 (prender) — 2026-08-25
- 2.0.6-beta.28 (beta) — 2026-07-22
- 2.1.0-prenderv1.1 (prenderv1) — 2026-07-15
- 2.0.6-alpha.1 (alpha) — 2025-08-03
- 2.0.2-batchmanifest-beta.1 (dev) — 2024-05-28
- 2.1.0-prender.12 — 2026-07-24
- 2.1.0-prender.11 — 2026-07-20
- 2.1.0-prender.10 — 2026-07-17
- 2.1.0-prender.9 — 2026-07-17
- 2.1.0-prender.8 — 2026-07-10
- 2.1.0-prender.7 — 2026-07-09
- 2.1.0-prender.6 — 2026-06-30
- 2.1.0-prender.5 — 2026-06-29
- 2.1.0-2.1.0-prender.5.0 — 2026-06-29
- … 124 more at https://npm.io/package/@aiot-toolkit/commander/versions

## README

# `commander`

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

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

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

- 询问式参数是普通参数的子集，询问式参数一定可以直接在命令中输入，以确保工具可自动化运行

* 某参数在命令中填写后，不会在询问中出现

## 快速开始

1. 创建配置内容

```typescript
// 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)
```

2. 执行命令

- 执行 `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 |
| deprecated        | 废弃的提示信息， 如果有值，表示此参数已废弃 | string  |       |
| deprecatedVersion | 废弃的版本号                                | string  |       |

#### 1. InputParam

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

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

#### 2. SelectParam

单选参数，在 IParam 的基础上，增加

| 属性    | 描述          | 类型                                                                                        | 必填 |
| ------- | ------------- | ------------------------------------------------------------------------------------------- | ---- |
| type    | 固定值 select | 'select'                                                                                    | true |
| choices | 选项列表      | {<br/>value:选项值（必填）, <br/> name:选项名称(可选),<br/>description: 描述(可选）<br/>}[] | 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>, ()=>void | true |

---
_Source: https://npm.io/package/@aiot-toolkit/commander · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
