English | 简体中文

Design for quickly developing CLI applications using Node.js
See detail usage from wiki
Installation
npm install @axiosleo/cli-tool
Quickly initialize application
npm install @axiosleo/cli-tool -g
cli-tool init <app-name>
# make command file
cli-tool make <command-name> <commands-dir-path>
# for example
cli-tool make test ./commands/ # will generate command file on ./commands/test.js
# run command js script
cli-tool exec ./commands/test.js
Usage
Start application
const { App } = require('@axiosleo/cli-tool');
const app = new App({
name: 'cli-tool',
version: '1.0.0',
desc: 'cli app description',
commands_dir: '/path/to/commands/dir/',
commands_sort: ['help', ... ],
commands_group: {
'group description': ['command_name', ...],
}
});
app.start();
Run single command
- register with command class
const CommandExample = require("/path/to/your/command/file");
app.register(CommandExample);
app.exec("<command-name>");
- register with command object
const CommandExample = require("/path/to/your/command/file");
const command = new CommandExample();
app.register(command);
app.exec("<command-name>");
- register with command file path
app.register("/path/to/your/command/file");
app.exec("<command-name>");
Use locales
The “desc” of CLI Application and Command will be automatically translated by using the locales json file.
The detected system locale is used when it is listed in sets; otherwise the first item is used. On Windows only the LC_ALL, LC_MESSAGES, LANG and LANGUAGE environment variables are read, and en-US is used when none of them is set.
locales example json file : locales
see detail from locales wiki
const path = require("path");
app.locale({
dir: path.join(__dirname, "../locales"),
sets: ["en-US", "zh-CN"],
});
app.start();
Command Class Example
"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"],
});
this.addArgument("arg-name", "desc", "required", null);
this.addOption("test", "t", "desc", "required", null);
}
async exec(args, options, argList, app) {
const arg1 = args.argName;
const option1 = options.optionName;
const index = 0;
const arg2 = argList[index];
const answer = await this.ask("Please input your answer");
const confirm = await this.confirm("Confirm do this now?", false);
const action = await this.select("Select an action", ["info", "update"]);
const rows = [["Bob", 2]];
const head = ["Name", "Score"];
this.table(rows, head);
}
}
module.exports = CommandExample;
License
This project is open-sourced software licensed under MIT.

评论