我希望 --import 参数有一个“子参数”,它只对这个参数起作用,对其他任何地方都不起作用。示例:
app.pl --import --fresh
输出:命令工作
app.pl --export
输出:命令工作
app.pl --export --fresh
输出:找不到命令
这可以通过 GetOpt::Long 实现吗?请稍微指导一下。
最佳答案
我认为如果不求助于部分解析,使用 Getopt::Long 最接近的是:
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
GetOptions('export=s%{1,5}'=>\my %export, 'another_option=s'=>\my $ao);
print Dumper({ 'export'=> \%export, 'another_option'=>$ao});
perl t1.pl --export fresh=1 b=2 c=3 --another_option value
$VAR1 = {
'export' => {
'c' => '3',
'b' => '2',
'fresh' => '1'
},
'another_option' => 'value'
};
这里export=s%{1,5}
解析--export fresh=1 b=2 c=3
到hash %export
.
s%{1,5}
需要 1 到 5 个 key=value
对
关于Perl Getopt::Long - 仅对定义的参数使用子参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394976/