Cli
Extends:
Handles the command-line integration based on docopt specification.
It enables the execution of actions triggered as command-line applications. The args are parsed using the reader CliArgs and the output is provided by writer CliOutput.
Using cli handler:
Creating an action that can be executed through command-line
@Mebo.grant('cli')
@Mebo.register('myAction')
class MyAction extends Mebo.Action{
constructor(){
super();
this.setMeta('description', 'Welcome!');
this.createInput('myArgument: text', {elementType: 'argument', description: 'my argument'});
this.createInput('myOption: bool', {description: 'my option'});
}
async _perform(data){
const result = {
myArgument: data.myArgument,
myOption: data.myOption,
};
return result;
}
}
The command-line support can be invoked in two ways:
1) Multiple commands (recommended): Used to provide multiple granted actions through command-line
if (require.main === module) {
const cli = Mebo.Handler.get('cli');
if (cli.isSupported()){
cli.init();
}
}
When this method is used the command-line help (-h
or --help
)
provides a list of commands:
node mycli.js --help
__ __ _
| \/ | ___| |__ ___
| |\/| |/ _ \ '_ \ / _ \_
| | | | __/ |_) | (_) |
|_| |_|\___|_.__/ \___/
Available commands:
myAction
In order to access the help for each command you need to provide
the command name before the help flag (-h
or --help
)
node . myAction --help
Welcome.
Usage: node mycli.js myAction [options] <my-argument>
Arguments:
my-argument my argument (text type).
Options:
--my-option my option (bool type).
A complete example about providing multiple commands through command-line can be found at: https://github.com/meboHQ/example-cli
2) Single command: Used to provide just a single granted action through command-line
if (require.main === module) {
// creating an cli handler which is used to load the arguments
// arguments to the action and to output the result back to the stream
const cli = Mebo.Handler.create('cli');
// loading the parsed information to the action
cli.runAction('myAction').then((result) => {
// success output
cli.output(result);
// error output
}).catch((err) => {
cli.output(err);
});
}
When using the single command the help flag (-h
or --help
)
provides the help about the command directly:
node mycli.js --help
Welcome.
Usage: node mycli.js [options] <my-argument>
Arguments:
my-argument my argument (text type).
Options:
--my-option my option (bool type).
Static Method Summary
Static Public Methods | ||
public static |
Initializes a registered action as cli. |
|
public static |
isSupported(argv: Array<string>): boolean Returns a boolean telling if cli support can be initialized based on the input argv. |
Static Protected Methods | ||
protected static |
_grantingAction(handlerName: string, actionName: string, options: object) This method can be re-implemented by derived classes to hook when an Action is granted for a handler (Handler.grantAction) |
|
protected static |
actionCommands(actionName: string): Array<string> Return a list of commands mapped to the action name. |
Constructor Summary
Public Constructor | ||
public |
constructor(argv: Array<string>, stdout: stream, stderr: stream) Creates an cli handler |
Method Summary
Public Methods | ||
public |
Returns a list of argument values used by the reader, by default it uses
|
|
public |
Sets a list of argument values used by the reader. |
|
public |
setStderr(value: stream) Sets the stderr stream |
|
public |
setStdout(value: stream) Sets the stdout stream |
|
public |
stderr(): stream Returns the stream used as stderr |
|
public |
stdout(): stream Returns the stream used as stdout |
Protected Methods | ||
protected |
_createReader(action: Action, options: Object): Reader Creates an instance of a reader for the current handler. |
|
protected |
_createWriter(value: *, options: Object): Writer Creates an instance of a writer for the current handler |
Inherited Summary
From class Handler | ||
public static |
Removes all listeners connected to the signal emitted through Handler.onErrorDuringOutput. |
|
public static |
Creates a handler based on the registered name |
|
public static |
Returns the registered handler |
|
public static |
grantAction(handlerName: string, actionName: string, args: ...args) Grants the execution of the action through the handler |
|
public static |
grantedActionNames(handlerName: string): Array<string> Returns a list granted actions for the input handler |
|
public static |
onErrorDuringOutput(listener: function) Adds a listener to an exception raised during the Handler.output process. |
|
public static |
Register an Handler type to the available handlers |
|
public static |
registerReader(readerClass: Reader, handlerName: string, handlerMask: string) Register a Reader for the handler |
|
public static |
registerWriter(writerClass: Writer, handlerName: string, handlerMask: string) Register a Writer for the handler |
|
public static |
registeredHandler(handlerName: string, handlerMask: string): Handler Returns the registered handler |
|
public static |
registeredHandlerMasks(handlerName: string): Array<string> Returns a list of registered handler makers for the input handler name |
|
public static |
Returns a list containing the names of the registered handler types |
|
public static |
registeredReader(handlerName: string, handlerMask: string): Reader Returns the reader registered for the handler |
|
public static |
registeredWriter(handlerName: string, handlerMask: string): Writer Returns the writer registered for the handler |
|
protected static |
_grantingAction(handlerName: string, actionName: string, args: ...args) This method can be re-implemented by derived classes to hook when an Action is granted for a handler (Handler.grantAction) |
|
public |
Returns a value under the handler's metadata. |
|
public |
Results a value through the handler. |
|
public |
Executes an action through the handler. |
|
public |
Returns the session |
|
public |
Sets a value to the handler's metadata. Detailed information about the metadata support can be found at Metadata. |
|
public |
setSession(session: Session) Associates a Session with the handler. |
|
protected |
_createReader(action: Action, options: Object, additionalArgs: ...additionalArgs): Reader Creates an instance of a reader for the current handler |
|
protected |
_createWriter(value: *, options: Object, additionalArgs: ...additionalArgs): Writer Creates an instance of a writer for the current handler |
Static Public Methods
public static init(options: Object) source
Initializes a registered action as cli.
CliActions/Default.js
: defining an action as cli:
const Mebo = require('mebo');
@Mebo.grant('cli', 'cli.default', {command: 'default'})
@Mebo.register('cli.default')
class Default extends Mebo.Action{
constructor(){
super();
this.createInput('name: text');
this.createInput('myOtherInput?: numeric');
}
async _perform(data){
// ...
}
}
index.js
:
const Mebo = require('mebo');
require('Clis/Default.js');
// ...
const cli = Mebo.Handler.get('cli');
if (cli.isSupported()){
cli.init();
}
Listing available actions:
node myFile.js --help
Showing help from the app:
node myFile.js myCli --help
Executing an cli by specifying custom args:
node myFile.js myCli --arg-a=1 --arg-b=2
Params:
Name | Type | Attribute | Description |
options | Object | plain object containing custom options |
|
options.argv | Array<string> |
|
custom list of arguments, if not specified
it uses the |
options.stdout | stream |
|
custom writable stream, if not specified it uses
|
options.stderr | stream |
|
custom writable stream, if not specified it uses
|
options.finalizeCallback | function |
|
callback executed after the output. (The value the output value is passed as argument) |
public static isSupported(argv: Array<string>): boolean source
Returns a boolean telling if cli support can be initialized based on the input argv.
Static Protected Methods
protected static _grantingAction(handlerName: string, actionName: string, options: object) source
This method can be re-implemented by derived classes to hook when an Action is granted for a handler (Handler.grantAction)
Override:
Handler#_grantingActionParams:
Name | Type | Attribute | Description |
handlerName | string | registered handler name |
|
actionName | string | registered action name |
|
options | object | custom options passed during Handler.grantAction |
|
options.command | string |
|
command name used to initialize the cli, otherwise if not defined the actionName is used instead |
Public Constructors
Public Methods
public args(): Array<string> source
Returns a list of argument values used by the reader, by default it uses
process.argv
.
public setArgs(value: Array<string>) source
Sets a list of argument values used by the reader. It must follow
the same pattern found at process.argv
public setStderr(value: stream) source
Sets the stderr stream
Params:
Name | Type | Attribute | Description |
value | stream | stream used as stderr |
public setStdout(value: stream) source
Sets the stdout stream
Params:
Name | Type | Attribute | Description |
value | stream | stream used as stdout |
Protected Methods
protected _createReader(action: Action, options: Object): Reader source
Creates an instance of a reader for the current handler. This passes the Cli.args to the reader.
Override:
Handler#_createReaderprotected _createWriter(value: *, options: Object): Writer source
Creates an instance of a writer for the current handler
This passes the Cli.stdout and Cli.stderr to the writer.
Override:
Handler#_createWriterParams:
Name | Type | Attribute | Description |
value | * | arbitrary value passed to the writer |
|
options | Object | plain object containing the options passed to the writer |