diff --git a/lib/command.js b/lib/command.js index d0baa74c2..e803b7710 100644 --- a/lib/command.js +++ b/lib/command.js @@ -926,15 +926,10 @@ Expecting one of '${allowedValues.join("', '")}'`); this._asyncParsing = false; this._setShouldAwait(); - try { - const userArgs = this._prepareUserArgs(argv, parseOptions); - this._parseCommand([], userArgs); + const userArgs = this._prepareUserArgs(argv, parseOptions); + this._parseCommand([], userArgs); - return this; - } finally { - this._asyncParsing = undefined; - this._shouldAwait = undefined; - } + return this; } /** @@ -960,15 +955,10 @@ Expecting one of '${allowedValues.join("', '")}'`); this._asyncParsing = true; this._setShouldAwait(); - try { - const userArgs = this._prepareUserArgs(argv, parseOptions); - await this._parseCommand([], userArgs); + const userArgs = this._prepareUserArgs(argv, parseOptions); + await this._parseCommand([], userArgs); - return this; - } finally { - this._asyncParsing = undefined; - this._shouldAwait = undefined; - } + return this; } /** @@ -991,9 +981,13 @@ Expecting one of '${allowedValues.join("', '")}'`); */ _setShouldAwait() { - for (let cmd = this; this._shouldAwait === undefined; cmd = cmd.parent) { - this._shouldAwait = cmd._await ?? cmd._asyncParsing; - } + this._shouldAwait = undefined; + let cmd = this; + do { + this._shouldAwait = cmd._await; + cmd = cmd.parent; + } while (this._shouldAwait === undefined && cmd); + this._shouldAwait ??= this._asyncParsing; } /** @@ -1199,6 +1193,7 @@ Expecting one of '${allowedValues.join("', '")}'`); _dispatchSubcommand(commandName, operands, unknown) { const subCommand = this._findCommand(commandName); if (!subCommand) this.help({ error: true }); + subCommand._asyncParsing = this._asyncParsing; let hookResult; hookResult = this._chainOrCallSubCommandHook(hookResult, subCommand, 'preSubcommand');