1 |
|
2 |
|
3 |
|
4 |
|
5 | const EventEmitter = require('events').EventEmitter;
|
6 | const spawn = require('child_process').spawn;
|
7 | const path = require('path');
|
8 | const fs = require('fs');
|
9 |
|
10 |
|
11 |
|
12 | class Option {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | constructor(flags, description) {
|
22 | this.flags = flags;
|
23 | this.required = flags.indexOf('<') >= 0;
|
24 | this.optional = flags.indexOf('[') >= 0;
|
25 | this.mandatory = false;
|
26 | this.negate = flags.indexOf('-no-') !== -1;
|
27 | const flagParts = flags.split(/[ ,|]+/);
|
28 | if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) this.short = flagParts.shift();
|
29 | this.long = flagParts.shift();
|
30 | this.description = description || '';
|
31 | this.defaultValue = undefined;
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | name() {
|
42 | return this.long.replace(/^--/, '');
|
43 | };
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | attributeName() {
|
54 | return camelcase(this.name().replace(/^no-/, ''));
|
55 | };
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | is(arg) {
|
66 | return this.short === arg || this.long === arg;
|
67 | };
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | class CommanderError extends Error {
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | constructor(exitCode, code, message) {
|
83 | super(message);
|
84 |
|
85 | Error.captureStackTrace(this, this.constructor);
|
86 | this.name = this.constructor.name;
|
87 | this.code = code;
|
88 | this.exitCode = exitCode;
|
89 | this.nestedError = undefined;
|
90 | }
|
91 | }
|
92 |
|
93 | class Command extends EventEmitter {
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | constructor(name) {
|
102 | super();
|
103 | this.commands = [];
|
104 | this.options = [];
|
105 | this.parent = null;
|
106 | this._allowUnknownOption = false;
|
107 | this._args = [];
|
108 | this.rawArgs = null;
|
109 | this._scriptPath = null;
|
110 | this._name = name || '';
|
111 | this._optionValues = {};
|
112 | this._storeOptionsAsProperties = true;
|
113 | this._passCommandToAction = true;
|
114 | this._actionResults = [];
|
115 | this._actionHandler = null;
|
116 | this._executableHandler = false;
|
117 | this._executableFile = null;
|
118 | this._defaultCommandName = null;
|
119 | this._exitCallback = null;
|
120 | this._aliases = [];
|
121 |
|
122 | this._hidden = false;
|
123 | this._helpFlags = '-h, --help';
|
124 | this._helpDescription = 'display help for command';
|
125 | this._helpShortFlag = '-h';
|
126 | this._helpLongFlag = '--help';
|
127 | this._hasImplicitHelpCommand = undefined;
|
128 | this._helpCommandName = 'help';
|
129 | this._helpCommandnameAndArgs = 'help [command]';
|
130 | this._helpCommandDescription = 'display help for command';
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
161 | let desc = actionOptsOrExecDesc;
|
162 | let opts = execOpts;
|
163 | if (typeof desc === 'object' && desc !== null) {
|
164 | opts = desc;
|
165 | desc = null;
|
166 | }
|
167 | opts = opts || {};
|
168 | const args = nameAndArgs.split(/ +/);
|
169 | const cmd = this.createCommand(args.shift());
|
170 |
|
171 | if (desc) {
|
172 | cmd.description(desc);
|
173 | cmd._executableHandler = true;
|
174 | }
|
175 | if (opts.isDefault) this._defaultCommandName = cmd._name;
|
176 |
|
177 | cmd._hidden = !!(opts.noHelp || opts.hidden);
|
178 | cmd._helpFlags = this._helpFlags;
|
179 | cmd._helpDescription = this._helpDescription;
|
180 | cmd._helpShortFlag = this._helpShortFlag;
|
181 | cmd._helpLongFlag = this._helpLongFlag;
|
182 | cmd._helpCommandName = this._helpCommandName;
|
183 | cmd._helpCommandnameAndArgs = this._helpCommandnameAndArgs;
|
184 | cmd._helpCommandDescription = this._helpCommandDescription;
|
185 | cmd._exitCallback = this._exitCallback;
|
186 | cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;
|
187 | cmd._passCommandToAction = this._passCommandToAction;
|
188 |
|
189 | cmd._executableFile = opts.executableFile || null;
|
190 | this.commands.push(cmd);
|
191 | cmd._parseExpectedArgs(args);
|
192 | cmd.parent = this;
|
193 |
|
194 | if (desc) return this;
|
195 | return cmd;
|
196 | };
|
197 |
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | createCommand(name) {
|
210 | return new Command(name);
|
211 | };
|
212 |
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | addCommand(cmd, opts) {
|
225 | if (!cmd._name) throw new Error('Command passed to .addCommand() must have a name');
|
226 |
|
227 |
|
228 |
|
229 | function checkExplicitNames(commandArray) {
|
230 | commandArray.forEach((cmd) => {
|
231 | if (cmd._executableHandler && !cmd._executableFile) {
|
232 | throw new Error(`Must specify executableFile for deeply nested executable: ${cmd.name()}`);
|
233 | }
|
234 | checkExplicitNames(cmd.commands);
|
235 | });
|
236 | }
|
237 | checkExplicitNames(cmd.commands);
|
238 |
|
239 | opts = opts || {};
|
240 | if (opts.isDefault) this._defaultCommandName = cmd._name;
|
241 | if (opts.noHelp || opts.hidden) cmd._hidden = true;
|
242 |
|
243 | this.commands.push(cmd);
|
244 | cmd.parent = this;
|
245 | return this;
|
246 | };
|
247 |
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | arguments(desc) {
|
255 | return this._parseExpectedArgs(desc.split(/ +/));
|
256 | };
|
257 |
|
258 | |
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | addHelpCommand(enableOrNameAndArgs, description) {
|
270 | if (enableOrNameAndArgs === false) {
|
271 | this._hasImplicitHelpCommand = false;
|
272 | } else {
|
273 | this._hasImplicitHelpCommand = true;
|
274 | if (typeof enableOrNameAndArgs === 'string') {
|
275 | this._helpCommandName = enableOrNameAndArgs.split(' ')[0];
|
276 | this._helpCommandnameAndArgs = enableOrNameAndArgs;
|
277 | }
|
278 | this._helpCommandDescription = description || this._helpCommandDescription;
|
279 | }
|
280 | return this;
|
281 | };
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 | _lazyHasImplicitHelpCommand() {
|
289 | if (this._hasImplicitHelpCommand === undefined) {
|
290 | this._hasImplicitHelpCommand = this.commands.length && !this._actionHandler && !this._findCommand('help');
|
291 | }
|
292 | return this._hasImplicitHelpCommand;
|
293 | };
|
294 |
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | _parseExpectedArgs(args) {
|
306 | if (!args.length) return;
|
307 | args.forEach((arg) => {
|
308 | const argDetails = {
|
309 | required: false,
|
310 | name: '',
|
311 | variadic: false
|
312 | };
|
313 |
|
314 | switch (arg[0]) {
|
315 | case '<':
|
316 | argDetails.required = true;
|
317 | argDetails.name = arg.slice(1, -1);
|
318 | break;
|
319 | case '[':
|
320 | argDetails.name = arg.slice(1, -1);
|
321 | break;
|
322 | }
|
323 |
|
324 | if (argDetails.name.length > 3 && argDetails.name.slice(-3) === '...') {
|
325 | argDetails.variadic = true;
|
326 | argDetails.name = argDetails.name.slice(0, -3);
|
327 | }
|
328 | if (argDetails.name) {
|
329 | this._args.push(argDetails);
|
330 | }
|
331 | });
|
332 | this._args.forEach((arg, i) => {
|
333 | if (arg.variadic && i < this._args.length - 1) {
|
334 | throw new Error(`only the last argument can be variadic '${arg.name}'`);
|
335 | }
|
336 | });
|
337 | return this;
|
338 | };
|
339 |
|
340 | |
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | exitOverride(fn) {
|
349 | if (fn) {
|
350 | this._exitCallback = fn;
|
351 | } else {
|
352 | this._exitCallback = (err) => {
|
353 | if (err.code !== 'commander.executeSubCommandAsync') {
|
354 | throw err;
|
355 | } else {
|
356 |
|
357 | }
|
358 | };
|
359 | }
|
360 | return this;
|
361 | };
|
362 |
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | _exit(exitCode, code, message) {
|
374 | if (this._exitCallback) {
|
375 | this._exitCallback(new CommanderError(exitCode, code, message));
|
376 |
|
377 | }
|
378 | process.exit(exitCode);
|
379 | };
|
380 |
|
381 | |
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | action(fn) {
|
399 | const listener = (args) => {
|
400 |
|
401 | const expectedArgsCount = this._args.length;
|
402 | const actionArgs = args.slice(0, expectedArgsCount);
|
403 | if (this._passCommandToAction) {
|
404 | actionArgs[expectedArgsCount] = this;
|
405 | } else {
|
406 | actionArgs[expectedArgsCount] = this.opts();
|
407 | }
|
408 |
|
409 | if (args.length > expectedArgsCount) {
|
410 | actionArgs.push(args.slice(expectedArgsCount));
|
411 | }
|
412 |
|
413 | const actionResult = fn.apply(this, actionArgs);
|
414 |
|
415 | let rootCommand = this;
|
416 | while (rootCommand.parent) {
|
417 | rootCommand = rootCommand.parent;
|
418 | }
|
419 | rootCommand._actionResults.push(actionResult);
|
420 | };
|
421 | this._actionHandler = listener;
|
422 | return this;
|
423 | };
|
424 |
|
425 | |
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | _optionEx(config, flags, description, fn, defaultValue) {
|
438 | const option = new Option(flags, description);
|
439 | const oname = option.name();
|
440 | const name = option.attributeName();
|
441 | option.mandatory = !!config.mandatory;
|
442 |
|
443 |
|
444 | if (typeof fn !== 'function') {
|
445 | if (fn instanceof RegExp) {
|
446 |
|
447 |
|
448 | const regex = fn;
|
449 | fn = (val, def) => {
|
450 | const m = regex.exec(val);
|
451 | return m ? m[0] : def;
|
452 | };
|
453 | } else {
|
454 | defaultValue = fn;
|
455 | fn = null;
|
456 | }
|
457 | }
|
458 |
|
459 |
|
460 | if (option.negate || option.optional || option.required || typeof defaultValue === 'boolean') {
|
461 |
|
462 | if (option.negate) {
|
463 | const positiveLongFlag = option.long.replace(/^--no-/, '--');
|
464 | defaultValue = this._findOption(positiveLongFlag) ? this._getOptionValue(name) : true;
|
465 | }
|
466 |
|
467 | if (defaultValue !== undefined) {
|
468 | this._setOptionValue(name, defaultValue);
|
469 | option.defaultValue = defaultValue;
|
470 | }
|
471 | }
|
472 |
|
473 |
|
474 | this.options.push(option);
|
475 |
|
476 |
|
477 |
|
478 | this.on('option:' + oname, (val) => {
|
479 |
|
480 | if (val !== null && fn) {
|
481 | val = fn(val, this._getOptionValue(name) === undefined ? defaultValue : this._getOptionValue(name));
|
482 | }
|
483 |
|
484 |
|
485 | if (typeof this._getOptionValue(name) === 'boolean' || typeof this._getOptionValue(name) === 'undefined') {
|
486 |
|
487 | if (val == null) {
|
488 | this._setOptionValue(name, option.negate
|
489 | ? false
|
490 | : defaultValue || true);
|
491 | } else {
|
492 | this._setOptionValue(name, val);
|
493 | }
|
494 | } else if (val !== null) {
|
495 |
|
496 | this._setOptionValue(name, option.negate ? false : val);
|
497 | }
|
498 | });
|
499 |
|
500 | return this;
|
501 | };
|
502 |
|
503 | |
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 | option(flags, description, fn, defaultValue) {
|
556 | return this._optionEx({}, flags, description, fn, defaultValue);
|
557 | };
|
558 |
|
559 | |
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 | requiredOption(flags, description, fn, defaultValue) {
|
574 | return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);
|
575 | };
|
576 |
|
577 | |
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 | allowUnknownOption(arg) {
|
585 | this._allowUnknownOption = (arg === undefined) || arg;
|
586 | return this;
|
587 | };
|
588 |
|
589 | |
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 | storeOptionsAsProperties(value) {
|
599 | this._storeOptionsAsProperties = (value === undefined) || value;
|
600 | if (this.options.length) {
|
601 | throw new Error('call .storeOptionsAsProperties() before adding options');
|
602 | }
|
603 | return this;
|
604 | };
|
605 |
|
606 | |
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 | passCommandToAction(value) {
|
616 | this._passCommandToAction = (value === undefined) || value;
|
617 | return this;
|
618 | };
|
619 |
|
620 | |
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | _setOptionValue(key, value) {
|
629 | if (this._storeOptionsAsProperties) {
|
630 | this[key] = value;
|
631 | } else {
|
632 | this._optionValues[key] = value;
|
633 | }
|
634 | };
|
635 |
|
636 | |
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 | _getOptionValue(key) {
|
645 | if (this._storeOptionsAsProperties) {
|
646 | return this[key];
|
647 | }
|
648 | return this._optionValues[key];
|
649 | };
|
650 |
|
651 | |
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | parse(argv, parseOptions) {
|
671 | if (argv !== undefined && !Array.isArray(argv)) {
|
672 | throw new Error('first parameter to parse must be array or undefined');
|
673 | }
|
674 | parseOptions = parseOptions || {};
|
675 |
|
676 |
|
677 | if (argv === undefined) {
|
678 | argv = process.argv;
|
679 |
|
680 | if (process.versions && process.versions.electron) {
|
681 | parseOptions.from = 'electron';
|
682 | }
|
683 | }
|
684 | this.rawArgs = argv.slice();
|
685 |
|
686 |
|
687 | let userArgs;
|
688 | switch (parseOptions.from) {
|
689 | case undefined:
|
690 | case 'node':
|
691 | this._scriptPath = argv[1];
|
692 | userArgs = argv.slice(2);
|
693 | break;
|
694 | case 'electron':
|
695 |
|
696 | if (process.defaultApp) {
|
697 | this._scriptPath = argv[1];
|
698 | userArgs = argv.slice(2);
|
699 | } else {
|
700 | userArgs = argv.slice(1);
|
701 | }
|
702 | break;
|
703 | case 'user':
|
704 | userArgs = argv.slice(0);
|
705 | break;
|
706 | default:
|
707 | throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
708 | }
|
709 | if (!this._scriptPath && process.mainModule) {
|
710 | this._scriptPath = process.mainModule.filename;
|
711 | }
|
712 |
|
713 |
|
714 | this._name = this._name || (this._scriptPath && path.basename(this._scriptPath, path.extname(this._scriptPath)));
|
715 |
|
716 |
|
717 | this._parseCommand([], userArgs);
|
718 |
|
719 | return this;
|
720 | };
|
721 |
|
722 | |
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 | parseAsync(argv, parseOptions) {
|
744 | this.parse(argv, parseOptions);
|
745 | return Promise.all(this._actionResults).then(() => this);
|
746 | };
|
747 |
|
748 | |
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 | _executeSubCommand(subcommand, args) {
|
755 | args = args.slice();
|
756 | let launchWithNode = false;
|
757 | const sourceExt = ['.js', '.ts', '.mjs'];
|
758 |
|
759 |
|
760 | this._checkForMissingMandatoryOptions();
|
761 |
|
762 |
|
763 | const scriptPath = this._scriptPath;
|
764 |
|
765 | let baseDir;
|
766 | try {
|
767 | const resolvedLink = fs.realpathSync(scriptPath);
|
768 | baseDir = path.dirname(resolvedLink);
|
769 | } catch (e) {
|
770 | baseDir = '.';
|
771 | }
|
772 |
|
773 |
|
774 | let bin = path.basename(scriptPath, path.extname(scriptPath)) + '-' + subcommand._name;
|
775 | if (subcommand._executableFile) {
|
776 | bin = subcommand._executableFile;
|
777 | }
|
778 |
|
779 | const localBin = path.join(baseDir, bin);
|
780 | if (fs.existsSync(localBin)) {
|
781 |
|
782 | bin = localBin;
|
783 | } else {
|
784 |
|
785 | sourceExt.forEach((ext) => {
|
786 | if (fs.existsSync(`${localBin}${ext}`)) {
|
787 | bin = `${localBin}${ext}`;
|
788 | }
|
789 | });
|
790 | }
|
791 | launchWithNode = sourceExt.includes(path.extname(bin));
|
792 |
|
793 | let proc;
|
794 | if (process.platform !== 'win32') {
|
795 | if (launchWithNode) {
|
796 | args.unshift(bin);
|
797 |
|
798 | args = incrementNodeInspectorPort(process.execArgv).concat(args);
|
799 |
|
800 | proc = spawn(process.argv[0], args, { stdio: 'inherit' });
|
801 | } else {
|
802 | proc = spawn(bin, args, { stdio: 'inherit' });
|
803 | }
|
804 | } else {
|
805 | args.unshift(bin);
|
806 |
|
807 | args = incrementNodeInspectorPort(process.execArgv).concat(args);
|
808 | proc = spawn(process.execPath, args, { stdio: 'inherit' });
|
809 | }
|
810 |
|
811 | const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
|
812 | signals.forEach((signal) => {
|
813 |
|
814 | process.on(signal, () => {
|
815 | if (proc.killed === false && proc.exitCode === null) {
|
816 | proc.kill(signal);
|
817 | }
|
818 | });
|
819 | });
|
820 |
|
821 |
|
822 |
|
823 | const exitCallback = this._exitCallback;
|
824 | if (!exitCallback) {
|
825 | proc.on('close', process.exit.bind(process));
|
826 | } else {
|
827 | proc.on('close', () => {
|
828 | exitCallback(new CommanderError(process.exitCode || 0, 'commander.executeSubCommandAsync', '(close)'));
|
829 | });
|
830 | }
|
831 | proc.on('error', (err) => {
|
832 |
|
833 | if (err.code === 'ENOENT') {
|
834 | const executableMissing = `'${bin}' does not exist
|
835 | - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
836 | - if the default executable name is not suitable, use the executableFile option to supply a custom name`;
|
837 | throw new Error(executableMissing);
|
838 |
|
839 | } else if (err.code === 'EACCES') {
|
840 | throw new Error(`'${bin}' not executable`);
|
841 | }
|
842 | if (!exitCallback) {
|
843 | process.exit(1);
|
844 | } else {
|
845 | const wrappedError = new CommanderError(1, 'commander.executeSubCommandAsync', '(error)');
|
846 | wrappedError.nestedError = err;
|
847 | exitCallback(wrappedError);
|
848 | }
|
849 | });
|
850 |
|
851 |
|
852 | this.runningCommand = proc;
|
853 | };
|
854 |
|
855 | |
856 |
|
857 |
|
858 | _dispatchSubcommand(commandName, operands, unknown) {
|
859 | const subCommand = this._findCommand(commandName);
|
860 | if (!subCommand) this._helpAndError();
|
861 |
|
862 | if (subCommand._executableHandler) {
|
863 | this._executeSubCommand(subCommand, operands.concat(unknown));
|
864 | } else {
|
865 | subCommand._parseCommand(operands, unknown);
|
866 | }
|
867 | };
|
868 |
|
869 | |
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 | _parseCommand(operands, unknown) {
|
876 | const parsed = this.parseOptions(unknown);
|
877 | operands = operands.concat(parsed.operands);
|
878 | unknown = parsed.unknown;
|
879 | this.args = operands.concat(unknown);
|
880 |
|
881 | if (operands && this._findCommand(operands[0])) {
|
882 | this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
883 | } else if (this._lazyHasImplicitHelpCommand() && operands[0] === this._helpCommandName) {
|
884 | if (operands.length === 1) {
|
885 | this.help();
|
886 | } else {
|
887 | this._dispatchSubcommand(operands[1], [], [this._helpLongFlag]);
|
888 | }
|
889 | } else if (this._defaultCommandName) {
|
890 | outputHelpIfRequested(this, unknown);
|
891 | this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
892 | } else {
|
893 | if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
894 |
|
895 | this._helpAndError();
|
896 | }
|
897 |
|
898 | outputHelpIfRequested(this, parsed.unknown);
|
899 | this._checkForMissingMandatoryOptions();
|
900 | if (parsed.unknown.length > 0) {
|
901 | this.unknownOption(parsed.unknown[0]);
|
902 | }
|
903 |
|
904 | if (this._actionHandler) {
|
905 | const args = this.args.slice();
|
906 | this._args.forEach((arg, i) => {
|
907 | if (arg.required && args[i] == null) {
|
908 | this.missingArgument(arg.name);
|
909 | } else if (arg.variadic) {
|
910 | args[i] = args.splice(i);
|
911 | }
|
912 | });
|
913 |
|
914 | this._actionHandler(args);
|
915 | this.emit('command:' + this.name(), operands, unknown);
|
916 | } else if (operands.length) {
|
917 | if (this._findCommand('*')) {
|
918 | this._dispatchSubcommand('*', operands, unknown);
|
919 | } else if (this.listenerCount('command:*')) {
|
920 | this.emit('command:*', operands, unknown);
|
921 | } else if (this.commands.length) {
|
922 | this.unknownCommand();
|
923 | }
|
924 | } else if (this.commands.length) {
|
925 |
|
926 | this._helpAndError();
|
927 | } else {
|
928 |
|
929 | }
|
930 | }
|
931 | };
|
932 |
|
933 | |
934 |
|
935 |
|
936 |
|
937 |
|
938 | _findCommand(name) {
|
939 | if (!name) return undefined;
|
940 | return this.commands.find(cmd => cmd._name === name || cmd._aliases.includes(name));
|
941 | };
|
942 |
|
943 | |
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 | _findOption(arg) {
|
952 | return this.options.find(option => option.is(arg));
|
953 | };
|
954 |
|
955 | |
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 | _checkForMissingMandatoryOptions() {
|
963 |
|
964 | for (let cmd = this; cmd; cmd = cmd.parent) {
|
965 | cmd.options.forEach((anOption) => {
|
966 | if (anOption.mandatory && (cmd._getOptionValue(anOption.attributeName()) === undefined)) {
|
967 | cmd.missingMandatoryOptionValue(anOption);
|
968 | }
|
969 | });
|
970 | }
|
971 | };
|
972 |
|
973 | |
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 | parseOptions(argv) {
|
991 | const operands = [];
|
992 | const unknown = [];
|
993 | let dest = operands;
|
994 | const args = argv.slice();
|
995 |
|
996 | function maybeOption(arg) {
|
997 | return arg.length > 1 && arg[0] === '-';
|
998 | }
|
999 |
|
1000 |
|
1001 | while (args.length) {
|
1002 | const arg = args.shift();
|
1003 |
|
1004 |
|
1005 | if (arg === '--') {
|
1006 | if (dest === unknown) dest.push(arg);
|
1007 | dest.push(...args);
|
1008 | break;
|
1009 | }
|
1010 |
|
1011 | if (maybeOption(arg)) {
|
1012 | const option = this._findOption(arg);
|
1013 |
|
1014 | if (option) {
|
1015 | if (option.required) {
|
1016 | const value = args.shift();
|
1017 | if (value === undefined) this.optionMissingArgument(option);
|
1018 | this.emit(`option:${option.name()}`, value);
|
1019 | } else if (option.optional) {
|
1020 | let value = null;
|
1021 |
|
1022 | if (args.length > 0 && !maybeOption(args[0])) {
|
1023 | value = args.shift();
|
1024 | }
|
1025 | this.emit(`option:${option.name()}`, value);
|
1026 | } else {
|
1027 | this.emit(`option:${option.name()}`);
|
1028 | }
|
1029 | continue;
|
1030 | }
|
1031 | }
|
1032 |
|
1033 |
|
1034 | if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {
|
1035 | const option = this._findOption(`-${arg[1]}`);
|
1036 | if (option) {
|
1037 | if (option.required || option.optional) {
|
1038 |
|
1039 | this.emit(`option:${option.name()}`, arg.slice(2));
|
1040 | } else {
|
1041 |
|
1042 | this.emit(`option:${option.name()}`);
|
1043 | args.unshift(`-${arg.slice(2)}`);
|
1044 | }
|
1045 | continue;
|
1046 | }
|
1047 | }
|
1048 |
|
1049 |
|
1050 | if (/^--[^=]+=/.test(arg)) {
|
1051 | const index = arg.indexOf('=');
|
1052 | const option = this._findOption(arg.slice(0, index));
|
1053 | if (option && (option.required || option.optional)) {
|
1054 | this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
1055 | continue;
|
1056 | }
|
1057 | }
|
1058 |
|
1059 |
|
1060 | if (arg.length > 1 && arg[0] === '-') {
|
1061 | dest = unknown;
|
1062 | }
|
1063 |
|
1064 |
|
1065 | dest.push(arg);
|
1066 | }
|
1067 |
|
1068 | return { operands, unknown };
|
1069 | };
|
1070 |
|
1071 | |
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 | opts() {
|
1078 | if (this._storeOptionsAsProperties) {
|
1079 |
|
1080 | const result = {};
|
1081 | const len = this.options.length;
|
1082 |
|
1083 | for (let i = 0; i < len; i++) {
|
1084 | const key = this.options[i].attributeName();
|
1085 | result[key] = key === this._versionOptionName ? this._version : this[key];
|
1086 | }
|
1087 | return result;
|
1088 | }
|
1089 |
|
1090 | return this._optionValues;
|
1091 | };
|
1092 |
|
1093 | |
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 | missingArgument(name) {
|
1101 | const message = `error: missing required argument '${name}'`;
|
1102 | console.error(message);
|
1103 | this._exit(1, 'commander.missingArgument', message);
|
1104 | };
|
1105 |
|
1106 | |
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 | optionMissingArgument(option, flag) {
|
1115 | let message;
|
1116 | if (flag) {
|
1117 | message = `error: option '${option.flags}' argument missing, got '${flag}'`;
|
1118 | } else {
|
1119 | message = `error: option '${option.flags}' argument missing`;
|
1120 | }
|
1121 | console.error(message);
|
1122 | this._exit(1, 'commander.optionMissingArgument', message);
|
1123 | };
|
1124 |
|
1125 | |
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 | missingMandatoryOptionValue(option) {
|
1133 | const message = `error: required option '${option.flags}' not specified`;
|
1134 | console.error(message);
|
1135 | this._exit(1, 'commander.missingMandatoryOptionValue', message);
|
1136 | };
|
1137 |
|
1138 | |
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 | unknownOption(flag) {
|
1146 | if (this._allowUnknownOption) return;
|
1147 | const message = `error: unknown option '${flag}'`;
|
1148 | console.error(message);
|
1149 | this._exit(1, 'commander.unknownOption', message);
|
1150 | };
|
1151 |
|
1152 | |
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 | unknownCommand() {
|
1159 | const partCommands = [this.name()];
|
1160 | for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
|
1161 | partCommands.unshift(parentCmd.name());
|
1162 | }
|
1163 | const fullCommand = partCommands.join(' ');
|
1164 | const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} ${this._helpLongFlag}'.`;
|
1165 | console.error(message);
|
1166 | this._exit(1, 'commander.unknownCommand', message);
|
1167 | };
|
1168 |
|
1169 | |
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 | version(str, flags, description) {
|
1185 | if (str === undefined) return this._version;
|
1186 | this._version = str;
|
1187 | flags = flags || '-V, --version';
|
1188 | description = description || 'output the version number';
|
1189 | const versionOption = new Option(flags, description);
|
1190 | this._versionOptionName = versionOption.long.substr(2) || 'version';
|
1191 | this.options.push(versionOption);
|
1192 | this.on('option:' + this._versionOptionName, () => {
|
1193 | process.stdout.write(str + '\n');
|
1194 | this._exit(0, 'commander.version', str);
|
1195 | });
|
1196 | return this;
|
1197 | };
|
1198 |
|
1199 | |
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 | description(str, argsDescription) {
|
1209 | if (str === undefined && argsDescription === undefined) return this._description;
|
1210 | this._description = str;
|
1211 | this._argsDescription = argsDescription;
|
1212 | return this;
|
1213 | };
|
1214 |
|
1215 | |
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 | alias(alias) {
|
1226 | if (alias === undefined) return this._aliases[0];
|
1227 |
|
1228 | let command = this;
|
1229 | if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
1230 |
|
1231 | command = this.commands[this.commands.length - 1];
|
1232 | }
|
1233 |
|
1234 | if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
|
1235 |
|
1236 | command._aliases.push(alias);
|
1237 | return this;
|
1238 | };
|
1239 |
|
1240 | |
1241 |
|
1242 |
|
1243 |
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 | aliases(aliases) {
|
1251 |
|
1252 | if (aliases === undefined) return this._aliases;
|
1253 |
|
1254 | aliases.forEach((alias) => this.alias(alias));
|
1255 | return this;
|
1256 | };
|
1257 |
|
1258 | |
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 | usage(str) {
|
1267 | if (str === undefined) {
|
1268 | if (this._usage) return this._usage;
|
1269 |
|
1270 | const args = this._args.map((arg) => {
|
1271 | return humanReadableArgName(arg);
|
1272 | });
|
1273 | return '[options]' +
|
1274 | (this.commands.length ? ' [command]' : '') +
|
1275 | (this._args.length ? ' ' + args.join(' ') : '');
|
1276 | }
|
1277 |
|
1278 | this._usage = str;
|
1279 | return this;
|
1280 | };
|
1281 |
|
1282 | |
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 | name(str) {
|
1291 | if (str === undefined) return this._name;
|
1292 | this._name = str;
|
1293 | return this;
|
1294 | };
|
1295 |
|
1296 | |
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 | prepareCommands() {
|
1304 | const commandDetails = this.commands.filter((cmd) => {
|
1305 | return !cmd._hidden;
|
1306 | }).map((cmd) => {
|
1307 | const args = cmd._args.map((arg) => {
|
1308 | return humanReadableArgName(arg);
|
1309 | }).join(' ');
|
1310 |
|
1311 | return [
|
1312 | cmd._name +
|
1313 | (cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +
|
1314 | (cmd.options.length ? ' [options]' : '') +
|
1315 | (args ? ' ' + args : ''),
|
1316 | cmd._description
|
1317 | ];
|
1318 | });
|
1319 |
|
1320 | if (this._lazyHasImplicitHelpCommand()) {
|
1321 | commandDetails.push([this._helpCommandnameAndArgs, this._helpCommandDescription]);
|
1322 | }
|
1323 | return commandDetails;
|
1324 | };
|
1325 |
|
1326 | |
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 | largestCommandLength() {
|
1334 | const commands = this.prepareCommands();
|
1335 | return commands.reduce((max, command) => {
|
1336 | return Math.max(max, command[0].length);
|
1337 | }, 0);
|
1338 | };
|
1339 |
|
1340 | |
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 | largestOptionLength() {
|
1348 | const options = [].slice.call(this.options);
|
1349 | options.push({
|
1350 | flags: this._helpFlags
|
1351 | });
|
1352 |
|
1353 | return options.reduce((max, option) => {
|
1354 | return Math.max(max, option.flags.length);
|
1355 | }, 0);
|
1356 | };
|
1357 |
|
1358 | |
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 | largestArgLength() {
|
1366 | return this._args.reduce((max, arg) => {
|
1367 | return Math.max(max, arg.name.length);
|
1368 | }, 0);
|
1369 | };
|
1370 |
|
1371 | |
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 |
|
1378 | padWidth() {
|
1379 | let width = this.largestOptionLength();
|
1380 | if (this._argsDescription && this._args.length) {
|
1381 | if (this.largestArgLength() > width) {
|
1382 | width = this.largestArgLength();
|
1383 | }
|
1384 | }
|
1385 |
|
1386 | if (this.commands && this.commands.length) {
|
1387 | if (this.largestCommandLength() > width) {
|
1388 | width = this.largestCommandLength();
|
1389 | }
|
1390 | }
|
1391 |
|
1392 | return width;
|
1393 | };
|
1394 |
|
1395 | |
1396 |
|
1397 |
|
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 | optionHelp() {
|
1403 | const width = this.padWidth();
|
1404 | const columns = process.stdout.columns || 80;
|
1405 | const descriptionWidth = columns - width - 4;
|
1406 | function padOptionDetails(flags, description) {
|
1407 | return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
|
1408 | };
|
1409 |
|
1410 |
|
1411 | const help = this.options.map((option) => {
|
1412 | const fullDesc = option.description +
|
1413 | ((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
|
1414 | return padOptionDetails(option.flags, fullDesc);
|
1415 | });
|
1416 |
|
1417 |
|
1418 | const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
|
1419 | const showLongHelpFlag = !this._findOption(this._helpLongFlag);
|
1420 | if (showShortHelpFlag || showLongHelpFlag) {
|
1421 | let helpFlags = this._helpFlags;
|
1422 | if (!showShortHelpFlag) {
|
1423 | helpFlags = this._helpLongFlag;
|
1424 | } else if (!showLongHelpFlag) {
|
1425 | helpFlags = this._helpShortFlag;
|
1426 | }
|
1427 | help.push(padOptionDetails(helpFlags, this._helpDescription));
|
1428 | }
|
1429 |
|
1430 | return help.join('\n');
|
1431 | };
|
1432 |
|
1433 | |
1434 |
|
1435 |
|
1436 |
|
1437 |
|
1438 |
|
1439 |
|
1440 | commandHelp() {
|
1441 | if (!this.commands.length && !this._lazyHasImplicitHelpCommand()) return '';
|
1442 |
|
1443 | const commands = this.prepareCommands();
|
1444 | const width = this.padWidth();
|
1445 |
|
1446 | const columns = process.stdout.columns || 80;
|
1447 | const descriptionWidth = columns - width - 4;
|
1448 |
|
1449 | return [
|
1450 | 'Commands:',
|
1451 | commands.map((cmd) => {
|
1452 | const desc = cmd[1] ? ' ' + cmd[1] : '';
|
1453 | return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);
|
1454 | }).join('\n').replace(/^/gm, ' '),
|
1455 | ''
|
1456 | ].join('\n');
|
1457 | };
|
1458 |
|
1459 | |
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 | helpInformation() {
|
1467 | let desc = [];
|
1468 | if (this._description) {
|
1469 | desc = [
|
1470 | this._description,
|
1471 | ''
|
1472 | ];
|
1473 |
|
1474 | const argsDescription = this._argsDescription;
|
1475 | if (argsDescription && this._args.length) {
|
1476 | const width = this.padWidth();
|
1477 | const columns = process.stdout.columns || 80;
|
1478 | const descriptionWidth = columns - width - 5;
|
1479 | desc.push('Arguments:');
|
1480 | desc.push('');
|
1481 | this._args.forEach((arg) => {
|
1482 | desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));
|
1483 | });
|
1484 | desc.push('');
|
1485 | }
|
1486 | }
|
1487 |
|
1488 | let cmdName = this._name;
|
1489 | if (this._aliases[0]) {
|
1490 | cmdName = cmdName + '|' + this._aliases[0];
|
1491 | }
|
1492 | let parentCmdNames = '';
|
1493 | for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
|
1494 | parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
|
1495 | }
|
1496 | const usage = [
|
1497 | 'Usage: ' + parentCmdNames + cmdName + ' ' + this.usage(),
|
1498 | ''
|
1499 | ];
|
1500 |
|
1501 | let cmds = [];
|
1502 | const commandHelp = this.commandHelp();
|
1503 | if (commandHelp) cmds = [commandHelp];
|
1504 |
|
1505 | const options = [
|
1506 | 'Options:',
|
1507 | '' + this.optionHelp().replace(/^/gm, ' '),
|
1508 | ''
|
1509 | ];
|
1510 |
|
1511 | return usage
|
1512 | .concat(desc)
|
1513 | .concat(options)
|
1514 | .concat(cmds)
|
1515 | .join('\n');
|
1516 | };
|
1517 |
|
1518 | |
1519 |
|
1520 |
|
1521 |
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 | outputHelp(cb) {
|
1528 | if (!cb) {
|
1529 | cb = (passthru) => {
|
1530 | return passthru;
|
1531 | };
|
1532 | }
|
1533 | const cbOutput = cb(this.helpInformation());
|
1534 | if (typeof cbOutput !== 'string' && !Buffer.isBuffer(cbOutput)) {
|
1535 | throw new Error('outputHelp callback must return a string or a Buffer');
|
1536 | }
|
1537 | process.stdout.write(cbOutput);
|
1538 | this.emit(this._helpLongFlag);
|
1539 | };
|
1540 |
|
1541 | |
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 | helpOption(flags, description) {
|
1552 | this._helpFlags = flags || this._helpFlags;
|
1553 | this._helpDescription = description || this._helpDescription;
|
1554 |
|
1555 | const splitFlags = this._helpFlags.split(/[ ,|]+/);
|
1556 |
|
1557 | this._helpShortFlag = undefined;
|
1558 | if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();
|
1559 |
|
1560 | this._helpLongFlag = splitFlags.shift();
|
1561 |
|
1562 | return this;
|
1563 | };
|
1564 |
|
1565 | |
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 | help(cb) {
|
1573 | this.outputHelp(cb);
|
1574 |
|
1575 |
|
1576 | this._exit(process.exitCode || 0, 'commander.help', '(outputHelp)');
|
1577 | };
|
1578 |
|
1579 | |
1580 |
|
1581 |
|
1582 |
|
1583 |
|
1584 |
|
1585 | _helpAndError() {
|
1586 | this.outputHelp();
|
1587 |
|
1588 | this._exit(1, 'commander.help', '(outputHelp)');
|
1589 | };
|
1590 | };
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 | exports = module.exports = new Command();
|
1597 | exports.program = exports;
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 | exports.Command = Command;
|
1604 | exports.Option = Option;
|
1605 | exports.CommanderError = CommanderError;
|
1606 |
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 | function camelcase(flag) {
|
1616 | return flag.split('-').reduce((str, word) => {
|
1617 | return str + word[0].toUpperCase() + word.slice(1);
|
1618 | });
|
1619 | }
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 | function pad(str, width) {
|
1631 | const len = Math.max(0, width - str.length);
|
1632 | return str + Array(len + 1).join(' ');
|
1633 | }
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 |
|
1644 |
|
1645 | function wrap(str, width, indent) {
|
1646 | const regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');
|
1647 | const lines = str.match(regex) || [];
|
1648 | return lines.map((line, i) => {
|
1649 | if (line.slice(-1) === '\n') {
|
1650 | line = line.slice(0, line.length - 1);
|
1651 | }
|
1652 | return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line.trimRight();
|
1653 | }).join('\n');
|
1654 | }
|
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 |
|
1667 | function optionalWrap(str, width, indent) {
|
1668 |
|
1669 |
|
1670 | if (str.match(/[\n]\s+/)) return str;
|
1671 |
|
1672 | const minWidth = 40;
|
1673 | if (width < minWidth) return str;
|
1674 |
|
1675 | return wrap(str, width, indent);
|
1676 | }
|
1677 |
|
1678 |
|
1679 |
|
1680 |
|
1681 |
|
1682 |
|
1683 |
|
1684 |
|
1685 |
|
1686 | function outputHelpIfRequested(cmd, args) {
|
1687 | const helpOption = args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
|
1688 | if (helpOption) {
|
1689 | cmd.outputHelp();
|
1690 |
|
1691 | cmd._exit(0, 'commander.helpDisplayed', '(outputHelp)');
|
1692 | }
|
1693 | }
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 |
|
1699 |
|
1700 |
|
1701 |
|
1702 |
|
1703 | function humanReadableArgName(arg) {
|
1704 | const nameOutput = arg.name + (arg.variadic === true ? '...' : '');
|
1705 |
|
1706 | return arg.required
|
1707 | ? '<' + nameOutput + '>'
|
1708 | : '[' + nameOutput + ']';
|
1709 | }
|
1710 |
|
1711 |
|
1712 |
|
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 | function incrementNodeInspectorPort(args) {
|
1720 |
|
1721 |
|
1722 |
|
1723 |
|
1724 | return args.map((arg) => {
|
1725 | let result = arg;
|
1726 | if (arg.indexOf('--inspect') === 0) {
|
1727 | let debugOption;
|
1728 | let debugHost = '127.0.0.1';
|
1729 | let debugPort = '9229';
|
1730 | let match;
|
1731 | if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
1732 |
|
1733 | debugOption = match[1];
|
1734 | } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
1735 | debugOption = match[1];
|
1736 | if (/^\d+$/.test(match[3])) {
|
1737 |
|
1738 | debugPort = match[3];
|
1739 | } else {
|
1740 |
|
1741 | debugHost = match[3];
|
1742 | }
|
1743 | } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
1744 |
|
1745 | debugOption = match[1];
|
1746 | debugHost = match[3];
|
1747 | debugPort = match[4];
|
1748 | }
|
1749 |
|
1750 | if (debugOption && debugPort !== '0') {
|
1751 | result = `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
1752 | }
|
1753 | }
|
1754 | return result;
|
1755 | });
|
1756 | }
|