使用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]);
}