Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env node
  2. /* All of the following commands are equivalent and write `foo\tbar foo` to out.txt
  3. $ ./echo.js -n -e --output=out.txt "foo\tbar" "foo"
  4. $ ./echo.js --newline --escape --output "out.txt" "foo\tbar" "foo"
  5. $ ./echo.js -ne --output=out.txt "foo\tbar" "foo"
  6. $ ./echo.js -en --output="out.txt" "foo\tbar" "foo"
  7. */
  8. var cli = require('cli');
  9. cli.parse({
  10. newline: ['n', 'Do not output the trailing newline'],
  11. escape: ['e', 'Enable interpretation of backslash escapes'],
  12. separator: ['s', 'Separate arguments using this value', 'string', ' '],
  13. output: [false, 'Write to FILE rather than the console', 'file']
  14. });
  15. cli.main(function (args, options) {
  16. var output = '', i, j, l, output_stream;
  17. if (this.argc) {
  18. if (options.escape) {
  19. var replace = {'\\n':'\n','\\r':'\r','\\t':'\t','\\e':'\e','\\v':'\v','\\f':'\f','\\c':'\c','\\b':'\b','\\a':'\a','\\\\':'\\'};
  20. var escape = function (str) {
  21. str += '';
  22. for (j in replace) {
  23. str = str.replace(i, replace[i]);
  24. }
  25. return str;
  26. }
  27. for (i = 0, l = this.argc; i < l; i++) {
  28. args[i] = escape(args[i]);
  29. }
  30. options.separator = escape(options.separator);
  31. }
  32. output += args.join(options.separator);
  33. }
  34. if (!options.newline) {
  35. output += '\n';
  36. }
  37. try {
  38. if (options.output) {
  39. output_stream = this.native.fs.createWriteStream(options.output)
  40. } else {
  41. output_stream = process.stdout;
  42. }
  43. output_stream.write(output);
  44. } catch (e) {
  45. this.fatal('Could not write to output stream');
  46. }
  47. });