如何使用 Perl 的 Getopt::Long 按照用户输入的顺序获取选项?

发布于 2024-07-08 06:17:39 字数 519 浏览 12 评论 0原文

我有一个现有的 Perl 程序,它使用 Getopt 包和 Getopt::Long::Configure 以及 permute 作为选项之一。 但是,现在我需要保留用户输入的选项的顺序。 Long.pm 中提到了一个选项 $RETURN_IN_ORDER,但似乎根本没有在任何地方使用。

当我通过 return_in_order 时,出现以下错误。


Getopt::Long:C:/Program Files/IBM/RationalSDLC/common/lib/perl5/5.8.6/Getopt/Long.pm 第 1199 行的未知配置参数“return_in_order”。


有人可以告诉我这是否受支持吗如果是的话,正确的使用方法是什么? 如果没有,我想知道我还有其他选择。

谢谢。

I have an existing Perl program that uses Getopt package and Getopt::Long::Configure with permute as one of the options. However, now I need to keep the order of the options entered by the user. There is an option $RETURN_IN_ORDER mentioned in the Long.pm, however doesn't seem to be used anywhere at all.

When I pass return_in_order, I am getting the following error.


Getopt::Long: unknown config parameter "return_in_order" at C:/Program Files/IBM/RationalSDLC/common/lib/perl5/5.8.6/Getopt/Long.pm line 1199.


Can someone please tell me if this is supported at all and if so, the right way to use? If not, I would like to know the other alternatives I have.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别把无礼当个性 2024-07-15 06:17:40

您有两个指向“require_order”的答案,但我认为这两个答案都误解了“require_order”的作用和您所寻求的内容。

如果未设置“require_order”,则可以(在命令行上)编写:

-a file -b

其中 -a 和 -b 都是简单选项(不带参数)。 设置“require_order”后,“file”的存在将终止选项,并且“-b”标志变为“文件名”。

我认为您正在寻找一种机制,可以让您知道“-a”出现在“文件”和“-b”之前。 我不认为 Getopt::Long 支持这一点。 事实上,我不知道有任何 Getopt::* 模块可以做到这一点(除了我自己的、未发布的、我有时使用的 Getopt::JLSS)。 [如果您对代码感兴趣,请通过 Gmail 向我发送电子邮件,并在名字和姓氏之间使用点。]

You have two answers pointing at 'require_order', but I think both those answers are misunderstanding both what 'require_order' does and what you seek.

If 'require_order' is unset, then you can write (on the command line):

-a file -b

Where both -a and -b are simple options (not taking an argument). With 'require_order' set, the presence of 'file' terminates the options and the '-b' flag becomes a 'file name'.

What I think you are seeking is a mechanism that allows you to tell that '-a' appeared before both 'file' and '-b'. I do not think that Getopt::Long supports that. In fact, I'm not aware of any Getopt::* module that does that (other than my own, unpublished, Getopt::JLSS which I sometimes use). [If you are interested in the code, send me an email at Gmail, using a dot between first and last name.]

在梵高的星空下 2024-07-15 06:17:40

根据我这里的联机帮助页,它称为 require_order。 :-)

It's called require_order, according to the manpage I have here. :-)

逆光下的微笑 2024-07-15 06:17:40

如果您提供一个对程序的调用示例,也许会有帮助。

Getopt::Long 支持将选项与普通“位置”参数混合:

./foo --option1 --option2 file1 file2 file3

如果您仅使用 GetOptions 解析选项,则 file1 到 3 将按照它们出现的顺序保留在 @ARGV 中。 也许你可以利用这种行为来实现你想要的。

此外,您可以多次指定相同的选项并将结果放入数组中(按顺序!):

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my @permute;
GetOptions(
  'p=s' => \@permute,
);

print "$_\n" for @permute;

“perl foo.pl -p=1 -p=2 -p=3 -p=4”会产生此输出: (注意:“-p X”与“-p=X”作用相同)

1
2
3
4

Maybe it would have helped if you provided an example call to your program.

Getopt::Long supports mixing options with ordinary "positional" args:

./foo --option1 --option2 file1 file2 file3

If you only parse the options with GetOptions, file1 to 3 will be left in @ARGV in the order they appeared. Maybe you can use that behaviour to achieve what you want.

Furthermore, you can specify the same option multiple times and have the results put in an array (in order!):

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my @permute;
GetOptions(
  'p=s' => \@permute,
);

print "$_\n" for @permute;

"perl foo.pl -p=1 -p=2 -p=3 -p=4" results in this output: (Note: "-p X" works the same as "-p=X")

1
2
3
4
游魂 2024-07-15 06:17:40

我的情况的要求非常不同,因为命令行参数将是选项和参数的组合,如下所示。

mytool -arg1 value 1 -arg2 value2 -arg3 value3

这就是为什么 require_order 不是特别有用。

正如我刚刚发现的,问题并不在于解析组件。 它与用于存储它们的哈希对象一起。

正如 tsee right 所指出的,如果我们使用默认配置,顺序将是相同的。

不管怎样,谢谢大家。

The requirement in my case is very different as the command line parameters are going to be a combination of options and arguments, such as the following.

mytool -arg1 value 1 -arg2 value2 -arg3 value3

And this is why require_order is not particularly helpful.

And as I just figured out, the problem is not really with the parsing component. It's with the hash object that is used to store them.

As tsee right pointed out, the order will be same if we use the default configurations.

Thanks folks, anyways.

甜`诱少女 2024-07-15 06:17:40

需要知道指定选项的顺序是我仍然使用 Getopt::Mixed 的原因。 该模块有一个 nextOption 函数,您可以循环遍历该函数,它会按照命令行上的顺序为您提供选项。

我的用例是包含和排除正则表达式:

myscript /usr --include /usr/local/bin --exclude /usr/local

当然实际发生的情况也取决于我如何处理包含并通过脚本排除内部,但我需要知道顺序。

Needing to know the order in which options were specified is why I still use Getopt::Mixed. That module has a nextOption function which you can loop over, and it will give you the options in the order they were on the command line.

My use case for this is include and exclude regexps:

myscript /usr --include /usr/local/bin --exclude /usr/local

What actually happens of course also depends on how I handle the includes and excludes inside by script, but I need to know the order.

╰つ倒转 2024-07-15 06:17:40

参加聚会非常。 我使用 选项处理子例程。 我的特殊用例是,我需要以任何顺序处理任意数量的 -e-f 选项,并保留该顺序(对于个人项目)。

use Data::Dumper; use Getopt::Long; use strict; use warnings;
my @Sources;    # Each element is [was -f, parameter]

my $dr_save_source = sub {      # Called each time we see -e or -f
    my ($which, $text) = @_;
    push @Sources, [$which eq "f", $text];
}; # dr_save_source

GetOptions("e=s@" => $dr_save_source, "f=s@" => $dr_save_source);
print Dumper(\@Sources);

提供子例程引用 $dr_save_source 作为与选项规范 (=> $dr_save_source) 关联的值,告诉 GetOptions 调用该子例程来处理每当该选项出现时。

随着选项数量的增加,这可能会有点笨拙,但它是......一个选项。

Very late to the party. I did this using an option-handling subroutine. My particular use case is that I need to handle any number of -e <foo> and -f <foo> options in any order, preserving that order (for a personal project).

use Data::Dumper; use Getopt::Long; use strict; use warnings;
my @Sources;    # Each element is [was -f, parameter]

my $dr_save_source = sub {      # Called each time we see -e or -f
    my ($which, $text) = @_;
    push @Sources, [$which eq "f", $text];
}; # dr_save_source

GetOptions("e=s@" => $dr_save_source, "f=s@" => $dr_save_source);
print Dumper(\@Sources);

Providing the subroutine reference $dr_save_source as the value associated with an option specification (=> $dr_save_source) tells GetOptions to call that subroutine to handle the option whenever it shows up.

This might get a bit clumsy as the number of options increased, but it is ... an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文