Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Interactive menu #1

@angularsen

Description

@angularsen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions