这是我在 stackoverflow 上发表的第一篇文章。 :)
我正在尝试使用 GetOpt::Long 解决这种情况。
./myscript -m/abc -m/bcd -t nfs -m/ecd -t nfs ...
-m是挂载点,-t是文件系统的类型(可以放,但不是必须的)。
Getopt::Long::Configure("bundling");
GetOptions('m:s@' => \$mount, 'mountpoint:s@' => \$mount,
't:s@' => \$fstype, 'fstype:s@' => \$fstype)
这是不对的,我无法配对正确的安装和 fstype
./check_mount.pl -m /abc -m /bcd -t nfs -m /ecd -t nfs
$VAR1 = [
'/abc',
'/bcd',
'/ecd'
];
$VAR1 = [
'nfs',
'nfs'
];
我需要填写未指定的文件类型,例如具有“undef”值。 对我来说最好的解决方案是获取哈希值,例如...
%opts;
$opts{'abc'} => 'undef'
$opts{'bcd'} => 'nfs'
$opts{'ecd'} => 'nfs'
这可能吗?谢谢。
最佳答案
直接用 Getopt::Long
做起来并不容易,但是如果你可以稍微改变参数结构,比如
./script.pl --disk /abc --disk /mno=nfs -d /xyz=nfs
...以下将带您到达您想要的位置(请注意,缺少的类型将显示为空字符串,而不是 undef
):
use warnings;
use strict;
use Data::Dumper;
use Getopt::Long;
my %disks;
GetOptions(
'd|disk:s' => \%disks, # this allows both -d and --disk to work
);
print Dumper \%disks;
输出:
$VAR1 = {
'/abc' => '',
'/mno' => 'nfs',
'/xyz' => 'nfs'
};
关于Perl GetOpt::Long 带有可选参数的多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338421/