-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
38 lines (32 loc) · 1.02 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pinyin from "./deps.ts";
import { parse } from "./deps.ts";
import { version } from "./version.ts";
const args = parse(Deno.args);
const isHelp = args._.length === 0 && Object.keys(args).length === 1;
if (isHelp) {
console.log(`usage: [options] 汉字.
-v, --version, output the version number
-s, --style <style>, pinyin styles: [NORMAL,TONE,TONE2,INITIALS,FIRST_LETTER]
-S, --segment, segmentation word to phrases
-h, --heteronym, output heteronym pinyins
-p, --separator <separator>, separator between words
-d, --debug, display debug log
`);
Deno.exit(0);
}
if (args.v || args.version) {
console.log(version);
Deno.exit(0);
}
const options = {
style: pinyin["STYLE_" + (args.style || args.s || "TONE").toUpperCase()],
heteronym: args.heteronym || args.h || false,
segment: args.segment || args.S || false,
};
if (args.d || args.debug) {
console.dir(args);
}
const separator = args.separator ?? args.p ?? " ";
const words = args._.join(" ");
const py = pinyin(words, options).join(separator);
console.log(py);