Perl Getopt :: Long-仅将子参数用于定义的参数

道伊

我希望--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为哈希%export
s%{1,5}期望从1到5key=value

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章