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 /
.