1 | "use strict";
|
2 |
|
3 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [0, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var _this = this;
|
39 | Object.defineProperty(exports, "__esModule", { value: true });
|
40 | var Parser_1 = require("./Parser");
|
41 | var index_1 = require("./Flags/index");
|
42 | test('parses args and flags', function () { return __awaiter(_this, void 0, void 0, function () {
|
43 | var p, _a, argv, flags;
|
44 | return __generator(this, function (_b) {
|
45 | switch (_b.label) {
|
46 | case 0:
|
47 | p = new Parser_1.Parser({
|
48 | args: [{ name: 'myarg' }, { name: 'myarg2' }],
|
49 | flags: { myflag: index_1.flags.string() }
|
50 | });
|
51 | return [4 , p.parse({ argv: ['foo', '--myflag', 'bar', 'baz'] })];
|
52 | case 1:
|
53 | _a = _b.sent(), argv = _a.argv, flags = _a.flags;
|
54 | expect(argv[0]).toEqual('foo');
|
55 | expect(argv[1]).toEqual('baz');
|
56 | expect(flags.myflag).toEqual('bar');
|
57 | return [2 ];
|
58 | }
|
59 | });
|
60 | }); });
|
61 | describe('flags', function () {
|
62 | test('parses flags', function () { return __awaiter(_this, void 0, void 0, function () {
|
63 | var p, flags;
|
64 | return __generator(this, function (_a) {
|
65 | switch (_a.label) {
|
66 | case 0:
|
67 | p = new Parser_1.Parser({ flags: { myflag: index_1.flags.boolean(), myflag2: index_1.flags.boolean() } });
|
68 | return [4 , p.parse({ argv: ['--myflag', '--myflag2'] })];
|
69 | case 1:
|
70 | flags = (_a.sent()).flags;
|
71 | expect(flags.myflag).toBeTruthy();
|
72 | expect(flags.myflag2).toBeTruthy();
|
73 | return [2 ];
|
74 | }
|
75 | });
|
76 | }); });
|
77 | test('parses short flags', function () { return __awaiter(_this, void 0, void 0, function () {
|
78 | var p, flags;
|
79 | return __generator(this, function (_a) {
|
80 | switch (_a.label) {
|
81 | case 0:
|
82 | p = new Parser_1.Parser({
|
83 | flags: {
|
84 | myflag: index_1.flags.boolean({ char: 'm' }),
|
85 | force: index_1.flags.boolean({ char: 'f' })
|
86 | }
|
87 | });
|
88 | return [4 , p.parse({ argv: ['-mf'] })];
|
89 | case 1:
|
90 | flags = (_a.sent()).flags;
|
91 | expect(flags.myflag).toBeTruthy();
|
92 | expect(flags.force).toBeTruthy();
|
93 | return [2 ];
|
94 | }
|
95 | });
|
96 | }); });
|
97 | test('parses flag value with "=" to separate', function () { return __awaiter(_this, void 0, void 0, function () {
|
98 | var p, flags;
|
99 | return __generator(this, function (_a) {
|
100 | switch (_a.label) {
|
101 | case 0:
|
102 | p = new Parser_1.Parser({
|
103 | flags: {
|
104 | myflag: index_1.flags.string({ char: 'm' })
|
105 | }
|
106 | });
|
107 | return [4 , p.parse({ argv: ['--myflag=foo'] })];
|
108 | case 1:
|
109 | flags = (_a.sent()).flags;
|
110 | expect(flags).toEqual({ myflag: 'foo' });
|
111 | return [2 ];
|
112 | }
|
113 | });
|
114 | }); });
|
115 | test('parses flag value with "=" in value', function () { return __awaiter(_this, void 0, void 0, function () {
|
116 | var p, flags;
|
117 | return __generator(this, function (_a) {
|
118 | switch (_a.label) {
|
119 | case 0:
|
120 | p = new Parser_1.Parser({
|
121 | flags: {
|
122 | myflag: index_1.flags.string({ char: 'm' })
|
123 | }
|
124 | });
|
125 | return [4 , p.parse({ argv: ['--myflag', '=foo'] })];
|
126 | case 1:
|
127 | flags = (_a.sent()).flags;
|
128 | expect(flags).toEqual({ myflag: '=foo' });
|
129 | return [2 ];
|
130 | }
|
131 | });
|
132 | }); });
|
133 | test('parses short flag value with "="', function () { return __awaiter(_this, void 0, void 0, function () {
|
134 | var p, flags;
|
135 | return __generator(this, function (_a) {
|
136 | switch (_a.label) {
|
137 | case 0:
|
138 | p = new Parser_1.Parser({
|
139 | flags: {
|
140 | myflag: index_1.flags.string({ char: 'm' })
|
141 | }
|
142 | });
|
143 | return [4 , p.parse({ argv: ['-m=foo'] })];
|
144 | case 1:
|
145 | flags = (_a.sent()).flags;
|
146 | expect(flags).toEqual({ myflag: 'foo' });
|
147 | return [2 ];
|
148 | }
|
149 | });
|
150 | }); });
|
151 | test('requires required flag', function () { return __awaiter(_this, void 0, void 0, function () {
|
152 | var p, err_1;
|
153 | return __generator(this, function (_a) {
|
154 | switch (_a.label) {
|
155 | case 0:
|
156 | p = new Parser_1.Parser({
|
157 | flags: {
|
158 | myflag: index_1.flags.string({ required: true })
|
159 | }
|
160 | });
|
161 | expect.assertions(1);
|
162 | _a.label = 1;
|
163 | case 1:
|
164 | _a.trys.push([1, 3, , 4]);
|
165 | return [4 , p.parse()];
|
166 | case 2:
|
167 | _a.sent();
|
168 | return [3 , 4];
|
169 | case 3:
|
170 | err_1 = _a.sent();
|
171 | expect(err_1.message).toEqual('Missing required flag --myflag');
|
172 | return [3 , 4];
|
173 | case 4: return [2 ];
|
174 | }
|
175 | });
|
176 | }); });
|
177 | test('requires nonoptional flag', function () { return __awaiter(_this, void 0, void 0, function () {
|
178 | var p, err_2;
|
179 | return __generator(this, function (_a) {
|
180 | switch (_a.label) {
|
181 | case 0:
|
182 | p = new Parser_1.Parser({
|
183 | flags: {
|
184 | myflag: index_1.flags.string({ required: true })
|
185 | }
|
186 | });
|
187 | expect.assertions(1);
|
188 | _a.label = 1;
|
189 | case 1:
|
190 | _a.trys.push([1, 3, , 4]);
|
191 | return [4 , p.parse()];
|
192 | case 2:
|
193 | _a.sent();
|
194 | return [3 , 4];
|
195 | case 3:
|
196 | err_2 = _a.sent();
|
197 | expect(err_2.message).toEqual('Missing required flag --myflag');
|
198 | return [3 , 4];
|
199 | case 4: return [2 ];
|
200 | }
|
201 | });
|
202 | }); });
|
203 | test('removes flags from argv', function () { return __awaiter(_this, void 0, void 0, function () {
|
204 | var p, _a, flags, argv;
|
205 | return __generator(this, function (_b) {
|
206 | switch (_b.label) {
|
207 | case 0:
|
208 | p = new Parser_1.Parser({
|
209 | args: [{ name: 'myarg' }],
|
210 | flags: { myflag: index_1.flags.string() }
|
211 | });
|
212 | return [4 , p.parse({ argv: ['--myflag', 'bar', 'foo'] })];
|
213 | case 1:
|
214 | _a = _b.sent(), flags = _a.flags, argv = _a.argv;
|
215 | expect(flags).toEqual({ myflag: 'bar' });
|
216 | expect(argv).toEqual(['foo']);
|
217 | return [2 ];
|
218 | }
|
219 | });
|
220 | }); });
|
221 | });
|
222 | describe('args', function () {
|
223 | test('requires no args by default', function () { return __awaiter(_this, void 0, void 0, function () {
|
224 | var p, err_3;
|
225 | return __generator(this, function (_a) {
|
226 | switch (_a.label) {
|
227 | case 0:
|
228 | expect.assertions(0);
|
229 | p = new Parser_1.Parser({ args: [{ name: 'myarg' }, { name: 'myarg2' }] });
|
230 | _a.label = 1;
|
231 | case 1:
|
232 | _a.trys.push([1, 3, , 4]);
|
233 | return [4 , p.parse()];
|
234 | case 2:
|
235 | _a.sent();
|
236 | return [3 , 4];
|
237 | case 3:
|
238 | err_3 = _a.sent();
|
239 | expect(err_3.message).toEqual('Missing required argument myarg');
|
240 | return [3 , 4];
|
241 | case 4: return [2 ];
|
242 | }
|
243 | });
|
244 | }); });
|
245 | test('parses args', function () { return __awaiter(_this, void 0, void 0, function () {
|
246 | var p, argv;
|
247 | return __generator(this, function (_a) {
|
248 | switch (_a.label) {
|
249 | case 0:
|
250 | p = new Parser_1.Parser({ args: [{ name: 'myarg' }, { name: 'myarg2' }] });
|
251 | return [4 , p.parse({ argv: ['foo', 'bar'] })];
|
252 | case 1:
|
253 | argv = (_a.sent()).argv;
|
254 | expect(argv).toEqual(['foo', 'bar']);
|
255 | return [2 ];
|
256 | }
|
257 | });
|
258 | }); });
|
259 | test('skips optional args', function () { return __awaiter(_this, void 0, void 0, function () {
|
260 | var p, argv;
|
261 | return __generator(this, function (_a) {
|
262 | switch (_a.label) {
|
263 | case 0:
|
264 | p = new Parser_1.Parser({ args: [{ name: 'myarg', required: false }, { name: 'myarg2', required: false }] });
|
265 | return [4 , p.parse({ argv: ['foo'] })];
|
266 | case 1:
|
267 | argv = (_a.sent()).argv;
|
268 | expect(argv).toEqual(['foo']);
|
269 | return [2 ];
|
270 | }
|
271 | });
|
272 | }); });
|
273 | test('parses something looking like a flag as an arg', function () { return __awaiter(_this, void 0, void 0, function () {
|
274 | var p, argv;
|
275 | return __generator(this, function (_a) {
|
276 | switch (_a.label) {
|
277 | case 0:
|
278 | p = new Parser_1.Parser({ args: [{ name: 'myarg' }] });
|
279 | return [4 , p.parse({ argv: ['--foo'] })];
|
280 | case 1:
|
281 | argv = (_a.sent()).argv;
|
282 | expect(argv).toEqual(['--foo']);
|
283 | return [2 ];
|
284 | }
|
285 | });
|
286 | }); });
|
287 | });
|
288 | describe('variableArgs', function () {
|
289 | test('skips flag parsing after "--"', function () { return __awaiter(_this, void 0, void 0, function () {
|
290 | var p, _a, argv, args;
|
291 | return __generator(this, function (_b) {
|
292 | switch (_b.label) {
|
293 | case 0:
|
294 | p = new Parser_1.Parser({
|
295 | variableArgs: true,
|
296 | flags: { myflag: index_1.flags.boolean() },
|
297 | args: [{ name: 'argOne' }]
|
298 | });
|
299 | return [4 , p.parse({ argv: ['foo', 'bar', '--', '--myflag'] })];
|
300 | case 1:
|
301 | _a = _b.sent(), argv = _a.argv, args = _a.args;
|
302 | expect(argv).toEqual(['foo', 'bar', '--myflag']);
|
303 | expect(args).toEqual({ argOne: 'foo' });
|
304 | return [2 ];
|
305 | }
|
306 | });
|
307 | }); });
|
308 | test('does not repeat arguments', function () { return __awaiter(_this, void 0, void 0, function () {
|
309 | var p, argv;
|
310 | return __generator(this, function (_a) {
|
311 | switch (_a.label) {
|
312 | case 0:
|
313 | p = new Parser_1.Parser({
|
314 | variableArgs: true
|
315 | });
|
316 | return [4 , p.parse({ argv: ['foo', '--myflag=foo bar'] })];
|
317 | case 1:
|
318 | argv = (_a.sent()).argv;
|
319 | expect(argv).toEqual(['foo', '--myflag=foo bar']);
|
320 | return [2 ];
|
321 | }
|
322 | });
|
323 | }); });
|
324 | });
|
325 |
|
\ | No newline at end of file |