8

I am working on a command line application based on Vorpal (http://vorpal.js.org/) and thus NodeJs.

I was wondering whether there was a way to allow (and list in the help) commands depending on a context.

For instance I might want to have the possibility to execute commands A and B on context 1 and commands C and D on context 2. I would then have a command to switch context which should always be valid.

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
  • Can you provide an example of what you are looking for? – Tarun Lalwani Apr 07 '18 at 14:58
  • Usually you can group the commands by using spaces. So you can have `.command("context1 A")`, `.command("context1 B")`, `.command("context2 C")` and `.command("context2 D")` – Tarun Lalwani Apr 08 '18 at 10:39
  • this not what I meant. Suppose I type 'help'. I want Vorpal to print a list of three possible commands "context1", "context2" and "context3". Then I type "context1". A this point if I type help I want Vorpal to print two possible commands: "A" and "B". – Andrea Sindico Apr 08 '18 at 14:31
  • You mean when you tab on the shell? – Tarun Lalwani Apr 08 '18 at 14:38
  • I think what you are looking for is this https://stackoverflow.com/questions/5570795/how-does-bash-tab-completion-work, When you run the program the auto complete of commands is outside the responsibility of the program. So what you are looking for is not `vorpal's` functionality rather bash functionality – Tarun Lalwani Apr 08 '18 at 19:55
  • I am not looking for auto complete function but for the possibility to have hierarchical commands driven by context – Andrea Sindico Apr 09 '18 at 04:38
  • That is why i am saying please update the question with some example so everyone is on same page – Tarun Lalwani Apr 09 '18 at 04:41

1 Answers1

4

You need to combine the function show and redefine the exit function for the context. Simplified implementation example:

var Vorpal = require('vorpal')
var mainDelimiter = 'main'
var main = new Vorpal().delimiter(mainDelimiter + '>')

var contexts = [
    {
        name: 'context1', help: 'context1 help',
        init: function (instance) {
            instance
                .command('A', 'A help')
                .action(function (args, cb) {
                    this.log('A...')
                    cb()
                })
            instance
                .command('B', 'B help')
                .action(function (args, cb) {
                    this.log('B...')
                    cb()
                })
        }
    },
    {
        name: 'context2', help: 'context2 help',
        init: function (instance) {
            instance
                .command('C', 'C help')
                .action(function (args, cb) {
                    this.log('C...')
                    cb()
                })
            instance
                .command('D', 'D help')
                .action(function (args, cb) {
                    this.log('D...')
                    cb()
                })
        }
    }

]

contexts.forEach(function (ctx, i) {
    var instance = new Vorpal().delimiter(mainDelimiter + '/' + ctx.name + '>')
    ctx.init(instance)

    // Override the function "exit" for the context
    instance.find('exit').remove()
    instance
        .command('exit', 'Exit context')
        .action(function (args, cb) {
            // Switch to the main context
            main.show()
            cb()
        })
    main
        .command(ctx.name, ctx.help)
        .action(function (args, cb) {
            // Switch to the selected context
            instance.show()
            cb()
        })
})

main.show()
stdob--
  • 28,222
  • 5
  • 58
  • 73