pinyin 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. var commander = require('commander');
  3. commander.
  4. version(require('../package').version).
  5. usage('[options] 汉字').
  6. option('-v, --version', 'output the version number').
  7. option('-s, --style <style>', 'pinyin styles: [NORMAL,TONE,TONE2,INITIALS,FIRST_LETTER]').
  8. option('-m, --mode <mode>', 'pinyin mode: [NORMAL,SURNAME]').
  9. option('-S, --segment [segment]', 'segmentation word to phrases, support "Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit"').
  10. option('-h, --heteronym', 'output heteronym pinyins').
  11. option('-g, --group', 'output group by phrases').
  12. option('-c, --compact', 'output the compact pinyin result').
  13. option('-p, --separator <separator>', 'separator between words').
  14. parse(process.argv);
  15. if (commander.list) {
  16. process.exit();
  17. }
  18. // --segment <segment> 是可选项,当后面带的不合法的 segment 时,当作文本处理。
  19. const validSegmentList = ["Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit", true, false, undefined];
  20. if (!validSegmentList.includes(commander.segment)) {
  21. commander.args.splice(0, 0, commander.segment);
  22. commander.segment = true;
  23. }
  24. // output help and exit if no args found
  25. if (commander.args.length === 0) {
  26. commander.help();
  27. }
  28. var pinyin = require("../").default;
  29. var options = {
  30. style: commander.style || "TONE",
  31. mode: commander.mode || "NORMAL",
  32. heteronym: commander.heteronym || false,
  33. group: commander.group || false,
  34. segment: commander.segment || false,
  35. compact: commander.compact || false,
  36. };
  37. var separator = commander.separator === undefined ? ' ' : commander.separator;
  38. var words = commander.args.join(" ");
  39. var py = pinyin(words, options).join(separator);
  40. console.log(py);
  41. // vim:ft=javascript