gpt4 book ai didi

perl - 如何在 Perl 的 Term::Shell 中自定义制表符补全?

转载 作者:行者123 更新时间:2023-12-04 05:02:57 27 4
gpt4 key购买 nike

我正在使用 Term::Shell包来实现 CLI 工具。这个包提供了一个 API:comp_CMD

只要用户按下 TAB 键,就会调用此函数。我的要求是:

shell> 堆栈TAB

超过

`shell>堆栈TAB

流样本垃圾

但默认的 comp_CMD 只提供一组 TAB 选项,例如

shell> 堆栈 TAB

超过

`shell>堆栈TAB

over under ### 问题就在这里

我不想在此处over under,而是获取flow sample junk

最佳答案

使用 comp_* 风格的处理程序,只能将完成的单词与最后一个未完成的单词进行匹配。然而,幸运的是,您可以通过重写 catch_comp 函数来获得所需的结果,如下所示;它让一个匹配整个命令行:

my %completion_tree = (
stack => { under => [],
over => [qw(flow sample junk)] }
);

sub catch_comp {
my $o = shift;
my ($cmd, $word, $line, $start) = @_;

my $completed = substr $line, 0, $start;
$completed =~ s/^\s*//;

my $tree = \%completion_tree;
foreach (split m'\s+', $completed) {
last if ref($tree) ne 'HASH';
$tree = $tree->{$_};
}

my @completions;
$_ = ref($tree);
@completions = @$tree if /ARRAY/;
@completions = keys %$tree if /HASH/;
@completions = ($tree)if /SCALAR/;

return $o->completions($word, [@completions]);
}

关于perl - 如何在 Perl 的 Term::Shell 中自定义制表符补全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1177227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com