forked from wooorm/franc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·170 lines (144 loc) · 3.61 KB
/
cli.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
'use strict';
/* Dependencies. */
var pack = require('./package.json');
var franc = require('./');
/* Detect if a value is expected to be piped in. */
var expextPipeIn = !process.stdin.isTTY;
/* Arguments. */
var argv = process.argv.slice(2);
/* Command. */
var command = Object.keys(pack.bin)[0];
var index;
var blacklist;
var whitelist;
var minLength;
var all = false;
/**
* Log the language for `value`.
*
* @param {string} value - Value to detect.
*/
function detect(value) {
var options;
if (value && value.length) {
options = {
minLength: minLength,
whitelist: whitelist,
blacklist: blacklist
};
if (all) {
franc.all(value, options).forEach(function (language) {
console.log(language[0] + ' ' + language[1]);
});
} else {
console.log(franc(value, options));
}
} else {
process.stderr.write(help());
process.exit(1);
}
}
/* Program. */
if (
argv.indexOf('--help') !== -1 ||
argv.indexOf('-h') !== -1
) {
console.log(help());
} else if (
argv.indexOf('--version') !== -1 ||
argv.indexOf('-v') !== -1
) {
console.log(pack.version);
} else {
index = argv.indexOf('--min-length');
if (index === -1) {
index = argv.indexOf('-m');
}
if (index !== -1) {
minLength = Number(argv[index + 1] || '');
argv.splice(index, 2);
}
index = argv.indexOf('--blacklist');
if (index === -1) {
index = argv.indexOf('-b');
}
if (index !== -1) {
blacklist = (argv[index + 1] || '').split(',');
argv.splice(index, 2);
}
index = argv.indexOf('--whitelist');
if (index === -1) {
index = argv.indexOf('-w');
}
if (index !== -1) {
whitelist = (argv[index + 1] || '').split(',');
argv.splice(index, 2);
}
index = argv.indexOf('--all');
if (index === -1) {
index = argv.indexOf('-a');
}
if (index !== -1) {
all = true;
argv.splice(index, 1);
}
if (argv.length) {
detect(argv.join(' '));
} else if (expextPipeIn) {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (data) {
detect(data.trim());
});
} else {
detect([]);
}
}
/**
* Help.
*
* @return {string} - Help message.
*/
function help() {
return [
'',
'Usage: ' + command + ' [options] <string>',
'',
pack.description,
'',
'Options:',
'',
' -h, --help output usage information',
' -v, --version output version number',
' -m, --min-length <number> minimum length to accept',
' -w, --whitelist <string> allow languages',
' -b, --blacklist <string> disallow languages',
' -a, --all display all guesses',
'',
'Usage:',
'',
'# output language',
'$ ' + command + ' "Alle menslike wesens word vry"',
'# ' + franc('Alle menslike wesens word vry'),
'',
'# output language from stdin (expects utf8)',
'$ echo "এটি একটি ভাষা একক IBM স্ক্রিপ্ট" | ' + command,
'# ' + franc('এটি একটি ভাষা একক IBM স্ক্রিপ্ট'),
'',
'# blacklist certain languages',
'$ ' + command + ' --blacklist por,glg ' +
'"O Brasil caiu 26 posições"',
'# ' + franc('O Brasil caiu 26 posições', {
blacklist: ['por', 'glg']
}),
'',
'# output language from stdin with whitelist',
'$ echo "Alle mennesker er født frie og" | ' + command +
' --whitelist nob,dan',
'# ' + franc('Alle mennesker er født frie og', {
whitelist: ['nob', 'dan']
}),
''
].join('\n ') + '\n';
}