* Process arguments in context of this command. * Returns action result, in case it is a promise. * * @private
(operands, unknown)
| 1560 | */ |
| 1561 | |
| 1562 | _parseCommand(operands, unknown) { |
| 1563 | const parsed = this.parseOptions(unknown); |
| 1564 | this._parseOptionsEnv(); // after cli, so parseArg not called on both cli and env |
| 1565 | this._parseOptionsImplied(); |
| 1566 | operands = operands.concat(parsed.operands); |
| 1567 | unknown = parsed.unknown; |
| 1568 | this.args = operands.concat(unknown); |
| 1569 | |
| 1570 | if (operands && this._findCommand(operands[0])) { |
| 1571 | return this._dispatchSubcommand(operands[0], operands.slice(1), unknown); |
| 1572 | } |
| 1573 | if ( |
| 1574 | this._getHelpCommand() && |
| 1575 | operands[0] === this._getHelpCommand().name() |
| 1576 | ) { |
| 1577 | return this._dispatchHelpCommand(operands[1]); |
| 1578 | } |
| 1579 | if (this._defaultCommandName) { |
| 1580 | this._outputHelpIfRequested(unknown); // Run the help for default command from parent rather than passing to default command |
| 1581 | return this._dispatchSubcommand( |
| 1582 | this._defaultCommandName, |
| 1583 | operands, |
| 1584 | unknown, |
| 1585 | ); |
| 1586 | } |
| 1587 | if ( |
| 1588 | this.commands.length && |
| 1589 | this.args.length === 0 && |
| 1590 | !this._actionHandler && |
| 1591 | !this._defaultCommandName |
| 1592 | ) { |
| 1593 | // probably missing subcommand and no handler, user needs help (and exit) |
| 1594 | this.help({ error: true }); |
| 1595 | } |
| 1596 | |
| 1597 | this._outputHelpIfRequested(parsed.unknown); |
| 1598 | this._checkForMissingMandatoryOptions(); |
| 1599 | this._checkForConflictingOptions(); |
| 1600 | |
| 1601 | // We do not always call this check to avoid masking a "better" error, like unknown command. |
| 1602 | const checkForUnknownOptions = () => { |
| 1603 | if (parsed.unknown.length > 0) { |
| 1604 | this.unknownOption(parsed.unknown[0]); |
| 1605 | } |
| 1606 | }; |
| 1607 | |
| 1608 | const commandEvent = `command:${this.name()}`; |
| 1609 | if (this._actionHandler) { |
| 1610 | checkForUnknownOptions(); |
| 1611 | this._processArguments(); |
| 1612 | |
| 1613 | let promiseChain; |
| 1614 | promiseChain = this._chainOrCallHooks(promiseChain, 'preAction'); |
| 1615 | promiseChain = this._chainOrCall(promiseChain, () => |
| 1616 | this._actionHandler(this.processedArgs), |
| 1617 | ); |
| 1618 | if (this.parent) { |
| 1619 | promiseChain = this._chainOrCall(promiseChain, () => { |
no test coverage detected