CLICommand
Direct Subclass:
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 |
Add a new option for the command. |
|
public |
Generate an instruction for this command. |
|
public abstract |
handle() Handle the command execution. |
|
public |
A simple wrapper for a |
|
public |
Register this command on a CLI program. |
Public Constructors
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 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
Public Methods
public addOption(name: string, instruction: string, description: string, defaultValue: string) source
Add a new option for the command.
Params:
Name | Type | Attribute | Description |
name | string | The option name. |
|
instruction | string | The option instruction, for example: |
|
description | string |
|
The option description. |
defaultValue | string |
|
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:
Name | Type | Attribute | Description |
args | Object |
|
A dictionary with the arguments and options for the command. If the
command includes an argument on its |
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:
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:
Name | Type | Attribute | Description |
text | string | The text to output. |