completer 函数的使用


🌐 Use of the completer function

completer 函数以用户输入的当前行作为参数,并返回一个包含两个条目的数组:

🌐 The completer function takes the current line entered by the user as an argument, and returns an Array with 2 entries:

  • 一个包含匹配完成条目的数组。
  • 用于匹配的子字符串。

例如:[[substr1, substr2, ...], originalsubstring]

🌐 For instance: [[substr1, substr2, ...], originalsubstring].

function completer(line) {
  const completions = '.help .error .exit .quit .q'.split(' ');
  const hits = completions.filter((c) => c.startsWith(line));
  // Show all completions if none found
  return [hits.length ? hits : completions, line];
} 

completer 函数如果接受两个参数,可以异步调用:

🌐 The completer function can be called asynchronously if it accepts two arguments:

function completer(linePartial, callback) {
  callback(null, [['123'], linePartial]);
}