我有一个脚本,可以使用 Getopt::Long
获取数十个参数/标志.
某些标志不允许混用,例如:--linux --unix
不允许一起提供。我知道我可以使用 if
检查陈述。有没有更干净、更好的方法来做到这一点?if
如果我不想允许多个标志组合,块会变得丑陋。
最佳答案
好像不是Getopt::Long有这样的功能,用了快search of 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;
}
}
这也可以扩展到 2 个以上的选项:
mutex(qw(linux unix windoze));
关于perl - 避免将某些参数混合到脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10299007/