replServer.defineCommand(keyword, cmd)
-
keyword
<String> -
cmd
<Object> | <Function>
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:
1 2 3 4 5 6 7 8 9 10 11 | // 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:
1 2 | > .sayhello Node.js User Hello, Node.js User! |
Please login to continue.