Hi,
I'm trying to get this to work in an interactive menu-like manner, by combining nested-yargs with a prompt to type in commands. This saves my users from having to type the app name over and over, and I can maintain a state in the running NodeJS instance.
I'm having some problems though, and hoped you had some ideas or maybe even tried this already, in case I'm just wasting my time on a bad way to solve this.
The problem is that I disabled exitProcess(false) on yargs in order to prevent it from shutting down the process on every error or success. I forked nested-yargs to replace the custom error handler's process.exit() with a throw new Error() instead, but still I'm getting 3 help screens due to the custom error handler being called 3 times if I type profile instead of profile list, for which I expect 1 help screen. I suspect maybe the exception flow is not designed for this in yargs, but I haven't fully understood the flow yet.
My app commands are structured something like this:
myapp
--config
|--init
--profile
|--list
|--get
// and so on
I use readline module to prompt the user, and for every input line I create a new yargs instance. Here is a simplification of my setup, where rl is the readline module:
rl.on('line', function(line) {
var newArgs = line.split(' ');
var yargsInstance = yargs(newArgs);
yargsInstance.exitProcess(false);
yargsInstance.showHelpOnFail(false);
try {
var myapp = cli.createApp();
myapp.command(require('./config'));
myapp.command(require('./profile'));
myapp.command(require('./event'));
cli.run(myapp, yargsInstance);
rl.prompt();
} catch (ex) {
console.error('Invalid command.');
rl.prompt();
}
}).on('close', function() {
util.log('close: exiting...');
process.exit();
});
Again, just posting this in case you have some knowledge on this. I'll keep hacking on it meanwhile and report back.
Hi,
I'm trying to get this to work in an interactive menu-like manner, by combining nested-yargs with a prompt to type in commands. This saves my users from having to type the app name over and over, and I can maintain a state in the running NodeJS instance.
I'm having some problems though, and hoped you had some ideas or maybe even tried this already, in case I'm just wasting my time on a bad way to solve this.
The problem is that I disabled
exitProcess(false)on yargs in order to prevent it from shutting down the process on every error or success. I forked nested-yargs to replace the custom error handler'sprocess.exit()with athrow new Error()instead, but still I'm getting 3 help screens due to the custom error handler being called 3 times if I typeprofileinstead ofprofile list, for which I expect 1 help screen. I suspect maybe the exception flow is not designed for this in yargs, but I haven't fully understood the flow yet.My app commands are structured something like this:
I use
readlinemodule to prompt the user, and for every input line I create a new yargs instance. Here is a simplification of my setup, whererlis the readline module:Again, just posting this in case you have some knowledge on this. I'll keep hacking on it meanwhile and report back.