English | 简体中文
为了使用 Node.js 快速开发命令行应用而设计
详见文档 wiki
npm install @axiosleo/cli-tool
npm install @axiosleo/cli-tool -g
cli-tool init <app-name>
# 生成命令行脚本文件
cli-tool make <command-name> <commands-dir-path>
# 示例
cli-tool make test ./commands/ # 将会生成 ./commands/test.js 文件
# 直接执行生成的命令行脚本文件
cli-tool exec ./command/test.js
const { App } = require('@axiosleo/cli-tool');
const app = new App({
name: 'cli-tool', // 命令行应用的名称, 必须
desc: 'cli app description',
version: '1.0.0', // 命令行应用的版本, 必须
commands_dir: '/path/to/commands/dir/', // 将会自动加载目录内的命令行脚本文件
commands_sort: ['help', ... ],
commands_group: {
'group description': ['command_name', ...],
}
});
app.start();
- 通过 Command 类注册命令
const CommandExample = require('/path/to/your/command/file');
app.register(CommandExample);
app.exec("<command-name>");
- 通过 Command 对象注册命令
const CommandExample = require('/path/to/your/command/file');
const command = new CommandExample();
app.register(command);
app.exec("<command-name>");
- 通过 Command 文件地址注册命令
app.register('/path/to/your/command/file');
app.exec("<command-name>");
开启多语言模式后,应用和命令的“描述”将会根据配置的 locales json 文件,自动翻译成对应语言
locales 示例 json 文件 : locales
详见 locales wiki
const path = require('path');
app.locale({
dir: path.join(__dirname, '../locales'), // /path/to/app/locales/dir
sets: ['en-US', 'zh-CN'], // 不能为空, 且第一个元素为默认值
});
app.start(); // 需要在调用 app.start() 方法前配置 locale
'use strict';
const { Command } = require('@axiosleo/cli-tool');
class CommandExample extends Command {
constructor() {
super({
name: 'command-name',
desc: 'command desc',
alias: ['command-alia1', 'command-alia2'],
});
/**
* add argument of current command
* @param name argument name
* @param desc argument description
* @param mode argument mode : required | optional
* @param default_value only supported on optional mode
*/
this.addArgument('arg-name', 'desc', 'required', null);
/**
* add option of current command
* @param name option name
* @param short option short name
* @param desc option description
* @param mode option mode : required | optional
* @param default_value only supported on optional mode
*/
this.addOption('test', 't', 'desc', 'required', null);
}
async exec(args, options, argList, app) {
// do something in here
// get arg&option by name
const arg1 = args.argName;
const option1 = options.optionName;
// get arg by index
const index = 0;
const arg2 = argList[index];
// ask for answer
const answer = await this.ask('Please input your answer');
// ask for confirm, default value is 'false'
const confirm = await this.confirm('Confirm do this now?', false);
// select action
const action = await this.select('Select an action', ['info', 'update']);
// print table
const rows = [
['Bob', 2]
];
const head = ['Name', 'Score'];
this.table(rows, head);
}
}
module.exports = CommandExample;
本项目基于 MIT 开源协议.