perl – 使用Getopt :: Long检测不明确的选项

前端之家收集整理的这篇文章主要介绍了perl – 使用Getopt :: Long检测不明确的选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一种简单的方法来检测Perl模块Getopt :: Long的模糊选项?

例如:

#!/usr/bin/env perl

# test ambiguous options

use Getopt::Long;

my $hostname = 'localhost';

GetOptions( help         => sub { print "call usage sub here\n"; exit },'hostname=s' => \$hostname,);

print "hostname == '$hostname'\n";

默认情况下,Getopt :: Long支持唯一缩写.对于非唯一缩写,会抛出警告并且脚本继续以其快乐的方式继续.

./t.pl -h not_localhost

Option h is ambiguous (help,hostname)
hostname == 'localhost'

我希望我的脚本立即死于不明确的选项,以便立即通知并防止它以意外的默认值运行.

解决方法

GetOptions returns false表示失败.

尝试:

GetOptions( help         => sub { print "call usage sub here\n"; exit },)
    or die "You Failed";

考虑善待您的用户并使用Pod::Usage.我自己的脚本通常看起来像这样:

use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
GetOptions(...)
    or pod2usage(2);

[actual code]

__END__
=head1 NAME
myscript.pl - My Awesome Script
=head1 SYNOPSYS
[etc.]

猜你在找的Perl相关文章