dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

mode should be just like parent #104

Closed mvayngrib closed 8 years ago

mvayngrib commented 8 years ago

for discussion

right now mode behaves differently from the parent vorpal, and thus the examples such as SQL and REPL are very primitive. I would argue that mode should be just like vorpal, and then SQL and REPL can work just fine through catch('[command]'), and modes can be fully interactive.

dthree commented 8 years ago

Could you please clarify a bit with actual examples? I'm having a little bit of trouble picking up what you mean.

Is it that you want several types of commands when in a mode, and to have the full Vorpal API as being separate from when you aren't in a mode?

In this case, I've found the best solution is to just create multiple instances of Vorpal, and toggle them with .show():

var sqlMode = new Vorpal().delimiter('sql-mode:');
var replMode = new Vorpal().delimiter('repl-mode:');
var default = new Vorpal();

sqlMode.use(tonsOfSqlCommandsAndCustomCatch);
replMode.use(tonsOfReplCustomizations);

default.command('sql mode').action(function (args, cb) {
  sqlMode.show();
  cb();
});

default.show();

Does this work?

mvayngrib commented 8 years ago

@dthree good idea! will try it out. But I'm curious, what was the intention behind making the modes different from vorpal instances?

dthree commented 8 years ago

Great!

Mode was intended to just be a special command, sort of like a custom catch command. It wasn't intended as a huge end-all. I personally have found several use cases, but none were major.


If my idea works out, would you mind posting your question on StackOverflow with the vorpal.js tag? I think you're the third person whose asked me a question like this, so it would be good to have it on SO.

mvayngrib commented 8 years ago

absolutely, will do after I give it a try (may take a day or two to get to it, busy)

dthree commented 8 years ago

Okay great no problem.

mvayngrib commented 8 years ago

@dthree looks like it works well except for the sigint handling: on sigint from sub, it exits

const Vorpal = require('vorpal')
const main = Vorpal()
  .delimiter('main')
  .show()

main
  .command('sub')
  .action(function (args, cb) {
    sub.show()
    cb()
  })

const sub = Vorpal()
  .delimiter('sub')

sub
  .sigint(function () {
    main.show()
  })
  .find('exit')
  .action(function (args, cb) {
    main.show()
    cb()
  })

sub
  .command('hey')
  .action(function (args, cb) {
    this.log('ho')
    cb()
  })
dthree commented 8 years ago

Good point.

I have an undocumented command that overrides default SIGINT, you can use that. I may make it official soon. Here's a usage example:

https://github.com/vorpaljs/cash/blob/master/src/index.js#L142