dthree / vorpal

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

example for nesting if at all possible #9

Closed lorneb closed 9 years ago

lorneb commented 9 years ago

Hi not sure if this even part of what Vorpal is all about, I did see the mode options, but what I wanted to know was

  1. Can I use Vorpal to make nested command sets. I''l try to explain. at the top level you might have commands some of which take you down into a new level, at which point you now have a new delimiter and a new set of commands, one of which might be .. to take you back up a level. I was looking for that kind of thing.
  2. If it would be possible could you point me in the right direction
dthree commented 9 years ago

The mode command is probably the closest option you have to this. Vorpal doesn't support namespacing command sets per se, but when you separate out command groups by spaces, such as get *, it groups them accordingly in the help menu. You can then use the catch command to perhaps simulate the freezing of a "directory" or something.

Like if there was a command get foobar, and you did a command like cd get, you could reset your delimiter to app~$ get: or something. Then the foobar string, when executed, it would call the catch command (as there is no foobar command). The catch command could know you were in the get directory, and then programmatically execute get foobar for the user.

In other words, it isn't strictly supported, but you could work it.

Does that help?

AljoschaMeyer commented 9 years ago

Wouldn't it be possible to instanciate vorpal multiple times and give different commands to these instances? The main instance could then provide different modes, whose .action() function simply delegates to a certain instance.

Or would there be problems with multiple different session objects?

dthree commented 9 years ago

Haha - @AljoschaMeyer you're really smart! Great idea - Vorpal actually supports this! The vorpal.show() command attaches a specific instance to the UI.

Try this:

function switch(vorpal) {
  vorpal.command('switch <instance>')
    .action(function (args, cb) {
      instances[args.instance].show();
    });
  return vorpal;
}

var instances = {
  'a': new Vorpal().use(switch).delimiter('a:'),
  'b': new Vorpal().use(switch).delimiter('b:'),
  'c': new Vorpal().use(switch).delimiter('c:'),
}
lorneb commented 9 years ago

Excellent thanks very much that certainly has given me a great start. Are you happy to take questions via the Issues, or is there a better place?

dthree commented 9 years ago

No probs :)

I'd wouldn't mind taking the questions to Stack Overflow: Vorpal is in its infancy (haven't started promoting it yet), but I fully intend it to become a major library. Your question above is a perfect SO-type question for it.

And If you have questions there and I don't see them, you can ping me on Gitter (see badge on readme) to remind me its there :)

dthree commented 9 years ago

Closing this.

jstrika commented 8 years ago

I think you have a bug (version 1.4.0)

If you run example from above, and try to "switch a" twice, the instance will exit !?

$ node switcher.js
a: switch a
a: switch a
$
dthree commented 8 years ago

k

mke66djx commented 7 years ago

I'm having issues with my history when working on different instances of Vorpal. Do we need to create a new vorpal each time we switch instances? I would think you would only run new once to create a new instance then be able to switch between them by just using show. I'm not even really sure that is my problem at the moment.

mke66djx commented 7 years ago

Hi, I've been working on the issue of multiple instances for a while now and was wondering you any of you can comment on why this isn't working for me.

Running your example above, in my test below, I experience a lot of unexpected behavior, one of them being issues with tab-tab to get list of commands, and same for autocomplete using autocomplete-fs. It will return the commands 4 times for each tab-tab or the autocomplete directory results four times. Making it almost unusable. Even without the autocomplete, a regular tab will return 4 sets of commands. Can anyone comment on what I'm doing wrong please?

const Vorpal = require('vorpal'); const mainVorpal = new Vorpal(); const path = require('path'); const fsAutocomplete = require('vorpal-autocomplete-fs');

mainVorpal .delimiter('X:') .use(swap) .show();

function swap(mainVorpal) { mainVorpal.command('sw ') .autocomplete(fsAutocomplete()) .action(function (args, cb) { instances[args.instance].show(); cb(); }); return mainVorpal; }

var instances = { 'a': new Vorpal().use(swap).delimiter('a:'), 'b': new Vorpal().use(swap).delimiter('b:'), 'c': new Vorpal().use(swap).delimiter('c:'), }