Home Intro Source Mebo GitHub
import Cli from 'mebo/src/Handlers/Cli.js'
public class | source

Cli

Extends:

Handler → Cli

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).

See:

Static Method Summary

Static Public Methods
public static

init(options: Object)

Initializes a registered action as cli.

public static

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

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 process.argv.

public

setArgs(value: Array<string>)

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

create(handlerName: string, mask: string, args: ...args): Handler

Creates a handler based on the registered name

public static

get(handlerName: string, handlerMask: string): Handler

Returns the registered handler

public static

grantAction(handlerName: string, actionName: string, args: ...args)

Grants the execution of the action through the handler

public static

Returns a list granted actions for the input handler

public static

Adds a listener to an exception raised during the Handler.output process.

public static

register(handlerClass: Handler, handlerName: string, handlerMask: string)

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

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

meta(path: string, defaultValue: *): *

Returns a value under the handler's metadata.

public

output(value: *, options: Object, finalizeSession: boolean)

Results a value through the handler.

public

async runAction(actionName: string, options: Object): *

Executes an action through the handler.

public

Returns the session

public

setMeta(path: string, value: *, merge: boolean)

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:

NameTypeAttributeDescription
options Object

plain object containing custom options

options.argv Array<string>
  • optional

custom list of arguments, if not specified it uses the process.argv (this information is passed to the creation of cli handler)

options.stdout stream
  • optional

custom writable stream, if not specified it uses process.stdout (this information is passed to the creation of cli handler)

options.stderr stream
  • optional

custom writable stream, if not specified it uses process.stderr (this information is passed to the creation of cli handler)

options.finalizeCallback function
  • optional

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.

Params:

NameTypeAttributeDescription
argv Array<string>
  • optional

custom list of arguments, if not specified it uses the process.argv (this information is passed to the creation of cli handler)

Return:

boolean

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#_grantingAction

Params:

NameTypeAttributeDescription
handlerName string

registered handler name

actionName string

registered action name

options object

custom options passed during Handler.grantAction

options.command string
  • optional

command name used to initialize the cli, otherwise if not defined the actionName is used instead

protected static actionCommands(actionName: string): Array<string> source

Return a list of commands mapped to the action name.

Params:

NameTypeAttributeDescription
actionName string

registered action name

Return:

Array<string>

Public Constructors

public constructor(argv: Array<string>, stdout: stream, stderr: stream) source

Creates an cli handler

Override:

Handler#constructor

Params:

NameTypeAttributeDescription
argv Array<string>

argument list

stdout stream

stream used as stdout

stderr stream

stream used as stderr

Public Methods

public args(): Array<string> source

Returns a list of argument values used by the reader, by default it uses process.argv.

Return:

Array<string>

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

Params:

NameTypeAttributeDescription
value Array<string>

argument list

public setStderr(value: stream) source

Sets the stderr stream

Params:

NameTypeAttributeDescription
value stream

stream used as stderr

public setStdout(value: stream) source

Sets the stdout stream

Params:

NameTypeAttributeDescription
value stream

stream used as stdout

public stderr(): stream source

Returns the stream used as stderr

Return:

stream

public stdout(): stream source

Returns the stream used as stdout

Return:

stream

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#_createReader

Params:

NameTypeAttributeDescription
action Action

action instance used by the reader to parse the values

options Object

plain object containing the options passed to the reader

Return:

Reader

protected _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#_createWriter

Params:

NameTypeAttributeDescription
value *

arbitrary value passed to the writer

options Object

plain object containing the options passed to the writer

Return:

Writer