Gitlab NPM Twitter
Kado
High Quality JavaScript Libraries

Command

Introduced in 4.0.0

Stability: 2 - Stable

The Command library provides a set of methods for building and executing a basic CLI utility. Which can be ran programatically through the parse() method.

const { Command } = require('kado')

Class Command

This class is a super class that is extended by the command section within the Application core object. Let us take a look at that usage snippet.

const CommandSuper = require('./Command')
class Command extends CommandSuper {
  constructor () {
    super()
    this.name = name
    this.options = options
    this.version = that.version
  }
  action (opts) {
    return options.action.call(this,opts)
  }
}
this.commands[module][name] = new Command()

When the child Command class is setup it presets the options and version which could be defined in a number of ways.

Now take a look at an actual options object.

const options = {
  description: 'Some command description',
  options: [
    { definition: '-s, --something <s>', description: 'Some thing' }
  ],
  action: (opts) => {
    return Promise.resolve(opts)
  }
}

Command.constructor()

Command.checkLog()

Throws an error when the logger has not been defined.

Command.checkOptions()

Throws an error when the options have not been defined.

Command.checkParser()

Throws an error when the parser has not been defined.

Command.printVersion()

Prints the current version of the program through the log.info() method.

Command.generateHelp()

Command.printHelp()

Uses console.log() to display the output of generateHelp()

Command.action()

Skeleton function meant to be overridden by the actual command and will throw an error if it still exists.

Command.parse(argv)