-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathparse-cli-args.js
More file actions
57 lines (44 loc) · 993 Bytes
/
parse-cli-args.js
File metadata and controls
57 lines (44 loc) · 993 Bytes
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
import yargs from 'yargs';
import { version } from '../package.json';
import common from './commands/common-yargs';
import commands from './commands';
const help = {
options: null,
execute: yargs.showHelp
};
export default function parseCliArgs(argv) {
if (argv === process.argv) {
argv = argv.slice(2);
}
common(yargs);
yargs
.version(`v${version}`)
.alias('version', 'v')
.wrap(null);
Object.keys(commands)
.map(key => commands[key])
.forEach(x => {
if (x.yargsSetup) {
x.yargsSetup();
}
if (x.examples) {
x.examples();
}
});
let commandOptions = yargs.parse(argv);
let command = commandOptions._[0];
if (!command) {
if (commandOptions.help) {
return help;
}
throw 'No command provided';
}
let options = commands[command].parseArgs(argv.slice(1));
if (commandOptions.help) {
return help;
}
return {
options,
execute: commands[command].execute
};
}