-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
65 lines (56 loc) · 1.1 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
#!/usr/bin/env node
var timediff = require('./timediff');
var pkg = require('./package.json');
var start = process.argv[2];
var end = process.argv[3];
var units = process.argv[4];
function help () {
console.log([
'',
' ' + pkg.description,
'',
' Usage: timediff <start> <end> [<units>]',
'',
' Examples:',
'',
" timediff 2013-12-01 '2015-04-20 12:20:10.342' YMWDHmSs",
' years: 1',
' months: 4',
' weeks: 2',
' days: 5',
' hours: 12',
' minutes: 20',
' seconds: 10',
' milliseconds: 342',
'',
' timediff 1989-11-09 now YD',
' years: 25',
' days: 71'
].join('\n'));
}
function run () {
var result;
try {
result = timediff(start, end, units);
} catch (e) {
console.error(e);
help();
return;
}
for (unit in result) {
console.log(unit + ': ' + result[unit]);
}
}
if (process.argv.indexOf('--help') !== -1) {
help();
return;
}
if (process.argv.indexOf('--version') !== -1) {
console.log(pkg.version);
return;
}
if (!start || !end) {
help();
return;
}
run();