replServer.defineCommand()

replServer.defineCommand(keyword, cmd)

Makes a command available in the REPL. The command is invoked by typing a . followed by the keyword. The cmd is an object with the following values:

  • help - help text to be displayed when .help is entered (Optional).
  • action - a function to execute, potentially taking in a string argument, when the command is invoked, bound to the REPLServer instance (Required).

If a function is provided instead of an object for cmd, it is treated as the action.

Example of defining a command:

// repl_test.js
const repl = require('repl');

var replServer = repl.start();
replServer.defineCommand('sayhello', {
  help: 'Say hello',
  action: function(name) {
    this.write(`Hello, ${name}!\n`);
    this.displayPrompt();
  }
});

Example of invoking that command from the REPL:

> .sayhello Node.js User
Hello, Node.js User!
doc_Nodejs
2016-04-30 04:42:06
Comments
Leave a Comment

Please login to continue.