Skip to content

Command Example

axios edited this page Jan 2, 2022 · 2 revisions
'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;
Clone this wiki locally