-
Notifications
You must be signed in to change notification settings - Fork 4
Quickly Start
axios edited this page Jan 26, 2021
·
3 revisions
const { App } = require('@axiosleo/cli-tool');
const app = new App({
name: 'cli', // cli app command name
desc: 'cli app description',
version: '1.0.0',
commands_dir: '/path/to/commands/dir/', // will auto load command files
commands_sort: ['help', ... ],
commands_group: {
'group description': ['command_name', ...],
}
});
app.start();
// or
app.register(require('/path/to/your/command/file'))
// ... ...
.register(require('/path/to/your/other-command/file'));
app.run();app.register(require('path/to/your/command/file'));
app.exec("<command-name>");The "desc" will be automatically translated by using the locales json file.
locales example json file : locales
const path = require('path');
app.locale({
dir: path.join(__dirname, '../locales'), // /path/to/app/locales/dir
sets: ['en-US', 'zh-CN'], // cannot be empty, the first set as default.
});
app.start(); // set locale before start app'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', ...],
args: [
{
name: 'name', // argument name
mode: 'optional', // required | optional
desc: 'arg desc',
default: null // only supported optional mode
}
],
options: [
{
name: 'name', // option name
short: 'n', // like 'n'
mode: 'optional', // required | optional
desc: 'option desc',
default: null // only supported optional mode
}
],
});
}
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 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);
}
}
module.exports = CommandExample;Anything unclear or inaccurate? Please let me know at [email protected]