-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
·48 lines (39 loc) · 1.13 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
#!/usr/bin/env node
const osmDateFormat = require('.')
const locales = require('./locale/list.json')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin
})
const ArgumentParser = require('argparse').ArgumentParser
const parser = new ArgumentParser({
addHelp: true,
description: 'Read openstreetmap date values (like start_date) from stdin and print a localized string to stdout.'
})
parser.addArgument('--lang', {
help: 'Language to use (if not set, automatically detected from $LANG). Available languages: ' + locales.join(', ')
})
parser.addArgument('--format', {
help: 'Format to use (default: short)',
default: 'long',
choices: [ 'long', 'short' ]
})
const args = parser.parseArgs()
let lang
if (args.lang) {
if (!locales.includes(args.lang)) {
console.error(`Language ${args.lang} not available`)
process.exit(1)
}
lang = args.lang
} else {
// detect locale
[ , lang ] = process.env.LANG.match(/^([a-z]+)[_\.]/)
if (!locales.includes(lang)) {
lang = locales[0]
}
}
osmDateFormat.locale(lang)
rl.on('line', input => {
console.log(osmDateFormat(input, args))
})