Perl Getopt::Long - 仅对定义的参数使用子参数

标签 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到hash %export.
s%{1,5} 需要 1 到 5 个 key=value

关于Perl Getopt::Long - 仅对定义的参数使用子参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394976/

相关文章:

node.js - 如何使用 LWP::UserAgent 接受自签名证书

perl - 如何在 Perl 中对实例方法进行猴子修补?

linux - MooseX::Getopt 将未处理的 token 放在哪里

c - 如何获取以 '+' 字符开头的选项的值?

perl - 如何在 Perl 中验证 SAML 断言?

sql - 如何用面向对象的 Perl 组装 SQL?

html - 如何从 perl 网站中提取特殊类型的表格?

c++ - gengetopt : How to parse a string without an option (like a file name)

c - 为什么gdb一直显示optarg为0x0

c - 在 C 中使用 getopt_long 的文件路径无效