sno*_*kin 6 perl flags arguments
我有一个脚本,可以使用几十个参数/标志Getopt::Long.某些标志不允许混合,例如:--linux --unix不允许一起提供.现在我知道我可以使用if声明检查,但我确信有更清洁,更好的方法.
if 如果我不想允许许多标志组合,块可能会变得丑陋.
有什么建议?
谢谢,
Getopt::Long似乎没有这样的功能,快速搜索 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));