1

在 Perl 脚本中,我使用Getopt::Completetab 键自动完成。一切都很好,除了我想实现的一个功能。

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

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

如何实现这种行为?

4

1 回答 1

1

这个没有测试!

查看文档后,我相信您可以将其'directories'视为实际的子。它在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'];
    }
);
于 2015-11-08T22:49:57.627 回答