1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509 |
- const EventEmitter = require('node:events').EventEmitter;
- const childProcess = require('node:child_process');
- const path = require('node:path');
- const fs = require('node:fs');
- const process = require('node:process');
- const { Argument, humanReadableArgName } = require('./argument.js');
- const { CommanderError } = require('./error.js');
- const { Help } = require('./help.js');
- const { Option, DualOptions } = require('./option.js');
- const { suggestSimilar } = require('./suggestSimilar');
- class Command extends EventEmitter {
-
- constructor(name) {
- super();
-
- this.commands = [];
-
- this.options = [];
- this.parent = null;
- this._allowUnknownOption = false;
- this._allowExcessArguments = true;
-
- this.registeredArguments = [];
- this._args = this.registeredArguments;
-
- this.args = [];
- this.rawArgs = [];
- this.processedArgs = [];
- this._scriptPath = null;
- this._name = name || '';
- this._optionValues = {};
- this._optionValueSources = {};
- this._storeOptionsAsProperties = false;
- this._actionHandler = null;
- this._executableHandler = false;
- this._executableFile = null;
- this._executableDir = null;
- this._defaultCommandName = null;
- this._exitCallback = null;
- this._aliases = [];
- this._combineFlagAndOptionalValue = true;
- this._description = '';
- this._summary = '';
- this._argsDescription = undefined;
- this._enablePositionalOptions = false;
- this._passThroughOptions = false;
- this._lifeCycleHooks = {};
-
- this._showHelpAfterError = false;
- this._showSuggestionAfterError = true;
-
- this._outputConfiguration = {
- writeOut: (str) => process.stdout.write(str),
- writeErr: (str) => process.stderr.write(str),
- getOutHelpWidth: () =>
- process.stdout.isTTY ? process.stdout.columns : undefined,
- getErrHelpWidth: () =>
- process.stderr.isTTY ? process.stderr.columns : undefined,
- outputError: (str, write) => write(str),
- };
- this._hidden = false;
-
- this._helpOption = undefined;
- this._addImplicitHelpCommand = undefined;
-
- this._helpCommand = undefined;
- this._helpConfiguration = {};
- }
-
- copyInheritedSettings(sourceCommand) {
- this._outputConfiguration = sourceCommand._outputConfiguration;
- this._helpOption = sourceCommand._helpOption;
- this._helpCommand = sourceCommand._helpCommand;
- this._helpConfiguration = sourceCommand._helpConfiguration;
- this._exitCallback = sourceCommand._exitCallback;
- this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
- this._combineFlagAndOptionalValue =
- sourceCommand._combineFlagAndOptionalValue;
- this._allowExcessArguments = sourceCommand._allowExcessArguments;
- this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
- this._showHelpAfterError = sourceCommand._showHelpAfterError;
- this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
- return this;
- }
-
- _getCommandAndAncestors() {
- const result = [];
-
- for (let command = this; command; command = command.parent) {
- result.push(command);
- }
- return result;
- }
-
- command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
- let desc = actionOptsOrExecDesc;
- let opts = execOpts;
- if (typeof desc === 'object' && desc !== null) {
- opts = desc;
- desc = null;
- }
- opts = opts || {};
- const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
- const cmd = this.createCommand(name);
- if (desc) {
- cmd.description(desc);
- cmd._executableHandler = true;
- }
- if (opts.isDefault) this._defaultCommandName = cmd._name;
- cmd._hidden = !!(opts.noHelp || opts.hidden);
- cmd._executableFile = opts.executableFile || null;
- if (args) cmd.arguments(args);
- this._registerCommand(cmd);
- cmd.parent = this;
- cmd.copyInheritedSettings(this);
- if (desc) return this;
- return cmd;
- }
-
- createCommand(name) {
- return new Command(name);
- }
-
- createHelp() {
- return Object.assign(new Help(), this.configureHelp());
- }
-
- configureHelp(configuration) {
- if (configuration === undefined) return this._helpConfiguration;
- this._helpConfiguration = configuration;
- return this;
- }
-
- configureOutput(configuration) {
- if (configuration === undefined) return this._outputConfiguration;
- Object.assign(this._outputConfiguration, configuration);
- return this;
- }
-
- showHelpAfterError(displayHelp = true) {
- if (typeof displayHelp !== 'string') displayHelp = !!displayHelp;
- this._showHelpAfterError = displayHelp;
- return this;
- }
-
- showSuggestionAfterError(displaySuggestion = true) {
- this._showSuggestionAfterError = !!displaySuggestion;
- return this;
- }
-
- addCommand(cmd, opts) {
- if (!cmd._name) {
- throw new Error(`Command passed to .addCommand() must have a name
- - specify the name in Command constructor or using .name()`);
- }
- opts = opts || {};
- if (opts.isDefault) this._defaultCommandName = cmd._name;
- if (opts.noHelp || opts.hidden) cmd._hidden = true;
- this._registerCommand(cmd);
- cmd.parent = this;
- cmd._checkForBrokenPassThrough();
- return this;
- }
-
- createArgument(name, description) {
- return new Argument(name, description);
- }
-
- argument(name, description, fn, defaultValue) {
- const argument = this.createArgument(name, description);
- if (typeof fn === 'function') {
- argument.default(defaultValue).argParser(fn);
- } else {
- argument.default(fn);
- }
- this.addArgument(argument);
- return this;
- }
-
- arguments(names) {
- names
- .trim()
- .split(/ +/)
- .forEach((detail) => {
- this.argument(detail);
- });
- return this;
- }
-
- addArgument(argument) {
- const previousArgument = this.registeredArguments.slice(-1)[0];
- if (previousArgument && previousArgument.variadic) {
- throw new Error(
- `only the last argument can be variadic '${previousArgument.name()}'`,
- );
- }
- if (
- argument.required &&
- argument.defaultValue !== undefined &&
- argument.parseArg === undefined
- ) {
- throw new Error(
- `a default value for a required argument is never used: '${argument.name()}'`,
- );
- }
- this.registeredArguments.push(argument);
- return this;
- }
-
- helpCommand(enableOrNameAndArgs, description) {
- if (typeof enableOrNameAndArgs === 'boolean') {
- this._addImplicitHelpCommand = enableOrNameAndArgs;
- return this;
- }
- enableOrNameAndArgs = enableOrNameAndArgs ?? 'help [command]';
- const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
- const helpDescription = description ?? 'display help for command';
- const helpCommand = this.createCommand(helpName);
- helpCommand.helpOption(false);
- if (helpArgs) helpCommand.arguments(helpArgs);
- if (helpDescription) helpCommand.description(helpDescription);
- this._addImplicitHelpCommand = true;
- this._helpCommand = helpCommand;
- return this;
- }
-
- addHelpCommand(helpCommand, deprecatedDescription) {
-
-
- if (typeof helpCommand !== 'object') {
- this.helpCommand(helpCommand, deprecatedDescription);
- return this;
- }
- this._addImplicitHelpCommand = true;
- this._helpCommand = helpCommand;
- return this;
- }
-
- _getHelpCommand() {
- const hasImplicitHelpCommand =
- this._addImplicitHelpCommand ??
- (this.commands.length &&
- !this._actionHandler &&
- !this._findCommand('help'));
- if (hasImplicitHelpCommand) {
- if (this._helpCommand === undefined) {
- this.helpCommand(undefined, undefined);
- }
- return this._helpCommand;
- }
- return null;
- }
-
- hook(event, listener) {
- const allowedValues = ['preSubcommand', 'preAction', 'postAction'];
- if (!allowedValues.includes(event)) {
- throw new Error(`Unexpected value for event passed to hook : '${event}'.
- Expecting one of '${allowedValues.join("', '")}'`);
- }
- if (this._lifeCycleHooks[event]) {
- this._lifeCycleHooks[event].push(listener);
- } else {
- this._lifeCycleHooks[event] = [listener];
- }
- return this;
- }
-
- exitOverride(fn) {
- if (fn) {
- this._exitCallback = fn;
- } else {
- this._exitCallback = (err) => {
- if (err.code !== 'commander.executeSubCommandAsync') {
- throw err;
- } else {
-
- }
- };
- }
- return this;
- }
-
- _exit(exitCode, code, message) {
- if (this._exitCallback) {
- this._exitCallback(new CommanderError(exitCode, code, message));
-
- }
- process.exit(exitCode);
- }
-
- action(fn) {
- const listener = (args) => {
-
- const expectedArgsCount = this.registeredArguments.length;
- const actionArgs = args.slice(0, expectedArgsCount);
- if (this._storeOptionsAsProperties) {
- actionArgs[expectedArgsCount] = this;
- } else {
- actionArgs[expectedArgsCount] = this.opts();
- }
- actionArgs.push(this);
- return fn.apply(this, actionArgs);
- };
- this._actionHandler = listener;
- return this;
- }
-
- createOption(flags, description) {
- return new Option(flags, description);
- }
-
- _callParseArg(target, value, previous, invalidArgumentMessage) {
- try {
- return target.parseArg(value, previous);
- } catch (err) {
- if (err.code === 'commander.invalidArgument') {
- const message = `${invalidArgumentMessage} ${err.message}`;
- this.error(message, { exitCode: err.exitCode, code: err.code });
- }
- throw err;
- }
- }
-
- _registerOption(option) {
- const matchingOption =
- (option.short && this._findOption(option.short)) ||
- (option.long && this._findOption(option.long));
- if (matchingOption) {
- const matchingFlag =
- option.long && this._findOption(option.long)
- ? option.long
- : option.short;
- throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
- - already used by option '${matchingOption.flags}'`);
- }
- this.options.push(option);
- }
-
- _registerCommand(command) {
- const knownBy = (cmd) => {
- return [cmd.name()].concat(cmd.aliases());
- };
- const alreadyUsed = knownBy(command).find((name) =>
- this._findCommand(name),
- );
- if (alreadyUsed) {
- const existingCmd = knownBy(this._findCommand(alreadyUsed)).join('|');
- const newCmd = knownBy(command).join('|');
- throw new Error(
- `cannot add command '${newCmd}' as already have command '${existingCmd}'`,
- );
- }
- this.commands.push(command);
- }
-
- addOption(option) {
- this._registerOption(option);
- const oname = option.name();
- const name = option.attributeName();
-
- if (option.negate) {
-
- const positiveLongFlag = option.long.replace(/^--no-/, '--');
- if (!this._findOption(positiveLongFlag)) {
- this.setOptionValueWithSource(
- name,
- option.defaultValue === undefined ? true : option.defaultValue,
- 'default',
- );
- }
- } else if (option.defaultValue !== undefined) {
- this.setOptionValueWithSource(name, option.defaultValue, 'default');
- }
-
- const handleOptionValue = (val, invalidValueMessage, valueSource) => {
-
-
- if (val == null && option.presetArg !== undefined) {
- val = option.presetArg;
- }
-
- const oldValue = this.getOptionValue(name);
- if (val !== null && option.parseArg) {
- val = this._callParseArg(option, val, oldValue, invalidValueMessage);
- } else if (val !== null && option.variadic) {
- val = option._concatValue(val, oldValue);
- }
-
- if (val == null) {
- if (option.negate) {
- val = false;
- } else if (option.isBoolean() || option.optional) {
- val = true;
- } else {
- val = '';
- }
- }
- this.setOptionValueWithSource(name, val, valueSource);
- };
- this.on('option:' + oname, (val) => {
- const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
- handleOptionValue(val, invalidValueMessage, 'cli');
- });
- if (option.envVar) {
- this.on('optionEnv:' + oname, (val) => {
- const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
- handleOptionValue(val, invalidValueMessage, 'env');
- });
- }
- return this;
- }
-
- _optionEx(config, flags, description, fn, defaultValue) {
- if (typeof flags === 'object' && flags instanceof Option) {
- throw new Error(
- 'To add an Option object use addOption() instead of option() or requiredOption()',
- );
- }
- const option = this.createOption(flags, description);
- option.makeOptionMandatory(!!config.mandatory);
- if (typeof fn === 'function') {
- option.default(defaultValue).argParser(fn);
- } else if (fn instanceof RegExp) {
-
- const regex = fn;
- fn = (val, def) => {
- const m = regex.exec(val);
- return m ? m[0] : def;
- };
- option.default(defaultValue).argParser(fn);
- } else {
- option.default(fn);
- }
- return this.addOption(option);
- }
-
- option(flags, description, parseArg, defaultValue) {
- return this._optionEx({}, flags, description, parseArg, defaultValue);
- }
-
- requiredOption(flags, description, parseArg, defaultValue) {
- return this._optionEx(
- { mandatory: true },
- flags,
- description,
- parseArg,
- defaultValue,
- );
- }
-
- combineFlagAndOptionalValue(combine = true) {
- this._combineFlagAndOptionalValue = !!combine;
- return this;
- }
-
- allowUnknownOption(allowUnknown = true) {
- this._allowUnknownOption = !!allowUnknown;
- return this;
- }
-
- allowExcessArguments(allowExcess = true) {
- this._allowExcessArguments = !!allowExcess;
- return this;
- }
-
- enablePositionalOptions(positional = true) {
- this._enablePositionalOptions = !!positional;
- return this;
- }
-
- passThroughOptions(passThrough = true) {
- this._passThroughOptions = !!passThrough;
- this._checkForBrokenPassThrough();
- return this;
- }
-
- _checkForBrokenPassThrough() {
- if (
- this.parent &&
- this._passThroughOptions &&
- !this.parent._enablePositionalOptions
- ) {
- throw new Error(
- `passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`,
- );
- }
- }
-
- storeOptionsAsProperties(storeAsProperties = true) {
- if (this.options.length) {
- throw new Error('call .storeOptionsAsProperties() before adding options');
- }
- if (Object.keys(this._optionValues).length) {
- throw new Error(
- 'call .storeOptionsAsProperties() before setting option values',
- );
- }
- this._storeOptionsAsProperties = !!storeAsProperties;
- return this;
- }
-
- getOptionValue(key) {
- if (this._storeOptionsAsProperties) {
- return this[key];
- }
- return this._optionValues[key];
- }
-
- setOptionValue(key, value) {
- return this.setOptionValueWithSource(key, value, undefined);
- }
-
- setOptionValueWithSource(key, value, source) {
- if (this._storeOptionsAsProperties) {
- this[key] = value;
- } else {
- this._optionValues[key] = value;
- }
- this._optionValueSources[key] = source;
- return this;
- }
-
- getOptionValueSource(key) {
- return this._optionValueSources[key];
- }
-
- getOptionValueSourceWithGlobals(key) {
-
- let source;
- this._getCommandAndAncestors().forEach((cmd) => {
- if (cmd.getOptionValueSource(key) !== undefined) {
- source = cmd.getOptionValueSource(key);
- }
- });
- return source;
- }
-
- _prepareUserArgs(argv, parseOptions) {
- if (argv !== undefined && !Array.isArray(argv)) {
- throw new Error('first parameter to parse must be array or undefined');
- }
- parseOptions = parseOptions || {};
-
- if (argv === undefined && parseOptions.from === undefined) {
- if (process.versions?.electron) {
- parseOptions.from = 'electron';
- }
-
- const execArgv = process.execArgv ?? [];
- if (
- execArgv.includes('-e') ||
- execArgv.includes('--eval') ||
- execArgv.includes('-p') ||
- execArgv.includes('--print')
- ) {
- parseOptions.from = 'eval';
- }
- }
-
- if (argv === undefined) {
- argv = process.argv;
- }
- this.rawArgs = argv.slice();
-
- let userArgs;
- switch (parseOptions.from) {
- case undefined:
- case 'node':
- this._scriptPath = argv[1];
- userArgs = argv.slice(2);
- break;
- case 'electron':
-
- if (process.defaultApp) {
- this._scriptPath = argv[1];
- userArgs = argv.slice(2);
- } else {
- userArgs = argv.slice(1);
- }
- break;
- case 'user':
- userArgs = argv.slice(0);
- break;
- case 'eval':
- userArgs = argv.slice(1);
- break;
- default:
- throw new Error(
- `unexpected parse option { from: '${parseOptions.from}' }`,
- );
- }
-
- if (!this._name && this._scriptPath)
- this.nameFromFilename(this._scriptPath);
- this._name = this._name || 'program';
- return userArgs;
- }
-
- parse(argv, parseOptions) {
- const userArgs = this._prepareUserArgs(argv, parseOptions);
- this._parseCommand([], userArgs);
- return this;
- }
-
- async parseAsync(argv, parseOptions) {
- const userArgs = this._prepareUserArgs(argv, parseOptions);
- await this._parseCommand([], userArgs);
- return this;
- }
-
- _executeSubCommand(subcommand, args) {
- args = args.slice();
- let launchWithNode = false;
- const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];
- function findFile(baseDir, baseName) {
-
- const localBin = path.resolve(baseDir, baseName);
- if (fs.existsSync(localBin)) return localBin;
-
- if (sourceExt.includes(path.extname(baseName))) return undefined;
-
- const foundExt = sourceExt.find((ext) =>
- fs.existsSync(`${localBin}${ext}`),
- );
- if (foundExt) return `${localBin}${foundExt}`;
- return undefined;
- }
-
- this._checkForMissingMandatoryOptions();
- this._checkForConflictingOptions();
-
- let executableFile =
- subcommand._executableFile || `${this._name}-${subcommand._name}`;
- let executableDir = this._executableDir || '';
- if (this._scriptPath) {
- let resolvedScriptPath;
- try {
- resolvedScriptPath = fs.realpathSync(this._scriptPath);
- } catch (err) {
- resolvedScriptPath = this._scriptPath;
- }
- executableDir = path.resolve(
- path.dirname(resolvedScriptPath),
- executableDir,
- );
- }
-
- if (executableDir) {
- let localFile = findFile(executableDir, executableFile);
-
- if (!localFile && !subcommand._executableFile && this._scriptPath) {
- const legacyName = path.basename(
- this._scriptPath,
- path.extname(this._scriptPath),
- );
- if (legacyName !== this._name) {
- localFile = findFile(
- executableDir,
- `${legacyName}-${subcommand._name}`,
- );
- }
- }
- executableFile = localFile || executableFile;
- }
- launchWithNode = sourceExt.includes(path.extname(executableFile));
- let proc;
- if (process.platform !== 'win32') {
- if (launchWithNode) {
- args.unshift(executableFile);
-
- args = incrementNodeInspectorPort(process.execArgv).concat(args);
- proc = childProcess.spawn(process.argv[0], args, { stdio: 'inherit' });
- } else {
- proc = childProcess.spawn(executableFile, args, { stdio: 'inherit' });
- }
- } else {
- args.unshift(executableFile);
-
- args = incrementNodeInspectorPort(process.execArgv).concat(args);
- proc = childProcess.spawn(process.execPath, args, { stdio: 'inherit' });
- }
- if (!proc.killed) {
-
- const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
- signals.forEach((signal) => {
- process.on(signal, () => {
- if (proc.killed === false && proc.exitCode === null) {
-
- proc.kill(signal);
- }
- });
- });
- }
-
- const exitCallback = this._exitCallback;
- proc.on('close', (code) => {
- code = code ?? 1;
- if (!exitCallback) {
- process.exit(code);
- } else {
- exitCallback(
- new CommanderError(
- code,
- 'commander.executeSubCommandAsync',
- '(close)',
- ),
- );
- }
- });
- proc.on('error', (err) => {
-
- if (err.code === 'ENOENT') {
- const executableDirMessage = executableDir
- ? `searched for local subcommand relative to directory '${executableDir}'`
- : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory';
- const executableMissing = `'${executableFile}' does not exist
- - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
- - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
- - ${executableDirMessage}`;
- throw new Error(executableMissing);
-
- } else if (err.code === 'EACCES') {
- throw new Error(`'${executableFile}' not executable`);
- }
- if (!exitCallback) {
- process.exit(1);
- } else {
- const wrappedError = new CommanderError(
- 1,
- 'commander.executeSubCommandAsync',
- '(error)',
- );
- wrappedError.nestedError = err;
- exitCallback(wrappedError);
- }
- });
-
- this.runningCommand = proc;
- }
-
- _dispatchSubcommand(commandName, operands, unknown) {
- const subCommand = this._findCommand(commandName);
- if (!subCommand) this.help({ error: true });
- let promiseChain;
- promiseChain = this._chainOrCallSubCommandHook(
- promiseChain,
- subCommand,
- 'preSubcommand',
- );
- promiseChain = this._chainOrCall(promiseChain, () => {
- if (subCommand._executableHandler) {
- this._executeSubCommand(subCommand, operands.concat(unknown));
- } else {
- return subCommand._parseCommand(operands, unknown);
- }
- });
- return promiseChain;
- }
-
- _dispatchHelpCommand(subcommandName) {
- if (!subcommandName) {
- this.help();
- }
- const subCommand = this._findCommand(subcommandName);
- if (subCommand && !subCommand._executableHandler) {
- subCommand.help();
- }
-
- return this._dispatchSubcommand(
- subcommandName,
- [],
- [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? '--help'],
- );
- }
-
- _checkNumberOfArguments() {
-
- this.registeredArguments.forEach((arg, i) => {
- if (arg.required && this.args[i] == null) {
- this.missingArgument(arg.name());
- }
- });
-
- if (
- this.registeredArguments.length > 0 &&
- this.registeredArguments[this.registeredArguments.length - 1].variadic
- ) {
- return;
- }
- if (this.args.length > this.registeredArguments.length) {
- this._excessArguments(this.args);
- }
- }
-
- _processArguments() {
- const myParseArg = (argument, value, previous) => {
-
- let parsedValue = value;
- if (value !== null && argument.parseArg) {
- const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
- parsedValue = this._callParseArg(
- argument,
- value,
- previous,
- invalidValueMessage,
- );
- }
- return parsedValue;
- };
- this._checkNumberOfArguments();
- const processedArgs = [];
- this.registeredArguments.forEach((declaredArg, index) => {
- let value = declaredArg.defaultValue;
- if (declaredArg.variadic) {
-
- if (index < this.args.length) {
- value = this.args.slice(index);
- if (declaredArg.parseArg) {
- value = value.reduce((processed, v) => {
- return myParseArg(declaredArg, v, processed);
- }, declaredArg.defaultValue);
- }
- } else if (value === undefined) {
- value = [];
- }
- } else if (index < this.args.length) {
- value = this.args[index];
- if (declaredArg.parseArg) {
- value = myParseArg(declaredArg, value, declaredArg.defaultValue);
- }
- }
- processedArgs[index] = value;
- });
- this.processedArgs = processedArgs;
- }
-
- _chainOrCall(promise, fn) {
-
- if (promise && promise.then && typeof promise.then === 'function') {
-
- return promise.then(() => fn());
- }
-
- return fn();
- }
-
- _chainOrCallHooks(promise, event) {
- let result = promise;
- const hooks = [];
- this._getCommandAndAncestors()
- .reverse()
- .filter((cmd) => cmd._lifeCycleHooks[event] !== undefined)
- .forEach((hookedCommand) => {
- hookedCommand._lifeCycleHooks[event].forEach((callback) => {
- hooks.push({ hookedCommand, callback });
- });
- });
- if (event === 'postAction') {
- hooks.reverse();
- }
- hooks.forEach((hookDetail) => {
- result = this._chainOrCall(result, () => {
- return hookDetail.callback(hookDetail.hookedCommand, this);
- });
- });
- return result;
- }
-
- _chainOrCallSubCommandHook(promise, subCommand, event) {
- let result = promise;
- if (this._lifeCycleHooks[event] !== undefined) {
- this._lifeCycleHooks[event].forEach((hook) => {
- result = this._chainOrCall(result, () => {
- return hook(this, subCommand);
- });
- });
- }
- return result;
- }
-
- _parseCommand(operands, unknown) {
- const parsed = this.parseOptions(unknown);
- this._parseOptionsEnv();
- this._parseOptionsImplied();
- operands = operands.concat(parsed.operands);
- unknown = parsed.unknown;
- this.args = operands.concat(unknown);
- if (operands && this._findCommand(operands[0])) {
- return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
- }
- if (
- this._getHelpCommand() &&
- operands[0] === this._getHelpCommand().name()
- ) {
- return this._dispatchHelpCommand(operands[1]);
- }
- if (this._defaultCommandName) {
- this._outputHelpIfRequested(unknown);
- return this._dispatchSubcommand(
- this._defaultCommandName,
- operands,
- unknown,
- );
- }
- if (
- this.commands.length &&
- this.args.length === 0 &&
- !this._actionHandler &&
- !this._defaultCommandName
- ) {
-
- this.help({ error: true });
- }
- this._outputHelpIfRequested(parsed.unknown);
- this._checkForMissingMandatoryOptions();
- this._checkForConflictingOptions();
-
- const checkForUnknownOptions = () => {
- if (parsed.unknown.length > 0) {
- this.unknownOption(parsed.unknown[0]);
- }
- };
- const commandEvent = `command:${this.name()}`;
- if (this._actionHandler) {
- checkForUnknownOptions();
- this._processArguments();
- let promiseChain;
- promiseChain = this._chainOrCallHooks(promiseChain, 'preAction');
- promiseChain = this._chainOrCall(promiseChain, () =>
- this._actionHandler(this.processedArgs),
- );
- if (this.parent) {
- promiseChain = this._chainOrCall(promiseChain, () => {
- this.parent.emit(commandEvent, operands, unknown);
- });
- }
- promiseChain = this._chainOrCallHooks(promiseChain, 'postAction');
- return promiseChain;
- }
- if (this.parent && this.parent.listenerCount(commandEvent)) {
- checkForUnknownOptions();
- this._processArguments();
- this.parent.emit(commandEvent, operands, unknown);
- } else if (operands.length) {
- if (this._findCommand('*')) {
-
- return this._dispatchSubcommand('*', operands, unknown);
- }
- if (this.listenerCount('command:*')) {
-
- this.emit('command:*', operands, unknown);
- } else if (this.commands.length) {
- this.unknownCommand();
- } else {
- checkForUnknownOptions();
- this._processArguments();
- }
- } else if (this.commands.length) {
- checkForUnknownOptions();
-
- this.help({ error: true });
- } else {
- checkForUnknownOptions();
- this._processArguments();
-
- }
- }
-
- _findCommand(name) {
- if (!name) return undefined;
- return this.commands.find(
- (cmd) => cmd._name === name || cmd._aliases.includes(name),
- );
- }
-
- _findOption(arg) {
- return this.options.find((option) => option.is(arg));
- }
-
- _checkForMissingMandatoryOptions() {
-
- this._getCommandAndAncestors().forEach((cmd) => {
- cmd.options.forEach((anOption) => {
- if (
- anOption.mandatory &&
- cmd.getOptionValue(anOption.attributeName()) === undefined
- ) {
- cmd.missingMandatoryOptionValue(anOption);
- }
- });
- });
- }
-
- _checkForConflictingLocalOptions() {
- const definedNonDefaultOptions = this.options.filter((option) => {
- const optionKey = option.attributeName();
- if (this.getOptionValue(optionKey) === undefined) {
- return false;
- }
- return this.getOptionValueSource(optionKey) !== 'default';
- });
- const optionsWithConflicting = definedNonDefaultOptions.filter(
- (option) => option.conflictsWith.length > 0,
- );
- optionsWithConflicting.forEach((option) => {
- const conflictingAndDefined = definedNonDefaultOptions.find((defined) =>
- option.conflictsWith.includes(defined.attributeName()),
- );
- if (conflictingAndDefined) {
- this._conflictingOption(option, conflictingAndDefined);
- }
- });
- }
-
- _checkForConflictingOptions() {
-
- this._getCommandAndAncestors().forEach((cmd) => {
- cmd._checkForConflictingLocalOptions();
- });
- }
-
- parseOptions(argv) {
- const operands = [];
- const unknown = [];
- let dest = operands;
- const args = argv.slice();
- function maybeOption(arg) {
- return arg.length > 1 && arg[0] === '-';
- }
-
- let activeVariadicOption = null;
- while (args.length) {
- const arg = args.shift();
-
- if (arg === '--') {
- if (dest === unknown) dest.push(arg);
- dest.push(...args);
- break;
- }
- if (activeVariadicOption && !maybeOption(arg)) {
- this.emit(`option:${activeVariadicOption.name()}`, arg);
- continue;
- }
- activeVariadicOption = null;
- if (maybeOption(arg)) {
- const option = this._findOption(arg);
-
- if (option) {
- if (option.required) {
- const value = args.shift();
- if (value === undefined) this.optionMissingArgument(option);
- this.emit(`option:${option.name()}`, value);
- } else if (option.optional) {
- let value = null;
-
- if (args.length > 0 && !maybeOption(args[0])) {
- value = args.shift();
- }
- this.emit(`option:${option.name()}`, value);
- } else {
-
- this.emit(`option:${option.name()}`);
- }
- activeVariadicOption = option.variadic ? option : null;
- continue;
- }
- }
-
- if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {
- const option = this._findOption(`-${arg[1]}`);
- if (option) {
- if (
- option.required ||
- (option.optional && this._combineFlagAndOptionalValue)
- ) {
-
- this.emit(`option:${option.name()}`, arg.slice(2));
- } else {
-
- this.emit(`option:${option.name()}`);
- args.unshift(`-${arg.slice(2)}`);
- }
- continue;
- }
- }
-
- if (/^--[^=]+=/.test(arg)) {
- const index = arg.indexOf('=');
- const option = this._findOption(arg.slice(0, index));
- if (option && (option.required || option.optional)) {
- this.emit(`option:${option.name()}`, arg.slice(index + 1));
- continue;
- }
- }
-
-
-
- if (maybeOption(arg)) {
- dest = unknown;
- }
-
- if (
- (this._enablePositionalOptions || this._passThroughOptions) &&
- operands.length === 0 &&
- unknown.length === 0
- ) {
- if (this._findCommand(arg)) {
- operands.push(arg);
- if (args.length > 0) unknown.push(...args);
- break;
- } else if (
- this._getHelpCommand() &&
- arg === this._getHelpCommand().name()
- ) {
- operands.push(arg);
- if (args.length > 0) operands.push(...args);
- break;
- } else if (this._defaultCommandName) {
- unknown.push(arg);
- if (args.length > 0) unknown.push(...args);
- break;
- }
- }
-
- if (this._passThroughOptions) {
- dest.push(arg);
- if (args.length > 0) dest.push(...args);
- break;
- }
-
- dest.push(arg);
- }
- return { operands, unknown };
- }
-
- opts() {
- if (this._storeOptionsAsProperties) {
-
- const result = {};
- const len = this.options.length;
- for (let i = 0; i < len; i++) {
- const key = this.options[i].attributeName();
- result[key] =
- key === this._versionOptionName ? this._version : this[key];
- }
- return result;
- }
- return this._optionValues;
- }
-
- optsWithGlobals() {
-
- return this._getCommandAndAncestors().reduce(
- (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
- {},
- );
- }
-
- error(message, errorOptions) {
-
- this._outputConfiguration.outputError(
- `${message}\n`,
- this._outputConfiguration.writeErr,
- );
- if (typeof this._showHelpAfterError === 'string') {
- this._outputConfiguration.writeErr(`${this._showHelpAfterError}\n`);
- } else if (this._showHelpAfterError) {
- this._outputConfiguration.writeErr('\n');
- this.outputHelp({ error: true });
- }
-
- const config = errorOptions || {};
- const exitCode = config.exitCode || 1;
- const code = config.code || 'commander.error';
- this._exit(exitCode, code, message);
- }
-
- _parseOptionsEnv() {
- this.options.forEach((option) => {
- if (option.envVar && option.envVar in process.env) {
- const optionKey = option.attributeName();
-
- if (
- this.getOptionValue(optionKey) === undefined ||
- ['default', 'config', 'env'].includes(
- this.getOptionValueSource(optionKey),
- )
- ) {
- if (option.required || option.optional) {
-
-
- this.emit(`optionEnv:${option.name()}`, process.env[option.envVar]);
- } else {
-
-
- this.emit(`optionEnv:${option.name()}`);
- }
- }
- }
- });
- }
-
- _parseOptionsImplied() {
- const dualHelper = new DualOptions(this.options);
- const hasCustomOptionValue = (optionKey) => {
- return (
- this.getOptionValue(optionKey) !== undefined &&
- !['default', 'implied'].includes(this.getOptionValueSource(optionKey))
- );
- };
- this.options
- .filter(
- (option) =>
- option.implied !== undefined &&
- hasCustomOptionValue(option.attributeName()) &&
- dualHelper.valueFromOption(
- this.getOptionValue(option.attributeName()),
- option,
- ),
- )
- .forEach((option) => {
- Object.keys(option.implied)
- .filter((impliedKey) => !hasCustomOptionValue(impliedKey))
- .forEach((impliedKey) => {
- this.setOptionValueWithSource(
- impliedKey,
- option.implied[impliedKey],
- 'implied',
- );
- });
- });
- }
-
- missingArgument(name) {
- const message = `error: missing required argument '${name}'`;
- this.error(message, { code: 'commander.missingArgument' });
- }
-
- optionMissingArgument(option) {
- const message = `error: option '${option.flags}' argument missing`;
- this.error(message, { code: 'commander.optionMissingArgument' });
- }
-
- missingMandatoryOptionValue(option) {
- const message = `error: required option '${option.flags}' not specified`;
- this.error(message, { code: 'commander.missingMandatoryOptionValue' });
- }
-
- _conflictingOption(option, conflictingOption) {
-
-
- const findBestOptionFromValue = (option) => {
- const optionKey = option.attributeName();
- const optionValue = this.getOptionValue(optionKey);
- const negativeOption = this.options.find(
- (target) => target.negate && optionKey === target.attributeName(),
- );
- const positiveOption = this.options.find(
- (target) => !target.negate && optionKey === target.attributeName(),
- );
- if (
- negativeOption &&
- ((negativeOption.presetArg === undefined && optionValue === false) ||
- (negativeOption.presetArg !== undefined &&
- optionValue === negativeOption.presetArg))
- ) {
- return negativeOption;
- }
- return positiveOption || option;
- };
- const getErrorMessage = (option) => {
- const bestOption = findBestOptionFromValue(option);
- const optionKey = bestOption.attributeName();
- const source = this.getOptionValueSource(optionKey);
- if (source === 'env') {
- return `environment variable '${bestOption.envVar}'`;
- }
- return `option '${bestOption.flags}'`;
- };
- const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
- this.error(message, { code: 'commander.conflictingOption' });
- }
-
- unknownOption(flag) {
- if (this._allowUnknownOption) return;
- let suggestion = '';
- if (flag.startsWith('--') && this._showSuggestionAfterError) {
-
- let candidateFlags = [];
-
- let command = this;
- do {
- const moreFlags = command
- .createHelp()
- .visibleOptions(command)
- .filter((option) => option.long)
- .map((option) => option.long);
- candidateFlags = candidateFlags.concat(moreFlags);
- command = command.parent;
- } while (command && !command._enablePositionalOptions);
- suggestion = suggestSimilar(flag, candidateFlags);
- }
- const message = `error: unknown option '${flag}'${suggestion}`;
- this.error(message, { code: 'commander.unknownOption' });
- }
-
- _excessArguments(receivedArgs) {
- if (this._allowExcessArguments) return;
- const expected = this.registeredArguments.length;
- const s = expected === 1 ? '' : 's';
- const forSubcommand = this.parent ? ` for '${this.name()}'` : '';
- const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
- this.error(message, { code: 'commander.excessArguments' });
- }
-
- unknownCommand() {
- const unknownName = this.args[0];
- let suggestion = '';
- if (this._showSuggestionAfterError) {
- const candidateNames = [];
- this.createHelp()
- .visibleCommands(this)
- .forEach((command) => {
- candidateNames.push(command.name());
-
- if (command.alias()) candidateNames.push(command.alias());
- });
- suggestion = suggestSimilar(unknownName, candidateNames);
- }
- const message = `error: unknown command '${unknownName}'${suggestion}`;
- this.error(message, { code: 'commander.unknownCommand' });
- }
-
- version(str, flags, description) {
- if (str === undefined) return this._version;
- this._version = str;
- flags = flags || '-V, --version';
- description = description || 'output the version number';
- const versionOption = this.createOption(flags, description);
- this._versionOptionName = versionOption.attributeName();
- this._registerOption(versionOption);
- this.on('option:' + versionOption.name(), () => {
- this._outputConfiguration.writeOut(`${str}\n`);
- this._exit(0, 'commander.version', str);
- });
- return this;
- }
-
- description(str, argsDescription) {
- if (str === undefined && argsDescription === undefined)
- return this._description;
- this._description = str;
- if (argsDescription) {
- this._argsDescription = argsDescription;
- }
- return this;
- }
-
- summary(str) {
- if (str === undefined) return this._summary;
- this._summary = str;
- return this;
- }
-
- alias(alias) {
- if (alias === undefined) return this._aliases[0];
-
-
- let command = this;
- if (
- this.commands.length !== 0 &&
- this.commands[this.commands.length - 1]._executableHandler
- ) {
-
- command = this.commands[this.commands.length - 1];
- }
- if (alias === command._name)
- throw new Error("Command alias can't be the same as its name");
- const matchingCommand = this.parent?._findCommand(alias);
- if (matchingCommand) {
-
- const existingCmd = [matchingCommand.name()]
- .concat(matchingCommand.aliases())
- .join('|');
- throw new Error(
- `cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`,
- );
- }
- command._aliases.push(alias);
- return this;
- }
-
- aliases(aliases) {
-
- if (aliases === undefined) return this._aliases;
- aliases.forEach((alias) => this.alias(alias));
- return this;
- }
-
- usage(str) {
- if (str === undefined) {
- if (this._usage) return this._usage;
- const args = this.registeredArguments.map((arg) => {
- return humanReadableArgName(arg);
- });
- return []
- .concat(
- this.options.length || this._helpOption !== null ? '[options]' : [],
- this.commands.length ? '[command]' : [],
- this.registeredArguments.length ? args : [],
- )
- .join(' ');
- }
- this._usage = str;
- return this;
- }
-
- name(str) {
- if (str === undefined) return this._name;
- this._name = str;
- return this;
- }
-
- nameFromFilename(filename) {
- this._name = path.basename(filename, path.extname(filename));
- return this;
- }
-
- executableDir(path) {
- if (path === undefined) return this._executableDir;
- this._executableDir = path;
- return this;
- }
-
- helpInformation(contextOptions) {
- const helper = this.createHelp();
- if (helper.helpWidth === undefined) {
- helper.helpWidth =
- contextOptions && contextOptions.error
- ? this._outputConfiguration.getErrHelpWidth()
- : this._outputConfiguration.getOutHelpWidth();
- }
- return helper.formatHelp(this, helper);
- }
-
- _getHelpContext(contextOptions) {
- contextOptions = contextOptions || {};
- const context = { error: !!contextOptions.error };
- let write;
- if (context.error) {
- write = (arg) => this._outputConfiguration.writeErr(arg);
- } else {
- write = (arg) => this._outputConfiguration.writeOut(arg);
- }
- context.write = contextOptions.write || write;
- context.command = this;
- return context;
- }
-
- outputHelp(contextOptions) {
- let deprecatedCallback;
- if (typeof contextOptions === 'function') {
- deprecatedCallback = contextOptions;
- contextOptions = undefined;
- }
- const context = this._getHelpContext(contextOptions);
- this._getCommandAndAncestors()
- .reverse()
- .forEach((command) => command.emit('beforeAllHelp', context));
- this.emit('beforeHelp', context);
- let helpInformation = this.helpInformation(context);
- if (deprecatedCallback) {
- helpInformation = deprecatedCallback(helpInformation);
- if (
- typeof helpInformation !== 'string' &&
- !Buffer.isBuffer(helpInformation)
- ) {
- throw new Error('outputHelp callback must return a string or a Buffer');
- }
- }
- context.write(helpInformation);
- if (this._getHelpOption()?.long) {
- this.emit(this._getHelpOption().long);
- }
- this.emit('afterHelp', context);
- this._getCommandAndAncestors().forEach((command) =>
- command.emit('afterAllHelp', context),
- );
- }
-
- helpOption(flags, description) {
-
- if (typeof flags === 'boolean') {
- if (flags) {
- this._helpOption = this._helpOption ?? undefined;
- } else {
- this._helpOption = null;
- }
- return this;
- }
-
- flags = flags ?? '-h, --help';
- description = description ?? 'display help for command';
- this._helpOption = this.createOption(flags, description);
- return this;
- }
-
- _getHelpOption() {
-
- if (this._helpOption === undefined) {
- this.helpOption(undefined, undefined);
- }
- return this._helpOption;
- }
-
- addHelpOption(option) {
- this._helpOption = option;
- return this;
- }
-
- help(contextOptions) {
- this.outputHelp(contextOptions);
- let exitCode = process.exitCode || 0;
- if (
- exitCode === 0 &&
- contextOptions &&
- typeof contextOptions !== 'function' &&
- contextOptions.error
- ) {
- exitCode = 1;
- }
-
- this._exit(exitCode, 'commander.help', '(outputHelp)');
- }
-
- addHelpText(position, text) {
- const allowedValues = ['beforeAll', 'before', 'after', 'afterAll'];
- if (!allowedValues.includes(position)) {
- throw new Error(`Unexpected value for position to addHelpText.
- Expecting one of '${allowedValues.join("', '")}'`);
- }
- const helpEvent = `${position}Help`;
- this.on(helpEvent, (context) => {
- let helpStr;
- if (typeof text === 'function') {
- helpStr = text({ error: context.error, command: context.command });
- } else {
- helpStr = text;
- }
-
- if (helpStr) {
- context.write(`${helpStr}\n`);
- }
- });
- return this;
- }
-
- _outputHelpIfRequested(args) {
- const helpOption = this._getHelpOption();
- const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
- if (helpRequested) {
- this.outputHelp();
-
- this._exit(0, 'commander.helpDisplayed', '(outputHelp)');
- }
- }
- }
- function incrementNodeInspectorPort(args) {
-
-
-
-
- return args.map((arg) => {
- if (!arg.startsWith('--inspect')) {
- return arg;
- }
- let debugOption;
- let debugHost = '127.0.0.1';
- let debugPort = '9229';
- let match;
- if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
-
- debugOption = match[1];
- } else if (
- (match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null
- ) {
- debugOption = match[1];
- if (/^\d+$/.test(match[3])) {
-
- debugPort = match[3];
- } else {
-
- debugHost = match[3];
- }
- } else if (
- (match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null
- ) {
-
- debugOption = match[1];
- debugHost = match[3];
- debugPort = match[4];
- }
- if (debugOption && debugPort !== '0') {
- return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
- }
- return arg;
- });
- }
- exports.Command = Command;
|