-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli.js
66 lines (53 loc) · 1.37 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
'use strict';
const hn = require('./src/');
const minimist = require('minimist');
const version = require('./package.json').version;
const defaults = {
boolean: [
'help',
'version'
],
alias: {
h: 'help',
v: 'version',
l: 'limit',
k: 'keep-open'
},
default: {
'limit': 150,
'keep-open': false,
'latest': false
}
};
const help = `
Usage: hn [OPTIONS]
CLI to browse Hacker News
Example:
$ hn --limit 10 --keep-open
Options:
-v --version Display current software version
-h --help Display help and usage details
-l --limit Limit the number of items to display (defaults to 150)
-k --keep-open Wether or not to keep the list open after selecting an item (defaults to false)
--latest Sort the list by submission date (defaults to false)
`;
const run = argv => hn(argv);
// Must be ≠ 0 if any errors occur during execution
exports.exitCode = 0;
// Allow mocking the stdout/stderr
exports.stdout = process.stdout;
exports.stderr = process.stderr;
exports.parse = options => minimist(options, defaults);
exports.run = argv => {
// Reset status code at each run
exports.exitCode = 0;
if (argv.help) {
exports.stderr.write(help);
return;
}
if (argv.version) {
exports.stderr.write(`hn-cli v${version}\n`);
return;
}
run(argv);
};