Home Manual Reference Source
import CLICommand from 'projext/src/abstracts/cliCommand.js'
public class | version 1.0 | source

CLICommand

A helper class for creating commands for the CLI.

Constructor Summary

Public Constructor
public abstract

Class constructor.

Member Summary

Public Members
public

Whether or not the command supports unknown options.

public

This is a useful flag for when the command is ran as a result of another command.

public

This is the name of the program that runs the command.

public

The CLI command instruction.

public

A description of the command for the help interface.

public

A more complete description of the command to show when the command help interface is invoked.

public

Whether the command and its description should be shown on the CLI interface list of commands.

public

A list with the name of the options the command supports.

public

A dictionary of command options settings by their option name.

public

Whether or not a sub program should be executed for this command.

Private Members
private

This dictionary will be completed when the command gets activated.

Method Summary

Public Methods
public

addOption(name: string, instruction: string, description: string, defaultValue: string)

Add a new option for the command.

public

Generate an instruction for this command.

public abstract

handle()

Handle the command execution.

public

output(text: string)

A simple wrapper for a console.log.

public

register(program: Command, cli: Object)

Register this command on a CLI program.

Public Constructors

public abstract constructor() source

Class constructor.

Throw:

TypeError

If instantiated directly.

Public Members

public allowUnknownOptions: Boolean source

Whether or not the command supports unknown options. If it does, it will be sent to the onActivation method as a parameter.

public checkOptionsOnParent: boolean source

This is a useful flag for when the command is ran as a result of another command. It lets the interface know that it can search for option values on a parent command, if there's one.

public cliName: string source

This is the name of the program that runs the command. It will be added when the command is registered on the program.

public command: string source

The CLI command instruction. For example my-command [target].

public description: string source

A description of the command for the help interface.

public fullDescription: string source

A more complete description of the command to show when the command help interface is invoked. If left empty, it won't be used.

public hidden: boolean source

Whether the command and its description should be shown on the CLI interface list of commands.

public options: Array source

A list with the name of the options the command supports. New options can be added using the addOption method.

public optionsByName: Object source

A dictionary of command options settings by their option name. New options can be added using the addOption method.

public subProgram: boolean source

Whether or not a sub program should be executed for this command. Take for example the case of git, where git checkout [branch] executes git as main program, and checkout as a sub program. If this is true, then a binary with the name of the command should be exported on the package.json.

Private Members

private _unknownOptions: Object source

This dictionary will be completed when the command gets activated. If the command supports unknown options (allowUnknownOptions), they'll be parsed and sent to the handle method as the last parameter.

Public Methods

public addOption(name: string, instruction: string, description: string, defaultValue: string) source

Add a new option for the command.

Params:

NameTypeAttributeDescription
name string

The option name.

instruction string

The option instruction, for example: -t, --type [type].

description string
  • optional
  • default: ''

The option description.

defaultValue string
  • optional
  • default: ''

The option default value, in case is not used on execution.

Example:

// To capture an option
this.addOption(
  'type',
  '-t, --type [type]',
  'The type of thingy you want to use?',
);

// As a simple flag
this.addOption(
  'ready',
  '-r, --ready',
  'Is it read?',
  false
);

public generate(args: Object): string source

Generate an instruction for this command.

Params:

NameTypeAttributeDescription
args Object
  • optional
  • default: {}

A dictionary with the arguments and options for the command. If the command includes an argument on its command property, that argument is required.

Return:

string

The command instruction to run on the CLI interface.

Example:

// Let's say this command is `destroy [target] [--once]`

this.generate({ target: 'pluto' });
// Will return `destroy pluto`

this.generate({ target: 'moon', once: true });
// Will return `destroy moon --once`

public abstract handle() source

Handle the command execution. This method will receive first the captured arguments, then the executed command information from Commander and finally, a dictionary with the options and their values.

Throw:

Error

if not overwritten.

Example:

// Let's say the command is `run [target] [--production]`.
// And now, it was executed with `run my-target`
handle(target, command, options) {
  console.log(target);
  // Will output `my-target`
  console.log(options.production)
  // Will output `false`
}

public output(text: string) source

A simple wrapper for a console.log. Outputs a variable to the CLI interface.

Params:

NameTypeAttributeDescription
text string

The text to output.

public register(program: Command, cli: Object) source

Register this command on a CLI program.

Params:

NameTypeAttributeDescription
program Command

A Commander instance.

cli Object

The main CLI interface, just for the name.

cli.name string

The CLI interface name.

See: