Skip to content

Quickly Start

axios edited this page Jan 26, 2021 · 3 revisions

Start application

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();

Run single command

app.register(require('path/to/your/command/file'));
app.exec("<command-name>");

Use locales

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

Command 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', ...],
      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;

Clone this wiki locally