Yargs: Async handlers for CLIs

Created on 17 Jul 2017  路  1Comment  路  Source: yargs/yargs

I'm using parse to build a CLI app using readline.

My commands execute asynchronously. Is it possible to configure the handler to take a callback (or ideally handle a returned promise)?

I think the simplest way to achieve this would be to pass any value returned by a command handler to the parse method. (So that, for example, the handler can return a promise).

My current workaround is to set a value within yars E.g.,

~~~~
let myYargs = yargs.command({
command: 'run',
handler: yargs => {
yargs._result = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('OK');
resolve();
}, 1000);
});
}
});

let rl = readline.createInterface({ ... });

rl.prompt();
rl.on('line', (input) => {
myYargs.parse(['run'], (err, argv, output) => {
Promise.resolve(argv._result).then(() => {
rl.prompt(); // Get next line.
});
});
});
~~~~

Most helpful comment

@richburdon Async (promise-returning) command handler support was added with PR #1001 .. I think you can probably close this

>All comments

@richburdon Async (promise-returning) command handler support was added with PR #1001 .. I think you can probably close this

Was this page helpful?
0 / 5 - 0 ratings