1 | 'use strict'
|
2 |
|
3 | function getParamName (param) {
|
4 | return param ? (param.match(/^-{1,2}([^ ]*)$/) || []).pop() : null
|
5 | }
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function getArgs () {
|
12 | const args = { chain: [] }
|
13 | const argv = process.argv.slice(2)
|
14 |
|
15 | for (let ii = argv.length - 1; ii >= 0; --ii) {
|
16 | const curr = argv[ii]
|
17 | const next = ii < argv.length - 1 ? argv[ii + 1] : null
|
18 | const paramName = getParamName(curr)
|
19 |
|
20 | if (paramName) {
|
21 | let paramValue = true
|
22 | if (next && !getParamName(next)) {
|
23 | paramValue = next
|
24 | argv.splice(ii + 1, 1)
|
25 | }
|
26 |
|
27 | args[paramName] = paramValue
|
28 | argv.splice(ii, 1)
|
29 | }
|
30 |
|
31 | args._ = argv.filter(item => item !== '[' && item !== ']').join(' ')
|
32 | }
|
33 |
|
34 | return args
|
35 | }
|
36 |
|
37 | module.exports = getArgs()
|