Perl Getopt::Complete 回调中的目录完成

Directory completion in Perl Getopt::Complete callback

在 Perl 脚本中,我使用 Getopt::Complete 使用 tab 键自动完成。一切正常,除了我想实现的一项功能。

我想使用在某些情况下提供 'directory' 完成的回调。但是,从回调中返回 'directory' 不起作用,因为需要数组引用。

use Getopt::Complete (
    '<>' => sub {
        my ( $command, $value, $option, $other_opts ) = @_;
        if ( $other_opts->{'<>'} ) {
            return 'directories';    ## here I'd like directory completion
        }
        return ['foo'];
    }
);

如何实现这种行为?

这个没有测试!

在查看文档后,我相信您可以将 'directories' 视为一个实际的 sub。它在 Getopt::Complete::Compgen 中生成并以 Getopt::Complete::directories 结束。所以你应该可以简单地调用它。

use Getopt::Complete (
    '<>' => sub {
        my ( $command, $value, $option, $other_opts ) = @_;
        if ( $other_opts->{'<>'} ) {
            return Getopt::Complete::directories(
                $command, 
                $value, 
                $option, 
                $other_opts,
            ); # forward to directory
        }
        return ['foo'];
    }
);