49 lines
1.7 KiB
JavaScript
Executable File
49 lines
1.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
var commander = require('commander');
|
|
|
|
commander.
|
|
version(require('../package').version).
|
|
usage('[options] 汉字').
|
|
option('-v, --version', 'output the version number').
|
|
option('-s, --style <style>', 'pinyin styles: [NORMAL,TONE,TONE2,INITIALS,FIRST_LETTER]').
|
|
option('-m, --mode <mode>', 'pinyin mode: [NORMAL,SURNAME]').
|
|
option('-S, --segment [segment]', 'segmentation word to phrases, support "Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit"').
|
|
option('-h, --heteronym', 'output heteronym pinyins').
|
|
option('-g, --group', 'output group by phrases').
|
|
option('-c, --compact', 'output the compact pinyin result').
|
|
option('-p, --separator <separator>', 'separator between words').
|
|
parse(process.argv);
|
|
|
|
if (commander.list) {
|
|
process.exit();
|
|
}
|
|
|
|
// --segment <segment> 是可选项,当后面带的不合法的 segment 时,当作文本处理。
|
|
const validSegmentList = ["Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit", true, false, undefined];
|
|
if (!validSegmentList.includes(commander.segment)) {
|
|
commander.args.splice(0, 0, commander.segment);
|
|
commander.segment = true;
|
|
}
|
|
// output help and exit if no args found
|
|
if (commander.args.length === 0) {
|
|
commander.help();
|
|
}
|
|
|
|
var pinyin = require("../").default;
|
|
var options = {
|
|
style: commander.style || "TONE",
|
|
mode: commander.mode || "NORMAL",
|
|
heteronym: commander.heteronym || false,
|
|
group: commander.group || false,
|
|
segment: commander.segment || false,
|
|
compact: commander.compact || false,
|
|
};
|
|
|
|
var separator = commander.separator === undefined ? ' ' : commander.separator;
|
|
var words = commander.args.join(" ");
|
|
var py = pinyin(words, options).join(separator);
|
|
console.log(py);
|
|
|
|
// vim:ft=javascript
|