0

我注意到有一个实用方法 $.terminal.parse_arguments 但我不确定是否应该在命令上调用它(我假设它总是只是一个字符串?)

如果我这样使用它,我会收到一个错误“无法读取属性匹配”,这似乎表明可以以某种方式设置和处理更复杂的命令对象。请有人启发我。谢谢

4

1 回答 1

0

要解析命令,您可以使用两种方法:

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

或者

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

split 命令不会将数字和正则表达式转换为对象。

这两种方法都将返回如下所示的对象:

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

编辑:从 1.17.0 版开始,您可以使用它$.terminal.parse_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
}
*/

您还可以在对象解释器中使用解析选项:

$('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
});

然后sudo rm -rf /将打印wipe /.

于 2016-06-07T14:29:31.203 回答