在 Perl 中使用 Getopt::Long 获取多维数据结构
我让下面的代码可以根据需要工作,但想知道是否有更好的方法来执行此操作而不使用引号。
myScript.pl --filter 'key1 foo bar' --filter 'key2 baz qux'
#!/usr/local/bin/perl5.8.8
use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;
my %filter;
GetOptions("filter=s" => sub { my @args = split(/\s/, $_[1]); $filter{$args[0]}{value1} = $args[1]; $filter{$args[0]}{value2} = $args[2]; });
print Dumper %filter;
我得到了令人满意的输出:
$VAR1 = 'key2';
$VAR2 = {
'value1' => 'baz',
'value2' => 'qux'
};
$VAR3 = 'key1';
$VAR4 = {
'value1' => 'foo',
'value2' => 'bar'
};
但是,我想像这样使用它,不带引号:
myScript.pl --filter key1 foo bar --filter key2 baz qux --other_option ...
key1 和 key2 是这里的哈希键,并且是唯一的。
有什么想法/建议吗?
I got this code below to work as I need, but would like to know if there's a better way of doing this without quotes.
myScript.pl --filter 'key1 foo bar' --filter 'key2 baz qux'
#!/usr/local/bin/perl5.8.8
use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;
my %filter;
GetOptions("filter=s" => sub { my @args = split(/\s/, $_[1]); $filter{$args[0]}{value1} = $args[1]; $filter{$args[0]}{value2} = $args[2]; });
print Dumper %filter;
I get a satisfactory output:
$VAR1 = 'key2';
$VAR2 = {
'value1' => 'baz',
'value2' => 'qux'
};
$VAR3 = 'key1';
$VAR4 = {
'value1' => 'foo',
'value2' => 'bar'
};
However, I would like to use it like this, without quotes:
myScript.pl --filter key1 foo bar --filter key2 baz qux --other_option ...
key1 and key2 are hash keys here and will be unique.
Any ideas/suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我上次检查时(几年前),没有一个 20-30 Getopt 模块将处理一串参数,而我所知道的大多数基于 C 的参数解析代码也无法处理该问题。 (我确实有一个 C 代码解析器可以处理它,前提是您知道要使用多少个参数。目前尚不清楚您是否需要 1 个键和 2 个值,或者值列表是否可以比这更可变。
)可能是我看的不够仔细。
Getopt::Long 有选项:
这将生成一个包含键和两个值的数组。鉴于这需要与具有多个条目的能力相结合(每个
--filter
一个),您可能仍然不走运,但您应该探索Getopt::Long
代码> 非常仔细。When I last checked (a couple of years ago), none of the 20-30 Getopt modules listed on CPAN would handle a string of arguments as you show, and most of the C-based argument parsing code I know of does not handle that either. (I do have a C code parser that handles it, provided you know how many arguments to consume. It is not clear whether you require 1 key and 2 values or whether the list of values can be more variable than that.)
Maybe I didn't look carefully enough.
Getopt::Long has the option:
This would generate an array with the key and two values in it. Given that this needs to be combined with the ability to have multiple entries (one per
--filter
), you may still be out of luck, but you should exploreGetopt::Long
excruciatingly carefully.您可以使用选项名称
<>
指定非选项参数的回调,并适当调整您的%filters
哈希值,如下所示:我想象您的 @ARGV除非你使用
--
,否则最终会是空的。You may specify a callback for non-option arguments using the option name
<>
and adjust your%filters
hash appropriately, like this :I'd imagine your @ARGV would end up empty unless you use a
--
though.