1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var chek_1 = require("chek");
|
4 |
|
5 | var _args = process.argv.slice(0);
|
6 | exports.args = _args;
|
7 |
|
8 | var _baseArgs = _args.slice(2);
|
9 | exports.normalized = _baseArgs;
|
10 |
|
11 | var _optionArgs = _baseArgs.slice(1);
|
12 | exports.options = _optionArgs;
|
13 |
|
14 | var _cmds = [];
|
15 |
|
16 | var _flags = {};
|
17 |
|
18 | var _flagExp = /^--?/;
|
19 |
|
20 | var _exclude = [];
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function isFlag(flag) {
|
28 | if (_flagExp.test(flag)) {
|
29 | if (/^--/.test(flag))
|
30 | return 'value';
|
31 | return 'boolean';
|
32 | }
|
33 | return false;
|
34 | }
|
35 | exports.isFlag = isFlag;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function parseElement(val, idx, args) {
|
45 | var flagType = isFlag(val);
|
46 | if (!flagType)
|
47 | return false;
|
48 | if (flagType === 'boolean')
|
49 | return true;
|
50 | var nextIdx = idx + 1;
|
51 | if (args[nextIdx]) {
|
52 | _exclude.push(nextIdx);
|
53 | var val_1 = args[nextIdx];
|
54 | if (val_1 === 'true' || val_1 === 'false')
|
55 | return chek_1.toBoolean(val_1);
|
56 | return chek_1.castType(val_1, chek_1.getType(val_1));
|
57 | }
|
58 |
|
59 | return true;
|
60 | }
|
61 | exports.parseElement = parseElement;
|
62 |
|
63 | function parse(args) {
|
64 | args = args || _baseArgs;
|
65 | args.forEach(function (el, idx) {
|
66 | var flag = parseElement(el, idx, args);
|
67 | if (!flag && _exclude.indexOf(idx) === -1)
|
68 | _cmds.push(el);
|
69 | else if (flag)
|
70 | _flags[el.replace(_flagExp, '')] = flag;
|
71 | });
|
72 | return {
|
73 | flags: _flags,
|
74 | cmds: _cmds,
|
75 | cmd: _cmds[0]
|
76 | };
|
77 | }
|
78 | exports.parse = parse;
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function find(valid, args) {
|
87 |
|
88 | args = args || parse().cmds;
|
89 | var i = valid.length;
|
90 | var found;
|
91 | while (i-- && !found) {
|
92 | if (chek_1.contains(args, valid[i]))
|
93 | found = valid[i];
|
94 | }
|
95 | return found;
|
96 | }
|
97 | exports.find = find;
|
98 |
|
\ | No newline at end of file |