1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var chek_1 = require("chek");
|
4 | var logger_1 = require("./logger");
|
5 |
|
6 | var _args = process.argv.slice(2);
|
7 | var _command = null;
|
8 |
|
9 | var _commands = [];
|
10 |
|
11 | var _normalized = [];
|
12 |
|
13 | var _flags = {};
|
14 |
|
15 | var _flagExp = /^--?/;
|
16 |
|
17 | var _noFlagExp = /^--no-/;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function castToType(val) {
|
25 | if (/^(true|false)$/.test(val))
|
26 | return chek_1.toBoolean(val);
|
27 | if (/^[\d\.]+$/.test(val))
|
28 | return chek_1.toNumber(val);
|
29 | return val;
|
30 | }
|
31 | exports.castToType = castToType;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function flagsToArray(flags) {
|
39 | var arr = [];
|
40 | for (var k in flags) {
|
41 | var flag = flags[k];
|
42 | var prefix = flags[k] === false ? '--no-' : k.length > 1 ? '--' : '-';
|
43 | arr.push(prefix + k);
|
44 | if (!chek_1.isBoolean(flags[k]))
|
45 | arr.push(flags[k]);
|
46 | }
|
47 | return arr;
|
48 | }
|
49 | exports.flagsToArray = flagsToArray;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function splitArgs(str) {
|
57 | if (chek_1.isArray(str))
|
58 | return str;
|
59 | if (!chek_1.isString(str))
|
60 | return [];
|
61 | var arr = [];
|
62 | str
|
63 | .split(/(\x22[^\x22]*\x22)/)
|
64 | .filter(function (x) { return x; })
|
65 | .forEach(function (s) {
|
66 | if (s.match('\x22'))
|
67 | arr.push(s.replace(/\x22/g, ''));
|
68 | else
|
69 | arr = arr.concat(s.trim().split(' '));
|
70 | });
|
71 | return arr;
|
72 | }
|
73 | exports.splitArgs = splitArgs;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function normalizeArgs(args, exclude) {
|
85 | var result = [];
|
86 | args = splitArgs(args || []);
|
87 | exclude = splitArgs(exclude || []);
|
88 | args.slice(0).forEach(function (arg) {
|
89 | var val;
|
90 | if (~arg.indexOf('=')) {
|
91 | var kv = arg.split('=');
|
92 | arg = kv[0];
|
93 | val = kv[1];
|
94 | }
|
95 | if (/^-{1}[a-z]/i.test(arg)) {
|
96 | var spread = arg.slice(1).split('').map(function (a) { return '-' + a; });
|
97 | if (val)
|
98 | spread.push(val);
|
99 | result = result.concat(spread);
|
100 | }
|
101 | else {
|
102 | result.push(arg);
|
103 | if (val)
|
104 | result.push(val);
|
105 | }
|
106 | });
|
107 | return filterArgs(result, exclude);
|
108 | }
|
109 | exports.normalizeArgs = normalizeArgs;
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function filterArgs(args, exclude) {
|
118 | args = splitArgs(args || []);
|
119 | exclude = splitArgs(exclude || []);
|
120 | var clone = args.slice(0);
|
121 | return clone.filter(function (arg, i) {
|
122 | if (!~exclude.indexOf(arg))
|
123 | return true;
|
124 | var next = clone[i + 1];
|
125 | if (_flagExp.test(arg) && next && !_flagExp.test(next))
|
126 | clone.splice(i + 1, 1);
|
127 | return false;
|
128 | });
|
129 | }
|
130 | exports.filterArgs = filterArgs;
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function mergeArgs(def, args, exclude) {
|
139 | exclude = splitArgs(exclude || []);
|
140 | def = normalizeArgs(def);
|
141 | args = normalizeArgs(args);
|
142 | var clone = def.slice(0);
|
143 | args.forEach(function (arg, i) {
|
144 | var next = args[i + 1];
|
145 | var isFlag = _flagExp.test(arg);
|
146 | var isFlagNext = _flagExp.test(next || '');
|
147 | var defIdx = def.indexOf(arg);
|
148 |
|
149 | if (~defIdx && isFlag && !isFlagNext) {
|
150 | clone[defIdx + 1] = next;
|
151 | args.splice(i + 1, 1);
|
152 | }
|
153 | else if (!~defIdx) {
|
154 | clone.push(arg);
|
155 | }
|
156 | });
|
157 | return filterArgs(clone, exclude);
|
158 | }
|
159 | exports.mergeArgs = mergeArgs;
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function parse(args, exclude) {
|
168 | var normalized = normalizeArgs(args || _args, exclude);
|
169 | var clone = normalized.slice(0);
|
170 | clone.forEach(function (arg, i) {
|
171 | var next = clone[i + 1];
|
172 | var isFlag = _flagExp.test(arg);
|
173 | var isFlagNext = _flagExp.test(next || '');
|
174 | var isNoFlag = _noFlagExp.test(arg);
|
175 | if (isFlag) {
|
176 | var key = isNoFlag ? arg.replace(_noFlagExp, '') : arg.replace(_flagExp, '');
|
177 | key = key.split('.').map(function (k) { return chek_1.camelcase(k); }).join('.');
|
178 | var curVal = chek_1.get(_flags, key);
|
179 | var exists = chek_1.isValue(curVal);
|
180 | if (exists)
|
181 | curVal = chek_1.toArray(curVal, []);
|
182 | if (!isFlagNext && next) {
|
183 | if (isNoFlag)
|
184 | logger_1.log.error("Oops cannot set flag value using inverse (--no-xxx) flag " + arg + ".");
|
185 | var val = castToType(next);
|
186 | if (exists) {
|
187 | curVal.push(val);
|
188 | val = curVal;
|
189 | }
|
190 | chek_1.set(_flags, key, val);
|
191 | clone.splice(i + 1, 1);
|
192 | }
|
193 | else {
|
194 | chek_1.set(_flags, key, isNoFlag ? false : true);
|
195 | }
|
196 | }
|
197 | else {
|
198 | _commands.push(castToType(arg));
|
199 | }
|
200 | });
|
201 | if (_commands)
|
202 | _command = _commands.shift();
|
203 | return {
|
204 | command: _command,
|
205 | commands: _commands,
|
206 | cmd: _command,
|
207 | cmds: _commands,
|
208 | flags: _flags,
|
209 | source: _args,
|
210 | normalized: normalized
|
211 | };
|
212 | }
|
213 | exports.parse = parse;
|
214 |
|
\ | No newline at end of file |