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