1 | const EventEmitter = require('events').EventEmitter;
|
2 | const childProcess = require('child_process');
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 | const process = require('process');
|
6 |
|
7 | const { Argument, humanReadableArgName } = require('./argument.js');
|
8 | const { CommanderError } = require('./error.js');
|
9 | const { Help } = require('./help.js');
|
10 | const { Option, splitOptionFlags, DualOptions } = require('./option.js');
|
11 | const { suggestSimilar } = require('./suggestSimilar');
|
12 |
|
13 |
|
14 |
|
15 | class Command extends EventEmitter {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | constructor(name) {
|
23 | super();
|
24 |
|
25 | this.commands = [];
|
26 |
|
27 | this.options = [];
|
28 | this.parent = null;
|
29 | this._allowUnknownOption = false;
|
30 | this._allowExcessArguments = true;
|
31 |
|
32 | this._args = [];
|
33 |
|
34 | this.args = [];
|
35 | this.rawArgs = [];
|
36 | this.processedArgs = [];
|
37 | this._scriptPath = null;
|
38 | this._name = name || '';
|
39 | this._optionValues = {};
|
40 | this._optionValueSources = {};
|
41 | this._storeOptionsAsProperties = false;
|
42 | this._actionHandler = null;
|
43 | this._executableHandler = false;
|
44 | this._executableFile = null;
|
45 | this._executableDir = null;
|
46 | this._defaultCommandName = null;
|
47 | this._exitCallback = null;
|
48 | this._aliases = [];
|
49 | this._combineFlagAndOptionalValue = true;
|
50 | this._description = '';
|
51 | this._summary = '';
|
52 | this._argsDescription = undefined;
|
53 | this._enablePositionalOptions = false;
|
54 | this._passThroughOptions = false;
|
55 | this._lifeCycleHooks = {};
|
56 |
|
57 | this._showHelpAfterError = false;
|
58 | this._showSuggestionAfterError = true;
|
59 |
|
60 |
|
61 | this._outputConfiguration = {
|
62 | writeOut: (str) => process.stdout.write(str),
|
63 | writeErr: (str) => process.stderr.write(str),
|
64 | getOutHelpWidth: () => process.stdout.isTTY ? process.stdout.columns : undefined,
|
65 | getErrHelpWidth: () => process.stderr.isTTY ? process.stderr.columns : undefined,
|
66 | outputError: (str, write) => write(str)
|
67 | };
|
68 |
|
69 | this._hidden = false;
|
70 | this._hasHelpOption = true;
|
71 | this._helpFlags = '-h, --help';
|
72 | this._helpDescription = 'display help for command';
|
73 | this._helpShortFlag = '-h';
|
74 | this._helpLongFlag = '--help';
|
75 | this._addImplicitHelpCommand = undefined;
|
76 | this._helpCommandName = 'help';
|
77 | this._helpCommandnameAndArgs = 'help [command]';
|
78 | this._helpCommandDescription = 'display help for command';
|
79 | this._helpConfiguration = {};
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | copyInheritedSettings(sourceCommand) {
|
91 | this._outputConfiguration = sourceCommand._outputConfiguration;
|
92 | this._hasHelpOption = sourceCommand._hasHelpOption;
|
93 | this._helpFlags = sourceCommand._helpFlags;
|
94 | this._helpDescription = sourceCommand._helpDescription;
|
95 | this._helpShortFlag = sourceCommand._helpShortFlag;
|
96 | this._helpLongFlag = sourceCommand._helpLongFlag;
|
97 | this._helpCommandName = sourceCommand._helpCommandName;
|
98 | this._helpCommandnameAndArgs = sourceCommand._helpCommandnameAndArgs;
|
99 | this._helpCommandDescription = sourceCommand._helpCommandDescription;
|
100 | this._helpConfiguration = sourceCommand._helpConfiguration;
|
101 | this._exitCallback = sourceCommand._exitCallback;
|
102 | this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
103 | this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
104 | this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
105 | this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
106 | this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
107 | this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
108 |
|
109 | return this;
|
110 | }
|
111 |
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
138 | let desc = actionOptsOrExecDesc;
|
139 | let opts = execOpts;
|
140 | if (typeof desc === 'object' && desc !== null) {
|
141 | opts = desc;
|
142 | desc = null;
|
143 | }
|
144 | opts = opts || {};
|
145 | const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
146 |
|
147 | const cmd = this.createCommand(name);
|
148 | if (desc) {
|
149 | cmd.description(desc);
|
150 | cmd._executableHandler = true;
|
151 | }
|
152 | if (opts.isDefault) this._defaultCommandName = cmd._name;
|
153 | cmd._hidden = !!(opts.noHelp || opts.hidden);
|
154 | cmd._executableFile = opts.executableFile || null;
|
155 | if (args) cmd.arguments(args);
|
156 | this.commands.push(cmd);
|
157 | cmd.parent = this;
|
158 | cmd.copyInheritedSettings(this);
|
159 |
|
160 | if (desc) return this;
|
161 | return cmd;
|
162 | }
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | createCommand(name) {
|
175 | return new Command(name);
|
176 | }
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | createHelp() {
|
186 | return Object.assign(new Help(), this.configureHelp());
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | configureHelp(configuration) {
|
198 | if (configuration === undefined) return this._helpConfiguration;
|
199 |
|
200 | this._helpConfiguration = configuration;
|
201 | return this;
|
202 | }
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | configureOutput(configuration) {
|
224 | if (configuration === undefined) return this._outputConfiguration;
|
225 |
|
226 | Object.assign(this._outputConfiguration, configuration);
|
227 | return this;
|
228 | }
|
229 |
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | showHelpAfterError(displayHelp = true) {
|
237 | if (typeof displayHelp !== 'string') displayHelp = !!displayHelp;
|
238 | this._showHelpAfterError = displayHelp;
|
239 | return this;
|
240 | }
|
241 |
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | showSuggestionAfterError(displaySuggestion = true) {
|
249 | this._showSuggestionAfterError = !!displaySuggestion;
|
250 | return this;
|
251 | }
|
252 |
|
253 | |
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | addCommand(cmd, opts) {
|
264 | if (!cmd._name) {
|
265 | throw new Error(`Command passed to .addCommand() must have a name
|
266 | - specify the name in Command constructor or using .name()`);
|
267 | }
|
268 |
|
269 | opts = opts || {};
|
270 | if (opts.isDefault) this._defaultCommandName = cmd._name;
|
271 | if (opts.noHelp || opts.hidden) cmd._hidden = true;
|
272 |
|
273 | this.commands.push(cmd);
|
274 | cmd.parent = this;
|
275 | return this;
|
276 | }
|
277 |
|
278 | |
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | createArgument(name, description) {
|
290 | return new Argument(name, description);
|
291 | }
|
292 |
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | argument(name, description, fn, defaultValue) {
|
310 | const argument = this.createArgument(name, description);
|
311 | if (typeof fn === 'function') {
|
312 | argument.default(defaultValue).argParser(fn);
|
313 | } else {
|
314 | argument.default(fn);
|
315 | }
|
316 | this.addArgument(argument);
|
317 | return this;
|
318 | }
|
319 |
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | arguments(names) {
|
333 | names.split(/ +/).forEach((detail) => {
|
334 | this.argument(detail);
|
335 | });
|
336 | return this;
|
337 | }
|
338 |
|
339 | |
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | addArgument(argument) {
|
346 | const previousArgument = this._args.slice(-1)[0];
|
347 | if (previousArgument && previousArgument.variadic) {
|
348 | throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
349 | }
|
350 | if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
351 | throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
352 | }
|
353 | this._args.push(argument);
|
354 | return this;
|
355 | }
|
356 |
|
357 | |
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | addHelpCommand(enableOrNameAndArgs, description) {
|
368 | if (enableOrNameAndArgs === false) {
|
369 | this._addImplicitHelpCommand = false;
|
370 | } else {
|
371 | this._addImplicitHelpCommand = true;
|
372 | if (typeof enableOrNameAndArgs === 'string') {
|
373 | this._helpCommandName = enableOrNameAndArgs.split(' ')[0];
|
374 | this._helpCommandnameAndArgs = enableOrNameAndArgs;
|
375 | }
|
376 | this._helpCommandDescription = description || this._helpCommandDescription;
|
377 | }
|
378 | return this;
|
379 | }
|
380 |
|
381 | |
382 |
|
383 |
|
384 |
|
385 |
|
386 | _hasImplicitHelpCommand() {
|
387 | if (this._addImplicitHelpCommand === undefined) {
|
388 | return this.commands.length && !this._actionHandler && !this._findCommand('help');
|
389 | }
|
390 | return this._addImplicitHelpCommand;
|
391 | }
|
392 |
|
393 | |
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | hook(event, listener) {
|
402 | const allowedValues = ['preAction', 'postAction'];
|
403 | if (!allowedValues.includes(event)) {
|
404 | throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
405 | Expecting one of '${allowedValues.join("', '")}'`);
|
406 | }
|
407 | if (this._lifeCycleHooks[event]) {
|
408 | this._lifeCycleHooks[event].push(listener);
|
409 | } else {
|
410 | this._lifeCycleHooks[event] = [listener];
|
411 | }
|
412 | return this;
|
413 | }
|
414 |
|
415 | |
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | exitOverride(fn) {
|
423 | if (fn) {
|
424 | this._exitCallback = fn;
|
425 | } else {
|
426 | this._exitCallback = (err) => {
|
427 | if (err.code !== 'commander.executeSubCommandAsync') {
|
428 | throw err;
|
429 | } else {
|
430 |
|
431 | }
|
432 | };
|
433 | }
|
434 | return this;
|
435 | }
|
436 |
|
437 | |
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | _exit(exitCode, code, message) {
|
448 | if (this._exitCallback) {
|
449 | this._exitCallback(new CommanderError(exitCode, code, message));
|
450 |
|
451 | }
|
452 | process.exit(exitCode);
|
453 | }
|
454 |
|
455 | |
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 | action(fn) {
|
471 | const listener = (args) => {
|
472 |
|
473 | const expectedArgsCount = this._args.length;
|
474 | const actionArgs = args.slice(0, expectedArgsCount);
|
475 | if (this._storeOptionsAsProperties) {
|
476 | actionArgs[expectedArgsCount] = this;
|
477 | } else {
|
478 | actionArgs[expectedArgsCount] = this.opts();
|
479 | }
|
480 | actionArgs.push(this);
|
481 |
|
482 | return fn.apply(this, actionArgs);
|
483 | };
|
484 | this._actionHandler = listener;
|
485 | return this;
|
486 | }
|
487 |
|
488 | |
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 | createOption(flags, description) {
|
500 | return new Option(flags, description);
|
501 | }
|
502 |
|
503 | |
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 | addOption(option) {
|
510 | const oname = option.name();
|
511 | const name = option.attributeName();
|
512 |
|
513 |
|
514 | if (option.negate) {
|
515 |
|
516 | const positiveLongFlag = option.long.replace(/^--no-/, '--');
|
517 | if (!this._findOption(positiveLongFlag)) {
|
518 | this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, 'default');
|
519 | }
|
520 | } else if (option.defaultValue !== undefined) {
|
521 | this.setOptionValueWithSource(name, option.defaultValue, 'default');
|
522 | }
|
523 |
|
524 |
|
525 | this.options.push(option);
|
526 |
|
527 |
|
528 | const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
529 |
|
530 |
|
531 | if (val == null && option.presetArg !== undefined) {
|
532 | val = option.presetArg;
|
533 | }
|
534 |
|
535 |
|
536 | const oldValue = this.getOptionValue(name);
|
537 | if (val !== null && option.parseArg) {
|
538 | try {
|
539 | val = option.parseArg(val, oldValue);
|
540 | } catch (err) {
|
541 | if (err.code === 'commander.invalidArgument') {
|
542 | const message = `${invalidValueMessage} ${err.message}`;
|
543 | this.error(message, { exitCode: err.exitCode, code: err.code });
|
544 | }
|
545 | throw err;
|
546 | }
|
547 | } else if (val !== null && option.variadic) {
|
548 | val = option._concatValue(val, oldValue);
|
549 | }
|
550 |
|
551 |
|
552 | if (val == null) {
|
553 | if (option.negate) {
|
554 | val = false;
|
555 | } else if (option.isBoolean() || option.optional) {
|
556 | val = true;
|
557 | } else {
|
558 | val = '';
|
559 | }
|
560 | }
|
561 | this.setOptionValueWithSource(name, val, valueSource);
|
562 | };
|
563 |
|
564 | this.on('option:' + oname, (val) => {
|
565 | const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
566 | handleOptionValue(val, invalidValueMessage, 'cli');
|
567 | });
|
568 |
|
569 | if (option.envVar) {
|
570 | this.on('optionEnv:' + oname, (val) => {
|
571 | const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
572 | handleOptionValue(val, invalidValueMessage, 'env');
|
573 | });
|
574 | }
|
575 |
|
576 | return this;
|
577 | }
|
578 |
|
579 | |
580 |
|
581 |
|
582 |
|
583 |
|
584 | _optionEx(config, flags, description, fn, defaultValue) {
|
585 | if (typeof flags === 'object' && flags instanceof Option) {
|
586 | throw new Error('To add an Option object use addOption() instead of option() or requiredOption()');
|
587 | }
|
588 | const option = this.createOption(flags, description);
|
589 | option.makeOptionMandatory(!!config.mandatory);
|
590 | if (typeof fn === 'function') {
|
591 | option.default(defaultValue).argParser(fn);
|
592 | } else if (fn instanceof RegExp) {
|
593 |
|
594 | const regex = fn;
|
595 | fn = (val, def) => {
|
596 | const m = regex.exec(val);
|
597 | return m ? m[0] : def;
|
598 | };
|
599 | option.default(defaultValue).argParser(fn);
|
600 | } else {
|
601 | option.default(fn);
|
602 | }
|
603 |
|
604 | return this.addOption(option);
|
605 | }
|
606 |
|
607 | |
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 | option(flags, description, fn, defaultValue) {
|
658 | return this._optionEx({}, flags, description, fn, defaultValue);
|
659 | }
|
660 |
|
661 | |
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 | requiredOption(flags, description, fn, defaultValue) {
|
675 | return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);
|
676 | }
|
677 |
|
678 | |
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | combineFlagAndOptionalValue(combine = true) {
|
689 | this._combineFlagAndOptionalValue = !!combine;
|
690 | return this;
|
691 | }
|
692 |
|
693 | |
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | allowUnknownOption(allowUnknown = true) {
|
700 | this._allowUnknownOption = !!allowUnknown;
|
701 | return this;
|
702 | }
|
703 |
|
704 | |
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 | allowExcessArguments(allowExcess = true) {
|
711 | this._allowExcessArguments = !!allowExcess;
|
712 | return this;
|
713 | }
|
714 |
|
715 | |
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 | enablePositionalOptions(positional = true) {
|
723 | this._enablePositionalOptions = !!positional;
|
724 | return this;
|
725 | }
|
726 |
|
727 | |
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 | passThroughOptions(passThrough = true) {
|
737 | this._passThroughOptions = !!passThrough;
|
738 | if (!!this.parent && passThrough && !this.parent._enablePositionalOptions) {
|
739 | throw new Error('passThroughOptions can not be used without turning on enablePositionalOptions for parent command(s)');
|
740 | }
|
741 | return this;
|
742 | }
|
743 |
|
744 | |
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 | storeOptionsAsProperties(storeAsProperties = true) {
|
753 | this._storeOptionsAsProperties = !!storeAsProperties;
|
754 | if (this.options.length) {
|
755 | throw new Error('call .storeOptionsAsProperties() before adding options');
|
756 | }
|
757 | return this;
|
758 | }
|
759 |
|
760 | |
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 | getOptionValue(key) {
|
768 | if (this._storeOptionsAsProperties) {
|
769 | return this[key];
|
770 | }
|
771 | return this._optionValues[key];
|
772 | }
|
773 |
|
774 | |
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 | setOptionValue(key, value) {
|
783 | if (this._storeOptionsAsProperties) {
|
784 | this[key] = value;
|
785 | } else {
|
786 | this._optionValues[key] = value;
|
787 | }
|
788 | return this;
|
789 | }
|
790 |
|
791 | |
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 | setOptionValueWithSource(key, value, source) {
|
801 | this.setOptionValue(key, value);
|
802 | this._optionValueSources[key] = source;
|
803 | return this;
|
804 | }
|
805 |
|
806 | |
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 | getOptionValueSource(key) {
|
815 | return this._optionValueSources[key];
|
816 | }
|
817 |
|
818 | |
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 | _prepareUserArgs(argv, parseOptions) {
|
826 | if (argv !== undefined && !Array.isArray(argv)) {
|
827 | throw new Error('first parameter to parse must be array or undefined');
|
828 | }
|
829 | parseOptions = parseOptions || {};
|
830 |
|
831 |
|
832 | if (argv === undefined) {
|
833 | argv = process.argv;
|
834 |
|
835 | if (process.versions && process.versions.electron) {
|
836 | parseOptions.from = 'electron';
|
837 | }
|
838 | }
|
839 | this.rawArgs = argv.slice();
|
840 |
|
841 |
|
842 | let userArgs;
|
843 | switch (parseOptions.from) {
|
844 | case undefined:
|
845 | case 'node':
|
846 | this._scriptPath = argv[1];
|
847 | userArgs = argv.slice(2);
|
848 | break;
|
849 | case 'electron':
|
850 |
|
851 | if (process.defaultApp) {
|
852 | this._scriptPath = argv[1];
|
853 | userArgs = argv.slice(2);
|
854 | } else {
|
855 | userArgs = argv.slice(1);
|
856 | }
|
857 | break;
|
858 | case 'user':
|
859 | userArgs = argv.slice(0);
|
860 | break;
|
861 | default:
|
862 | throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
863 | }
|
864 |
|
865 |
|
866 | if (!this._name && this._scriptPath) this.nameFromFilename(this._scriptPath);
|
867 | this._name = this._name || 'program';
|
868 |
|
869 | return userArgs;
|
870 | }
|
871 |
|
872 | |
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 | parse(argv, parseOptions) {
|
890 | const userArgs = this._prepareUserArgs(argv, parseOptions);
|
891 | this._parseCommand([], userArgs);
|
892 |
|
893 | return this;
|
894 | }
|
895 |
|
896 | |
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 | async parseAsync(argv, parseOptions) {
|
916 | const userArgs = this._prepareUserArgs(argv, parseOptions);
|
917 | await this._parseCommand([], userArgs);
|
918 |
|
919 | return this;
|
920 | }
|
921 |
|
922 | |
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 | _executeSubCommand(subcommand, args) {
|
929 | args = args.slice();
|
930 | let launchWithNode = false;
|
931 | const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];
|
932 |
|
933 | function findFile(baseDir, baseName) {
|
934 |
|
935 | const localBin = path.resolve(baseDir, baseName);
|
936 | if (fs.existsSync(localBin)) return localBin;
|
937 |
|
938 |
|
939 | if (sourceExt.includes(path.extname(baseName))) return undefined;
|
940 |
|
941 |
|
942 | const foundExt = sourceExt.find(ext => fs.existsSync(`${localBin}${ext}`));
|
943 | if (foundExt) return `${localBin}${foundExt}`;
|
944 |
|
945 | return undefined;
|
946 | }
|
947 |
|
948 |
|
949 | this._checkForMissingMandatoryOptions();
|
950 | this._checkForConflictingOptions();
|
951 |
|
952 |
|
953 | let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
954 | let executableDir = this._executableDir || '';
|
955 | if (this._scriptPath) {
|
956 | let resolvedScriptPath;
|
957 | try {
|
958 | resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
959 | } catch (err) {
|
960 | resolvedScriptPath = this._scriptPath;
|
961 | }
|
962 | executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
963 | }
|
964 |
|
965 |
|
966 | if (executableDir) {
|
967 | let localFile = findFile(executableDir, executableFile);
|
968 |
|
969 |
|
970 | if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
971 | const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
972 | if (legacyName !== this._name) {
|
973 | localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
974 | }
|
975 | }
|
976 | executableFile = localFile || executableFile;
|
977 | }
|
978 |
|
979 | launchWithNode = sourceExt.includes(path.extname(executableFile));
|
980 |
|
981 | let proc;
|
982 | if (process.platform !== 'win32') {
|
983 | if (launchWithNode) {
|
984 | args.unshift(executableFile);
|
985 |
|
986 | args = incrementNodeInspectorPort(process.execArgv).concat(args);
|
987 |
|
988 | proc = childProcess.spawn(process.argv[0], args, { stdio: 'inherit' });
|
989 | } else {
|
990 | proc = childProcess.spawn(executableFile, args, { stdio: 'inherit' });
|
991 | }
|
992 | } else {
|
993 | args.unshift(executableFile);
|
994 |
|
995 | args = incrementNodeInspectorPort(process.execArgv).concat(args);
|
996 | proc = childProcess.spawn(process.execPath, args, { stdio: 'inherit' });
|
997 | }
|
998 |
|
999 | if (!proc.killed) {
|
1000 | const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
|
1001 | signals.forEach((signal) => {
|
1002 |
|
1003 | process.on(signal, () => {
|
1004 | if (proc.killed === false && proc.exitCode === null) {
|
1005 | proc.kill(signal);
|
1006 | }
|
1007 | });
|
1008 | });
|
1009 | }
|
1010 |
|
1011 |
|
1012 |
|
1013 | const exitCallback = this._exitCallback;
|
1014 | if (!exitCallback) {
|
1015 | proc.on('close', process.exit.bind(process));
|
1016 | } else {
|
1017 | proc.on('close', () => {
|
1018 | exitCallback(new CommanderError(process.exitCode || 0, 'commander.executeSubCommandAsync', '(close)'));
|
1019 | });
|
1020 | }
|
1021 | proc.on('error', (err) => {
|
1022 |
|
1023 | if (err.code === 'ENOENT') {
|
1024 | const executableDirMessage = executableDir
|
1025 | ? `searched for local subcommand relative to directory '${executableDir}'`
|
1026 | : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory';
|
1027 | const executableMissing = `'${executableFile}' does not exist
|
1028 | - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
1029 | - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
1030 | - ${executableDirMessage}`;
|
1031 | throw new Error(executableMissing);
|
1032 |
|
1033 | } else if (err.code === 'EACCES') {
|
1034 | throw new Error(`'${executableFile}' not executable`);
|
1035 | }
|
1036 | if (!exitCallback) {
|
1037 | process.exit(1);
|
1038 | } else {
|
1039 | const wrappedError = new CommanderError(1, 'commander.executeSubCommandAsync', '(error)');
|
1040 | wrappedError.nestedError = err;
|
1041 | exitCallback(wrappedError);
|
1042 | }
|
1043 | });
|
1044 |
|
1045 |
|
1046 | this.runningCommand = proc;
|
1047 | }
|
1048 |
|
1049 | |
1050 |
|
1051 |
|
1052 |
|
1053 | _dispatchSubcommand(commandName, operands, unknown) {
|
1054 | const subCommand = this._findCommand(commandName);
|
1055 | if (!subCommand) this.help({ error: true });
|
1056 |
|
1057 | if (subCommand._executableHandler) {
|
1058 | this._executeSubCommand(subCommand, operands.concat(unknown));
|
1059 | } else {
|
1060 | return subCommand._parseCommand(operands, unknown);
|
1061 | }
|
1062 | }
|
1063 |
|
1064 | |
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 | _checkNumberOfArguments() {
|
1071 |
|
1072 | this._args.forEach((arg, i) => {
|
1073 | if (arg.required && this.args[i] == null) {
|
1074 | this.missingArgument(arg.name());
|
1075 | }
|
1076 | });
|
1077 |
|
1078 | if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {
|
1079 | return;
|
1080 | }
|
1081 | if (this.args.length > this._args.length) {
|
1082 | this._excessArguments(this.args);
|
1083 | }
|
1084 | }
|
1085 |
|
1086 | |
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 | _processArguments() {
|
1093 | const myParseArg = (argument, value, previous) => {
|
1094 |
|
1095 | let parsedValue = value;
|
1096 | if (value !== null && argument.parseArg) {
|
1097 | try {
|
1098 | parsedValue = argument.parseArg(value, previous);
|
1099 | } catch (err) {
|
1100 | if (err.code === 'commander.invalidArgument') {
|
1101 | const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`;
|
1102 | this.error(message, { exitCode: err.exitCode, code: err.code });
|
1103 | }
|
1104 | throw err;
|
1105 | }
|
1106 | }
|
1107 | return parsedValue;
|
1108 | };
|
1109 |
|
1110 | this._checkNumberOfArguments();
|
1111 |
|
1112 | const processedArgs = [];
|
1113 | this._args.forEach((declaredArg, index) => {
|
1114 | let value = declaredArg.defaultValue;
|
1115 | if (declaredArg.variadic) {
|
1116 |
|
1117 | if (index < this.args.length) {
|
1118 | value = this.args.slice(index);
|
1119 | if (declaredArg.parseArg) {
|
1120 | value = value.reduce((processed, v) => {
|
1121 | return myParseArg(declaredArg, v, processed);
|
1122 | }, declaredArg.defaultValue);
|
1123 | }
|
1124 | } else if (value === undefined) {
|
1125 | value = [];
|
1126 | }
|
1127 | } else if (index < this.args.length) {
|
1128 | value = this.args[index];
|
1129 | if (declaredArg.parseArg) {
|
1130 | value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
1131 | }
|
1132 | }
|
1133 | processedArgs[index] = value;
|
1134 | });
|
1135 | this.processedArgs = processedArgs;
|
1136 | }
|
1137 |
|
1138 | |
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 | _chainOrCall(promise, fn) {
|
1148 |
|
1149 | if (promise && promise.then && typeof promise.then === 'function') {
|
1150 |
|
1151 | return promise.then(() => fn());
|
1152 | }
|
1153 |
|
1154 | return fn();
|
1155 | }
|
1156 |
|
1157 | |
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 | _chainOrCallHooks(promise, event) {
|
1166 | let result = promise;
|
1167 | const hooks = [];
|
1168 | getCommandAndParents(this)
|
1169 | .reverse()
|
1170 | .filter(cmd => cmd._lifeCycleHooks[event] !== undefined)
|
1171 | .forEach(hookedCommand => {
|
1172 | hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
1173 | hooks.push({ hookedCommand, callback });
|
1174 | });
|
1175 | });
|
1176 | if (event === 'postAction') {
|
1177 | hooks.reverse();
|
1178 | }
|
1179 |
|
1180 | hooks.forEach((hookDetail) => {
|
1181 | result = this._chainOrCall(result, () => {
|
1182 | return hookDetail.callback(hookDetail.hookedCommand, this);
|
1183 | });
|
1184 | });
|
1185 | return result;
|
1186 | }
|
1187 |
|
1188 | |
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 | _parseCommand(operands, unknown) {
|
1196 | const parsed = this.parseOptions(unknown);
|
1197 | this._parseOptionsEnv();
|
1198 | this._parseOptionsImplied();
|
1199 | operands = operands.concat(parsed.operands);
|
1200 | unknown = parsed.unknown;
|
1201 | this.args = operands.concat(unknown);
|
1202 |
|
1203 | if (operands && this._findCommand(operands[0])) {
|
1204 | return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
1205 | }
|
1206 | if (this._hasImplicitHelpCommand() && operands[0] === this._helpCommandName) {
|
1207 | if (operands.length === 1) {
|
1208 | this.help();
|
1209 | }
|
1210 | return this._dispatchSubcommand(operands[1], [], [this._helpLongFlag]);
|
1211 | }
|
1212 | if (this._defaultCommandName) {
|
1213 | outputHelpIfRequested(this, unknown);
|
1214 | return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
1215 | }
|
1216 | if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
1217 |
|
1218 | this.help({ error: true });
|
1219 | }
|
1220 |
|
1221 | outputHelpIfRequested(this, parsed.unknown);
|
1222 | this._checkForMissingMandatoryOptions();
|
1223 | this._checkForConflictingOptions();
|
1224 |
|
1225 |
|
1226 | const checkForUnknownOptions = () => {
|
1227 | if (parsed.unknown.length > 0) {
|
1228 | this.unknownOption(parsed.unknown[0]);
|
1229 | }
|
1230 | };
|
1231 |
|
1232 | const commandEvent = `command:${this.name()}`;
|
1233 | if (this._actionHandler) {
|
1234 | checkForUnknownOptions();
|
1235 | this._processArguments();
|
1236 |
|
1237 | let actionResult;
|
1238 | actionResult = this._chainOrCallHooks(actionResult, 'preAction');
|
1239 | actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
|
1240 | if (this.parent) {
|
1241 | actionResult = this._chainOrCall(actionResult, () => {
|
1242 | this.parent.emit(commandEvent, operands, unknown);
|
1243 | });
|
1244 | }
|
1245 | actionResult = this._chainOrCallHooks(actionResult, 'postAction');
|
1246 | return actionResult;
|
1247 | }
|
1248 | if (this.parent && this.parent.listenerCount(commandEvent)) {
|
1249 | checkForUnknownOptions();
|
1250 | this._processArguments();
|
1251 | this.parent.emit(commandEvent, operands, unknown);
|
1252 | } else if (operands.length) {
|
1253 | if (this._findCommand('*')) {
|
1254 | return this._dispatchSubcommand('*', operands, unknown);
|
1255 | }
|
1256 | if (this.listenerCount('command:*')) {
|
1257 |
|
1258 | this.emit('command:*', operands, unknown);
|
1259 | } else if (this.commands.length) {
|
1260 | this.unknownCommand();
|
1261 | } else {
|
1262 | checkForUnknownOptions();
|
1263 | this._processArguments();
|
1264 | }
|
1265 | } else if (this.commands.length) {
|
1266 | checkForUnknownOptions();
|
1267 |
|
1268 | this.help({ error: true });
|
1269 | } else {
|
1270 | checkForUnknownOptions();
|
1271 | this._processArguments();
|
1272 |
|
1273 | }
|
1274 | }
|
1275 |
|
1276 | |
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 | _findCommand(name) {
|
1282 | if (!name) return undefined;
|
1283 | return this.commands.find(cmd => cmd._name === name || cmd._aliases.includes(name));
|
1284 | }
|
1285 |
|
1286 | |
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 | _findOption(arg) {
|
1295 | return this.options.find(option => option.is(arg));
|
1296 | }
|
1297 |
|
1298 | |
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 | _checkForMissingMandatoryOptions() {
|
1306 |
|
1307 | for (let cmd = this; cmd; cmd = cmd.parent) {
|
1308 | cmd.options.forEach((anOption) => {
|
1309 | if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
|
1310 | cmd.missingMandatoryOptionValue(anOption);
|
1311 | }
|
1312 | });
|
1313 | }
|
1314 | }
|
1315 |
|
1316 | |
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 | _checkForConflictingLocalOptions() {
|
1322 | const definedNonDefaultOptions = this.options.filter(
|
1323 | (option) => {
|
1324 | const optionKey = option.attributeName();
|
1325 | if (this.getOptionValue(optionKey) === undefined) {
|
1326 | return false;
|
1327 | }
|
1328 | return this.getOptionValueSource(optionKey) !== 'default';
|
1329 | }
|
1330 | );
|
1331 |
|
1332 | const optionsWithConflicting = definedNonDefaultOptions.filter(
|
1333 | (option) => option.conflictsWith.length > 0
|
1334 | );
|
1335 |
|
1336 | optionsWithConflicting.forEach((option) => {
|
1337 | const conflictingAndDefined = definedNonDefaultOptions.find((defined) =>
|
1338 | option.conflictsWith.includes(defined.attributeName())
|
1339 | );
|
1340 | if (conflictingAndDefined) {
|
1341 | this._conflictingOption(option, conflictingAndDefined);
|
1342 | }
|
1343 | });
|
1344 | }
|
1345 |
|
1346 | |
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 | _checkForConflictingOptions() {
|
1353 |
|
1354 | for (let cmd = this; cmd; cmd = cmd.parent) {
|
1355 | cmd._checkForConflictingLocalOptions();
|
1356 | }
|
1357 | }
|
1358 |
|
1359 | |
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 |
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 | parseOptions(argv) {
|
1376 | const operands = [];
|
1377 | const unknown = [];
|
1378 | let dest = operands;
|
1379 | const args = argv.slice();
|
1380 |
|
1381 | function maybeOption(arg) {
|
1382 | return arg.length > 1 && arg[0] === '-';
|
1383 | }
|
1384 |
|
1385 |
|
1386 | let activeVariadicOption = null;
|
1387 | while (args.length) {
|
1388 | const arg = args.shift();
|
1389 |
|
1390 |
|
1391 | if (arg === '--') {
|
1392 | if (dest === unknown) dest.push(arg);
|
1393 | dest.push(...args);
|
1394 | break;
|
1395 | }
|
1396 |
|
1397 | if (activeVariadicOption && !maybeOption(arg)) {
|
1398 | this.emit(`option:${activeVariadicOption.name()}`, arg);
|
1399 | continue;
|
1400 | }
|
1401 | activeVariadicOption = null;
|
1402 |
|
1403 | if (maybeOption(arg)) {
|
1404 | const option = this._findOption(arg);
|
1405 |
|
1406 | if (option) {
|
1407 | if (option.required) {
|
1408 | const value = args.shift();
|
1409 | if (value === undefined) this.optionMissingArgument(option);
|
1410 | this.emit(`option:${option.name()}`, value);
|
1411 | } else if (option.optional) {
|
1412 | let value = null;
|
1413 |
|
1414 | if (args.length > 0 && !maybeOption(args[0])) {
|
1415 | value = args.shift();
|
1416 | }
|
1417 | this.emit(`option:${option.name()}`, value);
|
1418 | } else {
|
1419 | this.emit(`option:${option.name()}`);
|
1420 | }
|
1421 | activeVariadicOption = option.variadic ? option : null;
|
1422 | continue;
|
1423 | }
|
1424 | }
|
1425 |
|
1426 |
|
1427 | if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {
|
1428 | const option = this._findOption(`-${arg[1]}`);
|
1429 | if (option) {
|
1430 | if (option.required || (option.optional && this._combineFlagAndOptionalValue)) {
|
1431 |
|
1432 | this.emit(`option:${option.name()}`, arg.slice(2));
|
1433 | } else {
|
1434 |
|
1435 | this.emit(`option:${option.name()}`);
|
1436 | args.unshift(`-${arg.slice(2)}`);
|
1437 | }
|
1438 | continue;
|
1439 | }
|
1440 | }
|
1441 |
|
1442 |
|
1443 | if (/^--[^=]+=/.test(arg)) {
|
1444 | const index = arg.indexOf('=');
|
1445 | const option = this._findOption(arg.slice(0, index));
|
1446 | if (option && (option.required || option.optional)) {
|
1447 | this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
1448 | continue;
|
1449 | }
|
1450 | }
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 | if (maybeOption(arg)) {
|
1457 | dest = unknown;
|
1458 | }
|
1459 |
|
1460 |
|
1461 | if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
1462 | if (this._findCommand(arg)) {
|
1463 | operands.push(arg);
|
1464 | if (args.length > 0) unknown.push(...args);
|
1465 | break;
|
1466 | } else if (arg === this._helpCommandName && this._hasImplicitHelpCommand()) {
|
1467 | operands.push(arg);
|
1468 | if (args.length > 0) operands.push(...args);
|
1469 | break;
|
1470 | } else if (this._defaultCommandName) {
|
1471 | unknown.push(arg);
|
1472 | if (args.length > 0) unknown.push(...args);
|
1473 | break;
|
1474 | }
|
1475 | }
|
1476 |
|
1477 |
|
1478 | if (this._passThroughOptions) {
|
1479 | dest.push(arg);
|
1480 | if (args.length > 0) dest.push(...args);
|
1481 | break;
|
1482 | }
|
1483 |
|
1484 |
|
1485 | dest.push(arg);
|
1486 | }
|
1487 |
|
1488 | return { operands, unknown };
|
1489 | }
|
1490 |
|
1491 | |
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 | opts() {
|
1497 | if (this._storeOptionsAsProperties) {
|
1498 |
|
1499 | const result = {};
|
1500 | const len = this.options.length;
|
1501 |
|
1502 | for (let i = 0; i < len; i++) {
|
1503 | const key = this.options[i].attributeName();
|
1504 | result[key] = key === this._versionOptionName ? this._version : this[key];
|
1505 | }
|
1506 | return result;
|
1507 | }
|
1508 |
|
1509 | return this._optionValues;
|
1510 | }
|
1511 |
|
1512 | |
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 | optsWithGlobals() {
|
1518 |
|
1519 | return getCommandAndParents(this).reduce(
|
1520 | (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
|
1521 | {}
|
1522 | );
|
1523 | }
|
1524 |
|
1525 | |
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 | error(message, errorOptions) {
|
1534 |
|
1535 | this._outputConfiguration.outputError(`${message}\n`, this._outputConfiguration.writeErr);
|
1536 | if (typeof this._showHelpAfterError === 'string') {
|
1537 | this._outputConfiguration.writeErr(`${this._showHelpAfterError}\n`);
|
1538 | } else if (this._showHelpAfterError) {
|
1539 | this._outputConfiguration.writeErr('\n');
|
1540 | this.outputHelp({ error: true });
|
1541 | }
|
1542 |
|
1543 |
|
1544 | const config = errorOptions || {};
|
1545 | const exitCode = config.exitCode || 1;
|
1546 | const code = config.code || 'commander.error';
|
1547 | this._exit(exitCode, code, message);
|
1548 | }
|
1549 |
|
1550 | |
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 | _parseOptionsEnv() {
|
1557 | this.options.forEach((option) => {
|
1558 | if (option.envVar && option.envVar in process.env) {
|
1559 | const optionKey = option.attributeName();
|
1560 |
|
1561 | if (this.getOptionValue(optionKey) === undefined || ['default', 'config', 'env'].includes(this.getOptionValueSource(optionKey))) {
|
1562 | if (option.required || option.optional) {
|
1563 |
|
1564 | this.emit(`optionEnv:${option.name()}`, process.env[option.envVar]);
|
1565 | } else {
|
1566 |
|
1567 | this.emit(`optionEnv:${option.name()}`);
|
1568 | }
|
1569 | }
|
1570 | }
|
1571 | });
|
1572 | }
|
1573 |
|
1574 | |
1575 |
|
1576 |
|
1577 |
|
1578 |
|
1579 | _parseOptionsImplied() {
|
1580 | const dualHelper = new DualOptions(this.options);
|
1581 | const hasCustomOptionValue = (optionKey) => {
|
1582 | return this.getOptionValue(optionKey) !== undefined && !['default', 'implied'].includes(this.getOptionValueSource(optionKey));
|
1583 | };
|
1584 | this.options
|
1585 | .filter(option => (option.implied !== undefined) &&
|
1586 | hasCustomOptionValue(option.attributeName()) &&
|
1587 | dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option))
|
1588 | .forEach((option) => {
|
1589 | Object.keys(option.implied)
|
1590 | .filter(impliedKey => !hasCustomOptionValue(impliedKey))
|
1591 | .forEach(impliedKey => {
|
1592 | this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], 'implied');
|
1593 | });
|
1594 | });
|
1595 | }
|
1596 |
|
1597 | |
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 | missingArgument(name) {
|
1605 | const message = `error: missing required argument '${name}'`;
|
1606 | this.error(message, { code: 'commander.missingArgument' });
|
1607 | }
|
1608 |
|
1609 | |
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 | optionMissingArgument(option) {
|
1617 | const message = `error: option '${option.flags}' argument missing`;
|
1618 | this.error(message, { code: 'commander.optionMissingArgument' });
|
1619 | }
|
1620 |
|
1621 | |
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 | missingMandatoryOptionValue(option) {
|
1629 | const message = `error: required option '${option.flags}' not specified`;
|
1630 | this.error(message, { code: 'commander.missingMandatoryOptionValue' });
|
1631 | }
|
1632 |
|
1633 | |
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 | _conflictingOption(option, conflictingOption) {
|
1641 |
|
1642 |
|
1643 | const findBestOptionFromValue = (option) => {
|
1644 | const optionKey = option.attributeName();
|
1645 | const optionValue = this.getOptionValue(optionKey);
|
1646 | const negativeOption = this.options.find(target => target.negate && optionKey === target.attributeName());
|
1647 | const positiveOption = this.options.find(target => !target.negate && optionKey === target.attributeName());
|
1648 | if (negativeOption && (
|
1649 | (negativeOption.presetArg === undefined && optionValue === false) ||
|
1650 | (negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)
|
1651 | )) {
|
1652 | return negativeOption;
|
1653 | }
|
1654 | return positiveOption || option;
|
1655 | };
|
1656 |
|
1657 | const getErrorMessage = (option) => {
|
1658 | const bestOption = findBestOptionFromValue(option);
|
1659 | const optionKey = bestOption.attributeName();
|
1660 | const source = this.getOptionValueSource(optionKey);
|
1661 | if (source === 'env') {
|
1662 | return `environment variable '${bestOption.envVar}'`;
|
1663 | }
|
1664 | return `option '${bestOption.flags}'`;
|
1665 | };
|
1666 |
|
1667 | const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
1668 | this.error(message, { code: 'commander.conflictingOption' });
|
1669 | }
|
1670 |
|
1671 | |
1672 |
|
1673 |
|
1674 |
|
1675 |
|
1676 |
|
1677 |
|
1678 | unknownOption(flag) {
|
1679 | if (this._allowUnknownOption) return;
|
1680 | let suggestion = '';
|
1681 |
|
1682 | if (flag.startsWith('--') && this._showSuggestionAfterError) {
|
1683 |
|
1684 | let candidateFlags = [];
|
1685 | let command = this;
|
1686 | do {
|
1687 | const moreFlags = command.createHelp().visibleOptions(command)
|
1688 | .filter(option => option.long)
|
1689 | .map(option => option.long);
|
1690 | candidateFlags = candidateFlags.concat(moreFlags);
|
1691 | command = command.parent;
|
1692 | } while (command && !command._enablePositionalOptions);
|
1693 | suggestion = suggestSimilar(flag, candidateFlags);
|
1694 | }
|
1695 |
|
1696 | const message = `error: unknown option '${flag}'${suggestion}`;
|
1697 | this.error(message, { code: 'commander.unknownOption' });
|
1698 | }
|
1699 |
|
1700 | |
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 | _excessArguments(receivedArgs) {
|
1708 | if (this._allowExcessArguments) return;
|
1709 |
|
1710 | const expected = this._args.length;
|
1711 | const s = (expected === 1) ? '' : 's';
|
1712 | const forSubcommand = this.parent ? ` for '${this.name()}'` : '';
|
1713 | const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
1714 | this.error(message, { code: 'commander.excessArguments' });
|
1715 | }
|
1716 |
|
1717 | |
1718 |
|
1719 |
|
1720 |
|
1721 |
|
1722 |
|
1723 | unknownCommand() {
|
1724 | const unknownName = this.args[0];
|
1725 | let suggestion = '';
|
1726 |
|
1727 | if (this._showSuggestionAfterError) {
|
1728 | const candidateNames = [];
|
1729 | this.createHelp().visibleCommands(this).forEach((command) => {
|
1730 | candidateNames.push(command.name());
|
1731 |
|
1732 | if (command.alias()) candidateNames.push(command.alias());
|
1733 | });
|
1734 | suggestion = suggestSimilar(unknownName, candidateNames);
|
1735 | }
|
1736 |
|
1737 | const message = `error: unknown command '${unknownName}'${suggestion}`;
|
1738 | this.error(message, { code: 'commander.unknownCommand' });
|
1739 | }
|
1740 |
|
1741 | |
1742 |
|
1743 |
|
1744 |
|
1745 |
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 | version(str, flags, description) {
|
1756 | if (str === undefined) return this._version;
|
1757 | this._version = str;
|
1758 | flags = flags || '-V, --version';
|
1759 | description = description || 'output the version number';
|
1760 | const versionOption = this.createOption(flags, description);
|
1761 | this._versionOptionName = versionOption.attributeName();
|
1762 | this.options.push(versionOption);
|
1763 | this.on('option:' + versionOption.name(), () => {
|
1764 | this._outputConfiguration.writeOut(`${str}\n`);
|
1765 | this._exit(0, 'commander.version', str);
|
1766 | });
|
1767 | return this;
|
1768 | }
|
1769 |
|
1770 | |
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 | description(str, argsDescription) {
|
1778 | if (str === undefined && argsDescription === undefined) return this._description;
|
1779 | this._description = str;
|
1780 | if (argsDescription) {
|
1781 | this._argsDescription = argsDescription;
|
1782 | }
|
1783 | return this;
|
1784 | }
|
1785 |
|
1786 | |
1787 |
|
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 | summary(str) {
|
1793 | if (str === undefined) return this._summary;
|
1794 | this._summary = str;
|
1795 | return this;
|
1796 | }
|
1797 |
|
1798 | |
1799 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 | alias(alias) {
|
1808 | if (alias === undefined) return this._aliases[0];
|
1809 |
|
1810 |
|
1811 | let command = this;
|
1812 | if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
1813 |
|
1814 | command = this.commands[this.commands.length - 1];
|
1815 | }
|
1816 |
|
1817 | if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
|
1818 |
|
1819 | command._aliases.push(alias);
|
1820 | return this;
|
1821 | }
|
1822 |
|
1823 | |
1824 |
|
1825 |
|
1826 |
|
1827 |
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 | aliases(aliases) {
|
1833 |
|
1834 | if (aliases === undefined) return this._aliases;
|
1835 |
|
1836 | aliases.forEach((alias) => this.alias(alias));
|
1837 | return this;
|
1838 | }
|
1839 |
|
1840 | |
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 | usage(str) {
|
1848 | if (str === undefined) {
|
1849 | if (this._usage) return this._usage;
|
1850 |
|
1851 | const args = this._args.map((arg) => {
|
1852 | return humanReadableArgName(arg);
|
1853 | });
|
1854 | return [].concat(
|
1855 | (this.options.length || this._hasHelpOption ? '[options]' : []),
|
1856 | (this.commands.length ? '[command]' : []),
|
1857 | (this._args.length ? args : [])
|
1858 | ).join(' ');
|
1859 | }
|
1860 |
|
1861 | this._usage = str;
|
1862 | return this;
|
1863 | }
|
1864 |
|
1865 | |
1866 |
|
1867 |
|
1868 |
|
1869 |
|
1870 |
|
1871 |
|
1872 | name(str) {
|
1873 | if (str === undefined) return this._name;
|
1874 | this._name = str;
|
1875 | return this;
|
1876 | }
|
1877 |
|
1878 | |
1879 |
|
1880 |
|
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 |
|
1891 | nameFromFilename(filename) {
|
1892 | this._name = path.basename(filename, path.extname(filename));
|
1893 |
|
1894 | return this;
|
1895 | }
|
1896 |
|
1897 | |
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 |
|
1907 |
|
1908 |
|
1909 | executableDir(path) {
|
1910 | if (path === undefined) return this._executableDir;
|
1911 | this._executableDir = path;
|
1912 | return this;
|
1913 | }
|
1914 |
|
1915 | |
1916 |
|
1917 |
|
1918 |
|
1919 |
|
1920 |
|
1921 |
|
1922 | helpInformation(contextOptions) {
|
1923 | const helper = this.createHelp();
|
1924 | if (helper.helpWidth === undefined) {
|
1925 | helper.helpWidth = (contextOptions && contextOptions.error) ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
1926 | }
|
1927 | return helper.formatHelp(this, helper);
|
1928 | }
|
1929 |
|
1930 | |
1931 |
|
1932 |
|
1933 |
|
1934 | _getHelpContext(contextOptions) {
|
1935 | contextOptions = contextOptions || {};
|
1936 | const context = { error: !!contextOptions.error };
|
1937 | let write;
|
1938 | if (context.error) {
|
1939 | write = (arg) => this._outputConfiguration.writeErr(arg);
|
1940 | } else {
|
1941 | write = (arg) => this._outputConfiguration.writeOut(arg);
|
1942 | }
|
1943 | context.write = contextOptions.write || write;
|
1944 | context.command = this;
|
1945 | return context;
|
1946 | }
|
1947 |
|
1948 | |
1949 |
|
1950 |
|
1951 |
|
1952 |
|
1953 |
|
1954 |
|
1955 |
|
1956 | outputHelp(contextOptions) {
|
1957 | let deprecatedCallback;
|
1958 | if (typeof contextOptions === 'function') {
|
1959 | deprecatedCallback = contextOptions;
|
1960 | contextOptions = undefined;
|
1961 | }
|
1962 | const context = this._getHelpContext(contextOptions);
|
1963 |
|
1964 | getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));
|
1965 | this.emit('beforeHelp', context);
|
1966 |
|
1967 | let helpInformation = this.helpInformation(context);
|
1968 | if (deprecatedCallback) {
|
1969 | helpInformation = deprecatedCallback(helpInformation);
|
1970 | if (typeof helpInformation !== 'string' && !Buffer.isBuffer(helpInformation)) {
|
1971 | throw new Error('outputHelp callback must return a string or a Buffer');
|
1972 | }
|
1973 | }
|
1974 | context.write(helpInformation);
|
1975 |
|
1976 | this.emit(this._helpLongFlag);
|
1977 | this.emit('afterHelp', context);
|
1978 | getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
|
1979 | }
|
1980 |
|
1981 | |
1982 |
|
1983 |
|
1984 |
|
1985 |
|
1986 |
|
1987 |
|
1988 |
|
1989 |
|
1990 |
|
1991 | helpOption(flags, description) {
|
1992 | if (typeof flags === 'boolean') {
|
1993 | this._hasHelpOption = flags;
|
1994 | return this;
|
1995 | }
|
1996 | this._helpFlags = flags || this._helpFlags;
|
1997 | this._helpDescription = description || this._helpDescription;
|
1998 |
|
1999 | const helpFlags = splitOptionFlags(this._helpFlags);
|
2000 | this._helpShortFlag = helpFlags.shortFlag;
|
2001 | this._helpLongFlag = helpFlags.longFlag;
|
2002 |
|
2003 | return this;
|
2004 | }
|
2005 |
|
2006 | |
2007 |
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 | help(contextOptions) {
|
2015 | this.outputHelp(contextOptions);
|
2016 | let exitCode = process.exitCode || 0;
|
2017 | if (exitCode === 0 && contextOptions && typeof contextOptions !== 'function' && contextOptions.error) {
|
2018 | exitCode = 1;
|
2019 | }
|
2020 |
|
2021 | this._exit(exitCode, 'commander.help', '(outputHelp)');
|
2022 | }
|
2023 |
|
2024 | |
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 | addHelpText(position, text) {
|
2035 | const allowedValues = ['beforeAll', 'before', 'after', 'afterAll'];
|
2036 | if (!allowedValues.includes(position)) {
|
2037 | throw new Error(`Unexpected value for position to addHelpText.
|
2038 | Expecting one of '${allowedValues.join("', '")}'`);
|
2039 | }
|
2040 | const helpEvent = `${position}Help`;
|
2041 | this.on(helpEvent, (context) => {
|
2042 | let helpStr;
|
2043 | if (typeof text === 'function') {
|
2044 | helpStr = text({ error: context.error, command: context.command });
|
2045 | } else {
|
2046 | helpStr = text;
|
2047 | }
|
2048 |
|
2049 | if (helpStr) {
|
2050 | context.write(`${helpStr}\n`);
|
2051 | }
|
2052 | });
|
2053 | return this;
|
2054 | }
|
2055 | }
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 |
|
2062 |
|
2063 |
|
2064 |
|
2065 | function outputHelpIfRequested(cmd, args) {
|
2066 | const helpOption = cmd._hasHelpOption && args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
|
2067 | if (helpOption) {
|
2068 | cmd.outputHelp();
|
2069 |
|
2070 | cmd._exit(0, 'commander.helpDisplayed', '(outputHelp)');
|
2071 | }
|
2072 | }
|
2073 |
|
2074 |
|
2075 |
|
2076 |
|
2077 |
|
2078 |
|
2079 |
|
2080 |
|
2081 |
|
2082 | function incrementNodeInspectorPort(args) {
|
2083 |
|
2084 |
|
2085 |
|
2086 |
|
2087 | return args.map((arg) => {
|
2088 | if (!arg.startsWith('--inspect')) {
|
2089 | return arg;
|
2090 | }
|
2091 | let debugOption;
|
2092 | let debugHost = '127.0.0.1';
|
2093 | let debugPort = '9229';
|
2094 | let match;
|
2095 | if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
2096 |
|
2097 | debugOption = match[1];
|
2098 | } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
2099 | debugOption = match[1];
|
2100 | if (/^\d+$/.test(match[3])) {
|
2101 |
|
2102 | debugPort = match[3];
|
2103 | } else {
|
2104 |
|
2105 | debugHost = match[3];
|
2106 | }
|
2107 | } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
2108 |
|
2109 | debugOption = match[1];
|
2110 | debugHost = match[3];
|
2111 | debugPort = match[4];
|
2112 | }
|
2113 |
|
2114 | if (debugOption && debugPort !== '0') {
|
2115 | return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
2116 | }
|
2117 | return arg;
|
2118 | });
|
2119 | }
|
2120 |
|
2121 |
|
2122 |
|
2123 |
|
2124 |
|
2125 |
|
2126 |
|
2127 | function getCommandAndParents(startCommand) {
|
2128 | const result = [];
|
2129 | for (let command = startCommand; command; command = command.parent) {
|
2130 | result.push(command);
|
2131 | }
|
2132 | return result;
|
2133 | }
|
2134 |
|
2135 | exports.Command = Command;
|