cli.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /*
  4. * Dependencies.
  5. */
  6. var direction,
  7. pack;
  8. direction = require('./');
  9. pack = require('./package.json');
  10. /*
  11. * Arguments.
  12. */
  13. var argv;
  14. argv = process.argv.slice(2);
  15. /*
  16. * Command.
  17. */
  18. var command;
  19. command = Object.keys(pack.bin)[0];
  20. /**
  21. * Help.
  22. *
  23. * @return {string}
  24. */
  25. function help() {
  26. return [
  27. '',
  28. 'Usage: ' + command + ' [options] words...',
  29. '',
  30. pack.description,
  31. '',
  32. 'Options:',
  33. '',
  34. ' -h, --help output usage information',
  35. ' -v, --version output version number',
  36. '',
  37. 'Usage:',
  38. '',
  39. '# output direction of given values',
  40. '$ ' + command + ' @',
  41. '# neutral',
  42. '',
  43. '# output direction from stdin',
  44. '$ echo "الانجليزية" | ' + command,
  45. '# rtl'
  46. ].join('\n ') + '\n';
  47. }
  48. /*
  49. * Program.
  50. */
  51. if (
  52. argv.indexOf('--help') === 0 ||
  53. argv.indexOf('-h') === 0
  54. ) {
  55. console.log(help());
  56. } else if (
  57. argv.indexOf('--version') === 0 ||
  58. argv.indexOf('-v') === 0
  59. ) {
  60. console.log(pack.version);
  61. } else if (argv[0]) {
  62. console.log(direction(argv.join(' ')));
  63. } else {
  64. process.stdin.resume();
  65. process.stdin.setEncoding('utf8');
  66. process.stdin.on('data', function (data) {
  67. console.log(direction(data.trim()));
  68. });
  69. }