Skip to content

Commit ae2d1ee

Browse files
benglFishrock123
authored andcommitted
doc: repl: add defineComand and displayPrompt
Also some minor edits so the additions make sense. PR-URL: #3765 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 84bb745 commit ae2d1ee

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

doc/api/repl.markdown

+55-7
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
138138
> obj
139139
{ bar: 'baz' }
140140

141-
[Readline Interface]: readline.html#readline_class_interface
142-
[util.inspect()]: util.html#util_util_inspect_object_options
143-
[here]: util.html#util_custom_inspect_function_on_objects
144-
145141
## repl.start(options)
146142

147143
Returns and starts a `REPLServer` instance, that inherits from
@@ -244,6 +240,10 @@ a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
244240
For an example of running a REPL instance over `curl(1)`,
245241
see: https://gist.github.com/2053342
246242

243+
## Class: REPLServer
244+
245+
This inherits from [Readline Interface][] with the following events:
246+
247247
### Event: 'exit'
248248

249249
`function () {}`
@@ -254,7 +254,7 @@ to signal "end" on the `input` stream.
254254

255255
Example of listening for `exit`:
256256

257-
r.on('exit', function () {
257+
replServer.on('exit', function () {
258258
console.log('Got "exit" event from repl!');
259259
process.exit();
260260
});
@@ -271,11 +271,59 @@ be emitted.
271271
Example of listening for `reset`:
272272

273273
// Extend the initial repl context.
274-
var r = repl.start({ options ... });
274+
var replServer = repl.start({ options ... });
275275
someExtension.extend(r.context);
276276

277277
// When a new context is created extend it as well.
278-
r.on('reset', function (context) {
278+
replServer.on('reset', function (context) {
279279
console.log('repl has a new context');
280280
someExtension.extend(context);
281281
});
282+
283+
### replServer.defineCommand(keyword, cmd)
284+
285+
* `keyword` {String}
286+
* `cmd` {Object|Function}
287+
288+
Makes a command available in the REPL. The command is invoked by typing a `.`
289+
followed by the keyword. The `cmd` is an object with the following values:
290+
291+
- `help` - help text to be displayed when `.help` is entered (Optional).
292+
- `action` - a function to execute, potentially taking in a string argument,
293+
when the command is invoked, bound to the REPLServer instance (Required).
294+
295+
If a function is provided instead of an object for `cmd`, it is treated as the
296+
`action`.
297+
298+
Example of defining a command:
299+
300+
// repl_test.js
301+
var repl = require('repl');
302+
303+
var replServer = repl.start();
304+
replServer.defineCommand('sayhello', {
305+
help: 'Say hello',
306+
action: function(name) {
307+
this.write('Hello, ' + name + '!\n');
308+
this.displayPrompt();
309+
}
310+
});
311+
312+
Example of invoking that command from the REPL:
313+
314+
> .sayhello Node.js User
315+
Hello, Node.js User!
316+
317+
### replServer.displayPrompt([preserveCursor])
318+
319+
* `preserveCursor` {Boolean}
320+
321+
Like [readline.prompt][] except also adding indents with ellipses when inside
322+
blocks. The `preserveCursor` argument is passed to [readline.prompt][]. This is
323+
used primarily with `defineCommand`. It's also used internally to render each
324+
prompt line.
325+
326+
[Readline Interface]: readline.html#readline_class_interface
327+
[readline.prompt]: readline.html#readline_rl_prompt_preservecursor
328+
[util.inspect()]: util.html#util_util_inspect_object_options
329+
[here]: util.html#util_custom_inspect_function_on_objects

0 commit comments

Comments
 (0)