| const EventEmitter = require('events').EventEmitter; |
| const childProcess = require('child_process'); |
| const path = require('path'); |
| const fs = require('fs'); |
|
|
| const { Argument, humanReadableArgName } = require('./argument.js'); |
| const { CommanderError } = require('./error.js'); |
| const { Help } = require('./help.js'); |
| const { Option, splitOptionFlags } = 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._args = []; |
| |
| 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._defaultCommandName = null; |
| this._exitCallback = null; |
| this._aliases = []; |
| this._combineFlagAndOptionalValue = true; |
| this._description = ''; |
| this._argsDescription = undefined; |
| this._enablePositionalOptions = false; |
| this._passThroughOptions = false; |
| this._lifeCycleHooks = {}; |
| |
| this._showHelpAfterError = false; |
| this._showSuggestionAfterError = false; |
|
|
| |
| 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._hasHelpOption = true; |
| this._helpFlags = '-h, --help'; |
| this._helpDescription = 'display help for command'; |
| this._helpShortFlag = '-h'; |
| this._helpLongFlag = '--help'; |
| this._addImplicitHelpCommand = undefined; |
| this._helpCommandName = 'help'; |
| this._helpCommandnameAndArgs = 'help [command]'; |
| this._helpCommandDescription = 'display help for command'; |
| this._helpConfiguration = {}; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| copyInheritedSettings(sourceCommand) { |
| this._outputConfiguration = sourceCommand._outputConfiguration; |
| this._hasHelpOption = sourceCommand._hasHelpOption; |
| this._helpFlags = sourceCommand._helpFlags; |
| this._helpDescription = sourceCommand._helpDescription; |
| this._helpShortFlag = sourceCommand._helpShortFlag; |
| this._helpLongFlag = sourceCommand._helpLongFlag; |
| this._helpCommandName = sourceCommand._helpCommandName; |
| this._helpCommandnameAndArgs = sourceCommand._helpCommandnameAndArgs; |
| this._helpCommandDescription = sourceCommand._helpCommandDescription; |
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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.commands.push(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'); |
|
|
| |
| |
| function checkExplicitNames(commandArray) { |
| commandArray.forEach((cmd) => { |
| if (cmd._executableHandler && !cmd._executableFile) { |
| throw new Error(`Must specify executableFile for deeply nested executable: ${cmd.name()}`); |
| } |
| checkExplicitNames(cmd.commands); |
| }); |
| } |
| checkExplicitNames(cmd.commands); |
|
|
| opts = opts || {}; |
| if (opts.isDefault) this._defaultCommandName = cmd._name; |
| if (opts.noHelp || opts.hidden) cmd._hidden = true; |
|
|
| this.commands.push(cmd); |
| cmd.parent = this; |
| 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.split(/ +/).forEach((detail) => { |
| this.argument(detail); |
| }); |
| return this; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| addArgument(argument) { |
| const previousArgument = this._args.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._args.push(argument); |
| return this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| addHelpCommand(enableOrNameAndArgs, description) { |
| if (enableOrNameAndArgs === false) { |
| this._addImplicitHelpCommand = false; |
| } else { |
| this._addImplicitHelpCommand = true; |
| if (typeof enableOrNameAndArgs === 'string') { |
| this._helpCommandName = enableOrNameAndArgs.split(' ')[0]; |
| this._helpCommandnameAndArgs = enableOrNameAndArgs; |
| } |
| this._helpCommandDescription = description || this._helpCommandDescription; |
| } |
| return this; |
| }; |
|
|
| |
| |
| |
| |
|
|
| _hasImplicitHelpCommand() { |
| if (this._addImplicitHelpCommand === undefined) { |
| return this.commands.length && !this._actionHandler && !this._findCommand('help'); |
| } |
| return this._addImplicitHelpCommand; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| hook(event, listener) { |
| const allowedValues = ['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._args.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); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| addOption(option) { |
| const oname = option.name(); |
| const name = option.attributeName(); |
|
|
| let defaultValue = option.defaultValue; |
|
|
| |
| if (option.negate || option.optional || option.required || typeof defaultValue === 'boolean') { |
| |
| if (option.negate) { |
| const positiveLongFlag = option.long.replace(/^--no-/, '--'); |
| defaultValue = this._findOption(positiveLongFlag) ? this.getOptionValue(name) : true; |
| } |
| |
| if (defaultValue !== undefined) { |
| this.setOptionValueWithSource(name, defaultValue, 'default'); |
| } |
| } |
|
|
| |
| this.options.push(option); |
|
|
| |
| const handleOptionValue = (val, invalidValueMessage, valueSource) => { |
| |
| const oldValue = this.getOptionValue(name); |
|
|
| |
| if (val !== null && option.parseArg) { |
| try { |
| val = option.parseArg(val, oldValue === undefined ? defaultValue : oldValue); |
| } catch (err) { |
| if (err.code === 'commander.invalidArgument') { |
| const message = `${invalidValueMessage} ${err.message}`; |
| this._displayError(err.exitCode, err.code, message); |
| } |
| throw err; |
| } |
| } else if (val !== null && option.variadic) { |
| val = option._concatValue(val, oldValue); |
| } |
|
|
| |
| if (typeof oldValue === 'boolean' || typeof oldValue === 'undefined') { |
| |
| if (val == null) { |
| this.setOptionValueWithSource(name, option.negate ? false : defaultValue || true, valueSource); |
| } else { |
| this.setOptionValueWithSource(name, val, valueSource); |
| } |
| } else if (val !== null) { |
| |
| this.setOptionValueWithSource(name, option.negate ? false : 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) { |
| 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, fn, defaultValue) { |
| return this._optionEx({}, flags, description, fn, defaultValue); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| requiredOption(flags, description, fn, defaultValue) { |
| return this._optionEx({ mandatory: true }, flags, description, fn, 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; |
| if (!!this.parent && passThrough && !this.parent._enablePositionalOptions) { |
| throw new Error('passThroughOptions can not be used without turning on enablePositionalOptions for parent command(s)'); |
| } |
| return this; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| storeOptionsAsProperties(storeAsProperties = true) { |
| this._storeOptionsAsProperties = !!storeAsProperties; |
| if (this.options.length) { |
| throw new Error('call .storeOptionsAsProperties() before adding options'); |
| } |
| return this; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| getOptionValue(key) { |
| if (this._storeOptionsAsProperties) { |
| return this[key]; |
| } |
| return this._optionValues[key]; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| setOptionValue(key, value) { |
| if (this._storeOptionsAsProperties) { |
| this[key] = value; |
| } else { |
| this._optionValues[key] = value; |
| } |
| return this; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| setOptionValueWithSource(key, value, source) { |
| this.setOptionValue(key, value); |
| this._optionValueSources[key] = source; |
| return this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| getOptionValueSource(key) { |
| return this._optionValueSources[key]; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| _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) { |
| argv = process.argv; |
| |
| if (process.versions && process.versions.electron) { |
| parseOptions.from = 'electron'; |
| } |
| } |
| 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; |
| default: |
| throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`); |
| } |
| if (!this._scriptPath && require.main) { |
| this._scriptPath = require.main.filename; |
| } |
|
|
| |
| this._name = this._name || (this._scriptPath && path.basename(this._scriptPath, path.extname(this._scriptPath))); |
|
|
| 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']; |
|
|
| |
| this._checkForMissingMandatoryOptions(); |
|
|
| |
| let scriptPath = this._scriptPath; |
| |
| if (!scriptPath && require.main) { |
| scriptPath = require.main.filename; |
| } |
|
|
| let baseDir; |
| try { |
| const resolvedLink = fs.realpathSync(scriptPath); |
| baseDir = path.dirname(resolvedLink); |
| } catch (e) { |
| baseDir = '.'; |
| } |
|
|
| |
| let bin = path.basename(scriptPath, path.extname(scriptPath)) + '-' + subcommand._name; |
| if (subcommand._executableFile) { |
| bin = subcommand._executableFile; |
| } |
|
|
| const localBin = path.join(baseDir, bin); |
| if (fs.existsSync(localBin)) { |
| |
| bin = localBin; |
| } else { |
| |
| sourceExt.forEach((ext) => { |
| if (fs.existsSync(`${localBin}${ext}`)) { |
| bin = `${localBin}${ext}`; |
| } |
| }); |
| } |
| launchWithNode = sourceExt.includes(path.extname(bin)); |
|
|
| let proc; |
| if (process.platform !== 'win32') { |
| if (launchWithNode) { |
| args.unshift(bin); |
| |
| args = incrementNodeInspectorPort(process.execArgv).concat(args); |
|
|
| proc = childProcess.spawn(process.argv[0], args, { stdio: 'inherit' }); |
| } else { |
| proc = childProcess.spawn(bin, args, { stdio: 'inherit' }); |
| } |
| } else { |
| args.unshift(bin); |
| |
| args = incrementNodeInspectorPort(process.execArgv).concat(args); |
| proc = childProcess.spawn(process.execPath, args, { stdio: 'inherit' }); |
| } |
|
|
| 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; |
| if (!exitCallback) { |
| proc.on('close', process.exit.bind(process)); |
| } else { |
| proc.on('close', () => { |
| exitCallback(new CommanderError(process.exitCode || 0, 'commander.executeSubCommandAsync', '(close)')); |
| }); |
| } |
| proc.on('error', (err) => { |
| |
| if (err.code === 'ENOENT') { |
| const executableMissing = `'${bin}' 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`; |
| throw new Error(executableMissing); |
| |
| } else if (err.code === 'EACCES') { |
| throw new Error(`'${bin}' 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 }); |
|
|
| if (subCommand._executableHandler) { |
| this._executeSubCommand(subCommand, operands.concat(unknown)); |
| } else { |
| return subCommand._parseCommand(operands, unknown); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
|
|
| _checkNumberOfArguments() { |
| |
| this._args.forEach((arg, i) => { |
| if (arg.required && this.args[i] == null) { |
| this.missingArgument(arg.name()); |
| } |
| }); |
| |
| if (this._args.length > 0 && this._args[this._args.length - 1].variadic) { |
| return; |
| } |
| if (this.args.length > this._args.length) { |
| this._excessArguments(this.args); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
|
|
| _processArguments() { |
| const myParseArg = (argument, value, previous) => { |
| |
| let parsedValue = value; |
| if (value !== null && argument.parseArg) { |
| try { |
| parsedValue = argument.parseArg(value, previous); |
| } catch (err) { |
| if (err.code === 'commander.invalidArgument') { |
| const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`; |
| this._displayError(err.exitCode, err.code, message); |
| } |
| throw err; |
| } |
| } |
| return parsedValue; |
| }; |
|
|
| this._checkNumberOfArguments(); |
|
|
| const processedArgs = []; |
| this._args.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 = []; |
| getCommandAndParents(this) |
| .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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
|
|
| _parseCommand(operands, unknown) { |
| const parsed = this.parseOptions(unknown); |
| this._parseOptionsEnv(); |
| 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._hasImplicitHelpCommand() && operands[0] === this._helpCommandName) { |
| if (operands.length === 1) { |
| this.help(); |
| } |
| return this._dispatchSubcommand(operands[1], [], [this._helpLongFlag]); |
| } |
| if (this._defaultCommandName) { |
| outputHelpIfRequested(this, unknown); |
| return this._dispatchSubcommand(this._defaultCommandName, operands, unknown); |
| } |
| if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) { |
| |
| this.help({ error: true }); |
| } |
|
|
| outputHelpIfRequested(this, parsed.unknown); |
| this._checkForMissingMandatoryOptions(); |
|
|
| |
| const checkForUnknownOptions = () => { |
| if (parsed.unknown.length > 0) { |
| this.unknownOption(parsed.unknown[0]); |
| } |
| }; |
|
|
| const commandEvent = `command:${this.name()}`; |
| if (this._actionHandler) { |
| checkForUnknownOptions(); |
| this._processArguments(); |
|
|
| let actionResult; |
| actionResult = this._chainOrCallHooks(actionResult, 'preAction'); |
| actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs)); |
| if (this.parent) this.parent.emit(commandEvent, operands, unknown); |
| actionResult = this._chainOrCallHooks(actionResult, 'postAction'); |
| return actionResult; |
| } |
| 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() { |
| |
| for (let cmd = this; cmd; cmd = cmd.parent) { |
| cmd.options.forEach((anOption) => { |
| if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) { |
| cmd.missingMandatoryOptionValue(anOption); |
| } |
| }); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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 (arg === this._helpCommandName && this._hasImplicitHelpCommand()) { |
| 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; |
| }; |
|
|
| |
| |
| |
| |
| |
| _displayError(exitCode, code, message) { |
| 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 }); |
| } |
| 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()}`); |
| } |
| } |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
|
|
| missingArgument(name) { |
| const message = `error: missing required argument '${name}'`; |
| this._displayError(1, 'commander.missingArgument', message); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| optionMissingArgument(option) { |
| const message = `error: option '${option.flags}' argument missing`; |
| this._displayError(1, 'commander.optionMissingArgument', message); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| missingMandatoryOptionValue(option) { |
| const message = `error: required option '${option.flags}' not specified`; |
| this._displayError(1, 'commander.missingMandatoryOptionValue', message); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| 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._displayError(1, 'commander.unknownOption', message); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| _excessArguments(receivedArgs) { |
| if (this._allowExcessArguments) return; |
|
|
| const expected = this._args.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._displayError(1, 'commander.excessArguments', message); |
| }; |
|
|
| |
| |
| |
| |
| |
|
|
| 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._displayError(1, 'commander.unknownCommand', message); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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.options.push(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; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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'); |
|
|
| 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._args.map((arg) => { |
| return humanReadableArgName(arg); |
| }); |
| return [].concat( |
| (this.options.length || this._hasHelpOption ? '[options]' : []), |
| (this.commands.length ? '[command]' : []), |
| (this._args.length ? args : []) |
| ).join(' '); |
| } |
|
|
| this._usage = str; |
| return this; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| name(str) { |
| if (str === undefined) return this._name; |
| this._name = str; |
| 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); |
|
|
| getCommandAndParents(this).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); |
|
|
| this.emit(this._helpLongFlag); |
| this.emit('afterHelp', context); |
| getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context)); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| helpOption(flags, description) { |
| if (typeof flags === 'boolean') { |
| this._hasHelpOption = flags; |
| return this; |
| } |
| this._helpFlags = flags || this._helpFlags; |
| this._helpDescription = description || this._helpDescription; |
|
|
| const helpFlags = splitOptionFlags(this._helpFlags); |
| this._helpShortFlag = helpFlags.shortFlag; |
| this._helpLongFlag = helpFlags.longFlag; |
|
|
| 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; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| function outputHelpIfRequested(cmd, args) { |
| const helpOption = cmd._hasHelpOption && args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag); |
| if (helpOption) { |
| cmd.outputHelp(); |
| |
| cmd._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; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| function getCommandAndParents(startCommand) { |
| const result = []; |
| for (let command = startCommand; command; command = command.parent) { |
| result.push(command); |
| } |
| return result; |
| } |
|
|
| exports.Command = Command; |
|
|