@@ -138,10 +138,6 @@ and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
138
138
> obj
139
139
{ bar: 'baz' }
140
140
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
-
145
141
## repl.start(options)
146
142
147
143
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
244
240
For an example of running a REPL instance over ` curl(1) ` ,
245
241
see: https://gist.github.com/2053342
246
242
243
+ ## Class: REPLServer
244
+
245
+ This inherits from [ Readline Interface] [ ] with the following events:
246
+
247
247
### Event: 'exit'
248
248
249
249
` function () {} `
@@ -254,7 +254,7 @@ to signal "end" on the `input` stream.
254
254
255
255
Example of listening for ` exit ` :
256
256
257
- r .on('exit', function () {
257
+ replServer .on('exit', function () {
258
258
console.log('Got "exit" event from repl!');
259
259
process.exit();
260
260
});
@@ -271,11 +271,59 @@ be emitted.
271
271
Example of listening for ` reset ` :
272
272
273
273
// Extend the initial repl context.
274
- var r = repl.start({ options ... });
274
+ var replServer = repl.start({ options ... });
275
275
someExtension.extend(r.context);
276
276
277
277
// When a new context is created extend it as well.
278
- r .on('reset', function (context) {
278
+ replServer .on('reset', function (context) {
279
279
console.log('repl has a new context');
280
280
someExtension.extend(context);
281
281
});
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