【发布时间】:2017-06-22 19:51:20
【问题描述】:
我正在使用 Readline 模块使用 NodeJS 制作一个简单的 CLI 应用程序。我想自动完成用户的输入。为此,我使用了模块的autocompletion function:
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];
}
使用此功能,我可以完成一个命令,但不能在同一行中完成多个命令:
例如:
(CLI App) > .e<tab>
.error .exit
(CLI App) > .err<tab>
(CLI App) > .error
(CLI App) > .error .ex<tab>
.help .error .exit .quit .q
我修改了完成函数以仅获取用户正在编写的当前命令的自动完成建议:
function completer(line) {
const completions = '.help .error .exit .quit .q'.split(' ');
const hits = completions.filter((c) => c.startsWith(line.split(' ').slice(-1)));
return [hits.length ? hits : completions, line];
}
我得到了正确的建议,但用户输入没有改变:
(CLI App) > .e<tab>
.error .exit
(CLI App) > .err<tab>
(CLI App) > .error
(CLI App) > .error .ex<tab>
.exit
(CLI App) > .error .ex
有没有办法解决这个问题?非常感谢您提供的任何帮助。
谢谢。
【问题讨论】:
-
如果你只有一个命中,如何将
line的最后一部分替换为命中?添加应该不难 -
谢谢@ChrisSatchell。我替换了
line的最后一部分,它起作用了!!
标签: node.js autocomplete console-application command-line-interface