Sha256: 8e70dc3a05f8c1fcdcaa80bcd6b2a6cb4fef6f733b34546bf2f85fe70b3c1c60

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env node

/* All of the following commands are equivalent and write `foo\tbar foo` to out.txt
    $ ./echo.js -n -e --output=out.txt "foo\tbar" "foo"
    $ ./echo.js --newline --escape --output "out.txt" "foo\tbar" "foo"
    $ ./echo.js -ne --output=out.txt "foo\tbar" "foo"
    $ ./echo.js -en --output="out.txt" "foo\tbar" "foo"
*/

var cli = require('cli');

cli.parse({
    newline:   ['n', 'Do not output the trailing newline'],
    escape:    ['e', 'Enable interpretation of backslash escapes'],
    separator: ['s', 'Separate arguments using this value', 'string', ' '],
    output:    [false, 'Write to FILE rather than the console', 'file']
});

cli.main(function (args, options) {
    var output = '', i, j, l, output_stream;
    
    if (this.argc) {
        if (options.escape) {
            var replace = {'\\n':'\n','\\r':'\r','\\t':'\t','\\e':'\e','\\v':'\v','\\f':'\f','\\c':'\c','\\b':'\b','\\a':'\a','\\\\':'\\'};
            var escape = function (str) {
                string += '';
                for (j in replace) {
                    string = string.replace(i, replace[i]);
                }
                return string;
            }
            for (i = 0, l = this.argc; i < l; i++) {
                args[i] = escape(args[i]);
            }
            options.separator = escape(options.separator);
        }
        output += args.join(options.separator);
    }
    
    if (!options.newline) {
        output += '\n';
    }
    
    try {
        if (options.output) {
            output_stream = this.native.fs.createWriteStream(options.output)
        } else {
            output_stream = process.stdout;
        }
        output_stream.write(output);
    } catch (e) {
        this.fatal('Could not write to output stream');
    }
});

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
exercism-analysis-0.1.1 vendor/javascript/node_modules/jshint/node_modules/cli/examples/echo.js
trans-0.5.10 template/node_modules/grunt-contrib-jshint/node_modules/jshint/node_modules/cli/examples/echo.js
trans-0.5.9 template/node_modules/grunt-contrib-jshint/node_modules/jshint/node_modules/cli/examples/echo.js