node.jscommand-line-interfacevorpal.js

How to provide a multi word parameter to Vorpal.js


Using this:

vorpal
.command("echo <somewords>")
.description("Outputs 'what ever you typed")
.action(function (args, callback) {
    this.log(args.somewords);
    callback();
});

I can enter: echo 'some text to echo' and that works as expected, but echo "some other text" just echo's "some

  1. Is there a way to tell the option to include the rest of the line
  2. A way to use both ' or " to encase a multi word paramter

TIA


Solution

  • You can do this using variadic arguments, by adding a ... to the argument. This returns each word back in an array:

    vorpal
    .command("echo <somewords...>")
    .description("Outputs 'what ever you typed")
    .action(function (args, callback) {
        this.log(args.somewords.join(' '));
        callback();
    });