我有一个脚本可以使用获取数十个参数/标志Getopt::Long
。
某些标志不允许混合,例如:--linux --unix
不允许一起供应。我知道我可以使用if
陈述。有没有更干净、更好的方法来做到这一点?
if
如果我不想允许多种标志组合,块可能会变得丑陋。
看来不是这样Getopt::长有这样的功能,快速浏览后没有任何东西突出CPAN搜索。但是,如果您可以使用哈希来存储您的选项,那么创建您自己的函数似乎并不太难看:
use warnings;
use strict;
use Getopt::Long;
my %opts;
GetOptions(\%opts, qw(
linux
unix
help
)) or die;
mutex(qw(linux unix));
sub mutex {
my @args = @_;
my $cnt = 0;
for (@args) {
$cnt++ if exists $opts{$_};
die "Error: these options are mutually exclusive: @args" if $cnt > 1;
}
}
这也可以扩展到两个以上的选项:
mutex(qw(linux unix windoze));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)