【问题标题】:Is Perl's GetOpt::Long accepting abbreviations of switches a bug?Perl 的 GetOpt::Long 接受开关的缩写是一个错误吗?
【发布时间】:2009-05-28 14:22:52
【问题描述】:

这是我编写的用于测试命令行参数处理的简单脚本:

use Getopt::Long;

my $help = 0;

GetOptions(
'help|h|?' => \$help,
) or die "Error!";

print "OK\n";

我得到的结果如下:

D:\>perl test.pl --help
OK

D:\>perl test.pl --hell
Unknown option: hell
Error! at test.pl line 10.

D:\>perl test.pl --he
OK

D:\>perl test.pl --hel
OK

以前有人注意到这一点吗?行为(接受 he 和 hel 而不是帮助)是潜在的错误吗?

【问题讨论】:

    标签: perl getopt


    【解决方案1】:

    这是一个记录在here的功能

    【讨论】:

      【解决方案2】:

      不,这是故意的。它接受选项的最短非歧义版本,因此如果您有另一个选项“--hex”,它不会接受“--he”,但会接受“--hel”。

      【讨论】:

        【解决方案3】:

        这是feature。只要结果不模棱两可,选项可以被缩写。如果您不希望这种行为,可以通过configuration 禁用它。

        如果这是一个错误,则可以通过bug queuert.cpan.org 来检查它是否为已知错误。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多