javascriptvorpal.js

Is there a way to override the Vorpal mode exit command?


I'm using Vorpal mode to allow users to type in text, that I later parse (I couldn't find a way to do it with a regular command since I can't account for all text in advance). When the user types exit he's taken out of the mode. I'd like that to be a different string - or perhaps an additional string (say quit or end).

Is there a way I can either override the exit command, or failing that, trigger it myself when a certain string is provided by the user?

Sample code:
Here's an exercise that implements a Reverse Polish Notation calculator. If you look at the handleInput function (line 47), you'll see that I had to manually handle the q string and kill the process - where all I want it to do is behave like the mode's exit command (somewhere around line 74).


Solution

  • There isn't a way to currently do this, however you can file an issue on Github to add it as an enhancement, as this is a good idea.

    However, it looks like the catch command might be more what you're looking for. This accepts any arbitrary command, without being in a "mode".


    Update

    In looking at your code, it looks like catch would do the trick. Something like this:

    vorpal
      .catch('[input...]')
      .action(function (args, cb) {
        handleInput(args.input.join(' '), cb);
      });
    

    In the above example, it will be as if you are already in a "mode" as soon as the application starts. Any command that doesn't match a pre-defined command (such as exit) will call the catch method instead, and it can behave like a mode by accepting variadic arguments and joining them.

    Does that make sense?