使用Perl的Getopt :: Long,如何防止模块尝试匹配不明确的选项名称?

Gen*_*tle 5 perl getopt-long

我正在使用该Getopt::Long模块来处理命令行参数.

这个模块的典型行为是我们可以传递-f而不是变量的全名--file.同时,如果我有另一个命令行变量--find,并且如果我只-f在命令提示符下提供,它将返回错误:

Option f is ambiguous (file, find).
Run Code Online (Sandbox Code Playgroud)

我想知道我们怎样才能抑制这种模棱两可的用法呢?

提前致谢.

Zai*_*aid 10

看看Getopt::Long文档:

auto_abbrev

允许选项名称缩写为唯一性.除非已设置环境变量POSIXLY_CORRECT,否则启用默认值,在这种情况下禁用auto_abbrev.


例:

use strict;
use warnings;
use Getopt::Long qw(:config no_auto_abbrev);

my ( $file, $fish );

GetOptions( "file=s" => \$file, "fish=s" => \$fish );
Run Code Online (Sandbox Code Playgroud)

测试:

$ perl test.pl -fi 24
Unknown option: fi

$ perl test.pl -fis 24
Unknown option: fis
Run Code Online (Sandbox Code Playgroud)