-
Notifications
You must be signed in to change notification settings - Fork 2
/
argv.js
40 lines (36 loc) · 831 Bytes
/
argv.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
export const argv = ({ forward, reverse }) => {
const argv = process.argv.slice(2)
const cmd = {}
while (argv.length) {
const arg = argv.shift()
switch (arg) {
case '--limit':
cmd.limit = +argv.shift()
break
case '-l':
case '--language':
cmd.language = argv.shift()
break
case '-f':
case '--forward':
cmd.forward = argv.shift() || forward
break
case '-r':
case '--reverse':
cmd.reverse = argv.shift() || reverse
break
default:
if (arg.indexOf('--') === 0) {
const key = arg.substring(2)
cmd[key] = argv.shift()
} else {
cmd.forward = arg
}
break
}
}
if (!cmd.forward && !cmd.reverse) {
cmd.forward = forward
}
return cmd
}