0

I notice there is a utility method $.terminal.parse_arguments but I'm not sure if this should be called on the command (which I'm assuming is always just a string?)

If I use it like this I get an error 'cannot read property match', which seems to suggest more complex command objects can be setup and handled somehow. Please someone enlighten me. Thanks

mikejw
  • 183
  • 1
  • 2
  • 12

1 Answers1

0

To parse command you can use two methods:

$.terminal.parse_command('command arg1 arg2');

or

$.terminal.split_command('command arg1 arg2');

split command will not convert numbers and regexes to objects.

both methods will return object that look like this:

{
  name: 'command',
  args: ['arg1', 'arg2'],
  command: 'command arg1 arg2',
  rest: 'arg1 arg2'
}

EDIT: from version 1.17.0 you can use $.terminal.parse_options to parse command line options:

var cmd = $.terminal.parse_command('rm -rf /');
var options = $.terminal.parse_options(cmd.args, { boolean: ['f', 'r']});
console.log(options);
/*
{
  "_": [
    "/"
  ],
  "r": true,
  "f": true
}
*/

You can also use parse options in object interpreter:

$('body').terminal({
  sudo(command, ...args) {
     const options = $.terminal.parse_options(args, { boolean: ['f', 'r']});
     if (command === 'rm' && options.r && options.f) {
        if (options._.length) {
           const [dir] = options._;
           this.echo(`wipe ${dir}`);
        } else {
           this.error('rm: missing operand');
        }
     }
  }
}, {
  checkArity: false
});

then sudo rm -rf / will print wipe /.

jcubic
  • 61,973
  • 54
  • 229
  • 402