-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (34 loc) · 910 Bytes
/
index.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
const { program } = require('commander');
let pkg = require('./package.json');
let Server = require('./main.js');
console.log('-------------Welcome to my-http-server-------------');
let config = {
'-p, --port <port>': {
description: 'set a http-server port',
examples: `${pkg.name} -p 3000`
},
'-d, --directory <dir>': {
description: 'set a http-server directory',
examples: `${pkg.name} -d d`
}
};
let entries = (config, cb) => {
Object.entries(config).forEach(([key, option]) => {
cb(key, option);
});
};
// --help配置项
entries(config, (key, option) => {
program.option(key, option.description);
});
// 帮助
program.on('--help', () => {
console.log('Examples: ');
entries(config, (key, option) => {
console.log(' ' + option.examples);
});
});
program.version(pkg.version);
program.name(pkg.name);
let userConfig = program.parse(process.argv);
new Server(userConfig).start();