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
|
#!/usr/bin/env node
'use strict'
var meow = require('meow')
var franc = require('franc')
var pack = require('./package')
var command = Object.keys(pack.bin)[0]
var cli = meow(help(), {
flags: {
all: {
type: 'boolean',
alias: 'a'
},
whitelist: {
type: 'string',
alias: 'w'
},
only: {
type: 'string',
alias: 'o'
},
blacklist: {
type: 'string',
alias: 'b'
},
ignore: {
type: 'string',
alias: 'i'
},
minLength: {
type: 'string',
alias: 'm'
},
version: {
type: 'boolean',
alias: 'v'
},
help: {
type: 'boolean',
alias: 'h'
}
}
})
var value = cli.input.join(' ').trim()
var flags = cli.flags
flags.minLength = Number(flags.minLength) || null
flags.whitelist = list(flags.whitelist)
flags.blacklist = list(flags.blacklist)
flags.only = flags.whitelist.concat(list(flags.only))
flags.ignore = flags.blacklist.concat(list(flags.ignore))
if (value) {
detect(value)
} else {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function(data) {
detect(data.trim())
})
}
function detect(value) {
var options = {
minLength: flags.minLength,
only: flags.only,
ignore: flags.ignore
}
if (flags.all) {
franc.all(value, options).forEach(function(language) {
console.log(language[0] + ' ' + language[1])
})
} else {
console.log(franc(value, options))
}
}
function help() {
return [
'Usage: ' + command + ' [options] <string>',
'',
'Options:',
'',
' -h, --help output usage information',
' -v, --version output version number',
' -m, --min-length <number> minimum length to accept',
' -o, --only <string> allow languages',
' -i, --ignore <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 স্ক্রিপ্ট'),
'',
'# ignore certain languages',
'$ ' + command + ' --ignore por,glg "O Brasil caiu 26 posições"',
'# ' + franc('O Brasil caiu 26 posições', {ignore: ['por', 'glg']}),
'',
'# output language from stdin with only',
'$ echo "Alle mennesker er født frie og" | ' + command + ' --only nob,dan',
'# ' + franc('Alle mennesker er født frie og', {only: ['nob', 'dan']})
].join('\n')
}
function list(value) {
return value ? String(value).split(',') : []
}
|