Skip to content

Commit d1686db

Browse files
committed
Add subroutine for common parse call code
Borrowed from tj#1917 and tj#1919.
1 parent 7689506 commit d1686db

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/command.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,21 @@ Expecting one of '${allowedValues.join("', '")}'`);
897897
return userArgs;
898898
}
899899

900+
/**
901+
* @param {boolean} async
902+
* @param {Function} userArgsCallback
903+
* @param {string[]} [argv]
904+
* @param {Object} [parseOptions]
905+
* @param {string} [parseOptions.from]
906+
* @return {Command|Promise}
907+
* @api private
908+
*/
909+
910+
_parseSubroutine(async, userArgsCallback, argv, parseOptions) {
911+
const userArgs = this._prepareUserArgs(argv, parseOptions);
912+
return userArgsCallback(userArgs);
913+
}
914+
900915
/**
901916
* Parse `argv`, setting options and invoking commands when defined.
902917
*
@@ -915,10 +930,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
915930
*/
916931

917932
parse(argv, parseOptions) {
918-
const userArgs = this._prepareUserArgs(argv, parseOptions);
919-
this._parseCommand([], userArgs);
920-
921-
return this;
933+
return this._parseSubroutine(false, (userArgs) => {
934+
this._parseCommand([], userArgs);
935+
return this;
936+
}, argv, parseOptions);
922937
}
923938

924939
/**
@@ -941,10 +956,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
941956
*/
942957

943958
async parseAsync(argv, parseOptions) {
944-
const userArgs = this._prepareUserArgs(argv, parseOptions);
945-
await this._parseCommand([], userArgs);
946-
947-
return this;
959+
return this._parseSubroutine(true, async(userArgs) => {
960+
await this._parseCommand([], userArgs);
961+
return this;
962+
}, argv, parseOptions);
948963
}
949964

950965
/**

0 commit comments

Comments
 (0)