加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:18:22 所属栏目:大数据 来源:网络整理
导读:有没有一种简单的方法来检测Perl模块Getopt :: Long的模糊选项? 例如: #!/usr/bin/env perl# test ambiguous optionsuse Getopt::Long;my $hostname = 'localhost';GetOptions( help = sub { print "call usage sub heren"; exit },'hostname=s' = $host
有没有一种简单的方法来检测Perl模块Getopt :: Long的模糊选项?

例如:

#!/usr/bin/env perl

# test ambiguous options

use Getopt::Long;

my $hostname = 'localhost';

GetOptions( help         => sub { print "call usage sub heren"; 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 heren"; 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.]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读