【发布时间】:2012-06-11 03:13:11
【问题描述】:
如何检查是否只定义了 -a 或 -b 或 -c 之一?
所以不要在一起-a -b,也不是-a -c,也不是-b -c,也不是-a -b -c。
现在有
use strict;
use warnings;
use Carp;
use Getopt::Std;
our($opt_a, $opt_b, $opt_c);
getopts("abc");
croak("Options -a -b -c are mutually exclusive")
if ( is_here_multiple($opt_a, $opt_c, $opt_c) );
sub is_here_multiple {
my $x = 0;
foreach my $arg (@_) { $x++ if (defined($arg) || $arg); }
return $x > 1 ? 1 : 0;
}
以上方法可行,但不是很优雅。
Here 已经是类似的问题,但这次不同,因为检查 两个 独占值很容易 - 但这里有多个。
【问题讨论】:
标签: perl