1 | var format = require('util').format;
|
2 |
|
3 | var common = require('./common');
|
4 |
|
5 | common.register('echo', _echo, {
|
6 | allowGlobbing: false,
|
7 | });
|
8 |
|
9 | //@
|
10 | //@ ### echo([options,] string [, string ...])
|
11 | //@
|
12 | //@ Available options:
|
13 | //@
|
14 | //@ + `-e`: interpret backslash escapes (default)
|
15 | //@ + `-n`: remove trailing newline from output
|
16 | //@
|
17 | //@ Examples:
|
18 | //@
|
19 | //@ ```javascript
|
20 | //@ echo('hello world');
|
21 | //@ var str = echo('hello world');
|
22 | //@ echo('-n', 'no newline at end');
|
23 | //@ ```
|
24 | //@
|
25 | //@ Prints `string` to stdout, and returns string with additional utility methods
|
26 | //@ like `.to()`.
|
27 | function _echo(opts) {
|
28 | // allow strings starting with '-', see issue #20
|
29 | var messages = [].slice.call(arguments, opts ? 0 : 1);
|
30 | var options = {};
|
31 |
|
32 | // If the first argument starts with '-', parse it as options string.
|
33 | // If parseOptions throws, it wasn't an options string.
|
34 | try {
|
35 | options = common.parseOptions(messages[0], {
|
36 | 'e': 'escapes',
|
37 | 'n': 'no_newline',
|
38 | }, {
|
39 | silent: true,
|
40 | });
|
41 |
|
42 | // Allow null to be echoed
|
43 | if (messages[0]) {
|
44 | messages.shift();
|
45 | }
|
46 | } catch (_) {
|
47 | // Clear out error if an error occurred
|
48 | common.state.error = null;
|
49 | }
|
50 |
|
51 | var output = format.apply(null, messages);
|
52 |
|
53 | // Add newline if -n is not passed.
|
54 | if (!options.no_newline) {
|
55 | output += '\n';
|
56 | }
|
57 |
|
58 | process.stdout.write(output);
|
59 |
|
60 | return output;
|
61 | }
|
62 |
|
63 | module.exports = _echo;
|