|
| 1 | +# @axiosleo/cli-tool |
| 2 | + |
| 3 | +[English](/README.md) | 简体中文 |
| 4 | + |
| 5 | +[](https://npmjs.org/package/@axiosleo/cli-tool) |
| 6 | +[](https://npmjs.org/package/@axiosleo/cli-tool) |
| 7 | +[](https://github.com/AxiosLeo/node-cli/actions/workflows/ci.yml) |
| 8 | +[](https://codecov.io/gh/AxiosLeo/node-cli) |
| 9 | +[](LICENSE) |
| 10 | +[](https://app.fossa.com/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli/refs/branch/master) |
| 11 | + |
| 12 | +> 为了使用 Node.js 快速开发命令行应用而设计 |
| 13 | +> |
| 14 | +> 详见文档 [wiki](https://github.com/AxiosLeo/node-cli/wiki) |
| 15 | +
|
| 16 | +## 安装 |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @axiosleo/cli-tool |
| 20 | +``` |
| 21 | + |
| 22 | +## 快速初始化应用 |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install @axiosleo/cli-tool -g |
| 26 | + |
| 27 | +cli-tool init <app-name> |
| 28 | + |
| 29 | +# 生成命令行脚本文件 |
| 30 | +cli-tool make <command-name> <commands-dir-path> |
| 31 | +# 示例 |
| 32 | +cli-tool make test ./commands/ # 将会生成 ./commands/test.js 文件 |
| 33 | + |
| 34 | +# 直接执行生成的命令行脚本文件 |
| 35 | +cli-tool exec ./command/test.js |
| 36 | +``` |
| 37 | + |
| 38 | +## 使用 |
| 39 | + |
| 40 | +### 应用入口程序 |
| 41 | + |
| 42 | +```js |
| 43 | +const { App } = require('@axiosleo/cli-tool'); |
| 44 | +const app = new App({ |
| 45 | + name: 'cli-tool', // 命令行应用的名称, 必须 |
| 46 | + desc: 'cli app description', |
| 47 | + version: '1.0.0', // 命令行应用的版本, 必须 |
| 48 | + commands_dir: '/path/to/commands/dir/', // 讲会自动加载目录内的命令行脚本文件 |
| 49 | + commands_sort: ['help', ... ], |
| 50 | + commands_group: { |
| 51 | + 'group description': ['command_name', ...], |
| 52 | + } |
| 53 | +}); |
| 54 | +app.start(); |
| 55 | +``` |
| 56 | + |
| 57 | +### 执行单个命令 |
| 58 | + |
| 59 | +- 通过 Command 类注册命令 |
| 60 | + |
| 61 | +```js |
| 62 | +const CommandExample = require('/path/to/your/command/file'); |
| 63 | +app.register(CommandExample); |
| 64 | + |
| 65 | +app.exec("<command-name>"); |
| 66 | +``` |
| 67 | + |
| 68 | +- 通过 Command 对象注册命令 |
| 69 | + |
| 70 | +```js |
| 71 | +const CommandExample = require('/path/to/your/command/file'); |
| 72 | +const command = new CommandExample(); |
| 73 | +app.register(command); |
| 74 | + |
| 75 | +app.exec("<command-name>"); |
| 76 | +``` |
| 77 | + |
| 78 | +- 通过 Command 文件地址注册命令 |
| 79 | + |
| 80 | +```js |
| 81 | +app.register('/path/to/your/command/file'); |
| 82 | + |
| 83 | +app.exec("<command-name>"); |
| 84 | +``` |
| 85 | + |
| 86 | +## 多语言 |
| 87 | + |
| 88 | +> 开启多语言模式后,应用和命令的“描述”将会根据配置的 locales json 文件,自动翻译成对应语言 |
| 89 | +> |
| 90 | +> locales 示例 json 文件 : [locales](./locales) |
| 91 | +> |
| 92 | +> 详见 [locales wiki](https://github.com/AxiosLeo/node-cli/wiki/locales) |
| 93 | +
|
| 94 | +```js |
| 95 | +const path = require('path'); |
| 96 | +app.locale({ |
| 97 | + dir: path.join(__dirname, '../locales'), // /path/to/app/locales/dir |
| 98 | + sets: ['en-US', 'zh-CN'], // 不能为空, 且第一个元素为默认值 |
| 99 | +}); |
| 100 | +app.start(); // 需要在调用 app.start() 方法前配置 locale |
| 101 | +``` |
| 102 | + |
| 103 | +### 命令类示例 |
| 104 | + |
| 105 | +```js |
| 106 | +'use strict'; |
| 107 | + |
| 108 | +const { Command } = require('@axiosleo/cli-tool'); |
| 109 | + |
| 110 | +class CommandExample extends Command { |
| 111 | + constructor() { |
| 112 | + super({ |
| 113 | + name: 'command-name', |
| 114 | + desc: 'command desc', |
| 115 | + alias: ['command-alia1', 'command-alia2'], |
| 116 | + }); |
| 117 | + |
| 118 | + /** |
| 119 | + * add argument of current command |
| 120 | + * @param name argument name |
| 121 | + * @param desc argument description |
| 122 | + * @param mode argument mode : required | optional |
| 123 | + * @param default_value only supported on optional mode |
| 124 | + */ |
| 125 | + this.addArgument('arg-name', 'desc', 'required', null); |
| 126 | + |
| 127 | + /** |
| 128 | + * add option of current command |
| 129 | + * @param name option name |
| 130 | + * @param short option short name |
| 131 | + * @param desc option description |
| 132 | + * @param mode option mode : required | optional |
| 133 | + * @param default_value only supported on optional mode |
| 134 | + */ |
| 135 | + this.addOption('test', 't', 'desc', 'required', null); |
| 136 | + } |
| 137 | + |
| 138 | + async exec(args, options, argList, app) { |
| 139 | + // do something in here |
| 140 | + |
| 141 | + // get arg&option by name |
| 142 | + const arg1 = args.argName; |
| 143 | + const option1 = options.optionName; |
| 144 | + |
| 145 | + // get arg by index |
| 146 | + const index = 0; |
| 147 | + const arg2 = argList[index]; |
| 148 | + |
| 149 | + // ask for answer |
| 150 | + const answer = await this.ask('Please input your answer'); |
| 151 | + |
| 152 | + // ask for confirm, default value is 'false' |
| 153 | + const confirm = await this.confirm('Confirm do this now?', false); |
| 154 | + |
| 155 | + // select action |
| 156 | + const action = await this.select('Select an action', ['info', 'update']); |
| 157 | + |
| 158 | + // print table |
| 159 | + const rows = [ |
| 160 | + ['Bob', 2] |
| 161 | + ]; |
| 162 | + const head = ['Name', 'Score']; |
| 163 | + this.table(rows, head); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +module.exports = CommandExample; |
| 168 | +``` |
| 169 | + |
| 170 | +## 许可证 |
| 171 | + |
| 172 | +本项目基于 [MIT](LICENSE) 开源协议. |
| 173 | + |
| 174 | +[](https://app.fossa.com/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli/refs/branch/master/) |
0 commit comments