@@ -138,6 +138,89 @@ and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
138
138
> obj
139
139
{ bar: 'baz' }
140
140
141
+ ## Class: REPLServer
142
+
143
+ This inherits from [ Readline Interface] [ ] with the following events:
144
+
145
+ ### Event: 'exit'
146
+
147
+ ` function () {} `
148
+
149
+ Emitted when the user exits the REPL in any of the defined ways. Namely, typing
150
+ ` .exit ` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
151
+ to signal "end" on the ` input ` stream.
152
+
153
+ Example of listening for ` exit ` :
154
+
155
+ replServer.on('exit', function () {
156
+ console.log('Got "exit" event from repl!');
157
+ process.exit();
158
+ });
159
+
160
+
161
+ ### Event: 'reset'
162
+
163
+ ` function (context) {} `
164
+
165
+ Emitted when the REPL's context is reset. This happens when you type ` .clear ` .
166
+ If you start the repl with ` { useGlobal: true } ` then this event will never
167
+ be emitted.
168
+
169
+ Example of listening for ` reset ` :
170
+
171
+ // Extend the initial repl context.
172
+ var replServer = repl.start({ options ... });
173
+ someExtension.extend(r.context);
174
+
175
+ // When a new context is created extend it as well.
176
+ replServer.on('reset', function (context) {
177
+ console.log('repl has a new context');
178
+ someExtension.extend(context);
179
+ });
180
+
181
+ ### replServer.defineCommand(keyword, cmd)
182
+
183
+ * ` keyword ` {String}
184
+ * ` cmd ` {Object|Function}
185
+
186
+ Makes a command available in the REPL. The command is invoked by typing a ` . `
187
+ followed by the keyword. The ` cmd ` is an object with the following values:
188
+
189
+ - ` help ` - help text to be displayed when ` .help ` is entered (Optional).
190
+ - ` action ` - a function to execute, potentially taking in a string argument,
191
+ when the command is invoked, bound to the REPLServer instance (Required).
192
+
193
+ If a function is provided instead of an object for ` cmd ` , it is treated as the
194
+ ` action ` .
195
+
196
+ Example of defining a command:
197
+
198
+ // repl_test.js
199
+ var repl = require('repl');
200
+
201
+ var replServer = repl.start();
202
+ replServer.defineCommand('sayhello', {
203
+ help: 'Say hello',
204
+ action: function(name) {
205
+ this.write('Hello, ' + name + '!\n');
206
+ this.displayPrompt();
207
+ }
208
+ });
209
+
210
+ Example of invoking that command from the REPL:
211
+
212
+ > .sayhello Node.js User
213
+ Hello, Node.js User!
214
+
215
+ ### replServer.displayPrompt([ preserveCursor] )
216
+
217
+ * ` preserveCursor ` {Boolean}
218
+
219
+ Like [ readline.prompt] [ ] except also adding indents with ellipses when inside
220
+ blocks. The ` preserveCursor ` argument is passed to [ readline.prompt] [ ] . This is
221
+ used primarily with ` defineCommand ` . It's also used internally to render each
222
+ prompt line.
223
+
141
224
## repl.start(options)
142
225
143
226
Returns and starts a ` REPLServer ` instance, that inherits from
@@ -240,89 +323,6 @@ a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
240
323
For an example of running a REPL instance over ` curl(1) ` ,
241
324
see: https://gist.github.com/2053342
242
325
243
- ## Class: REPLServer
244
-
245
- This inherits from [ Readline Interface] [ ] with the following events:
246
-
247
- ### Event: 'exit'
248
-
249
- ` function () {} `
250
-
251
- Emitted when the user exits the REPL in any of the defined ways. Namely, typing
252
- ` .exit ` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
253
- to signal "end" on the ` input ` stream.
254
-
255
- Example of listening for ` exit ` :
256
-
257
- replServer.on('exit', function () {
258
- console.log('Got "exit" event from repl!');
259
- process.exit();
260
- });
261
-
262
-
263
- ### Event: 'reset'
264
-
265
- ` function (context) {} `
266
-
267
- Emitted when the REPL's context is reset. This happens when you type ` .clear ` .
268
- If you start the repl with ` { useGlobal: true } ` then this event will never
269
- be emitted.
270
-
271
- Example of listening for ` reset ` :
272
-
273
- // Extend the initial repl context.
274
- var replServer = repl.start({ options ... });
275
- someExtension.extend(r.context);
276
-
277
- // When a new context is created extend it as well.
278
- replServer.on('reset', function (context) {
279
- console.log('repl has a new context');
280
- someExtension.extend(context);
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
326
[ Readline Interface ] : readline.html#readline_class_interface
327
327
[ readline.prompt ] : readline.html#readline_rl_prompt_preservecursor
328
328
[ util.inspect() ] : util.html#util_util_inspect_object_options
0 commit comments