-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider supporting async option/argument parsers #1900
Comments
It is quite tricky supporting mixed sync and async, but it is something I am interested in if if can be done in a way that does not require changing too much code. A suggestion about your example work-around. You can have an async action handler, in which case you call The two callbacks that currently support async are the action handler, and the event hook. |
I only added |
I have come up with a very simple solution thanks to your mention of event hooks. async function awaitHook(thisCommand, actionCommand) {
actionCommand.processedArgs = await Promise.all(
actionCommand.processedArgs
);
}
await program.hook('preAction', awaitHook).parseAsync(); This achieves exactly what I wanted. |
Nice partial solution! I had not thought of action hook or looking at |
Does not work for options though unless I access |
Oh nevermind, I have just discovered there is async function awaitHook(thisCommand, actionCommand) {
actionCommand.processedArgs = await Promise.all(
actionCommand.processedArgs
);
const entries = Object.entries(actionCommand.opts());
for (const [key, value] of entries) {
actionCommand.setOptionValue(key, await value);
}
} A good idea would be to add a shortcut for |
Building in support for async option/argument parsers is fairly messy to support usage without an action handler, and there are workarounds which are reasonably simple for the author to implement by returning promise from parser and await the results later in parsing: #1900 (comment) See also PRs adding built-in support for the await-promise-later approach: #1902 #1913 #1915 Feel free to open a new issue if it comes up again, with new information and renewed interest. Thank you for your contributions. |
I would like to parse an argument that is expected to equal an index to an array that can only be read asynchronously. Currently, I see no better way than to use an async argument parser and handle the promise it returns in the action handler. Here is a simple example with lowdb:
Obviously, it would be better and more intuitive if the action handler execution were deferred until after the promise is resolved.
Possible implementations:
explicit, something along these lines:
defer action handler execution whenever parser returns a thenable object
program.respectAsyncArgParsers(true)
parseAsync()
method was called (breaking)The text was updated successfully, but these errors were encountered: