【问题标题】:Perl Getopt Using Same Option Multiple TimesPerl Getopt 多次使用相同的选项
【发布时间】:2014-10-10 09:01:00
【问题描述】:

在 Perl getopts 中,是否可以多次使用相同的选项但具有不同的值?我想为用户提供输入不同网格坐标的选项,但使用相同的选项名称以尽量减少混淆。

例如:

my_grid.pl --coords=10,12 --coords=-18,30 --coords=4,-25

然后,脚本将对这些不同的对执行一组操作。总会有至少一对,但不知道不同情况有多少对。

我想避免:--coords1= --coords2= --coords3= 等等。无论如何,我不知道如何使用 123 方法处理未知数量的坐标对。我在以前的项目中使用过getopts,但我遇到了更复杂的需求/问题。我试图搜索解决方案/示例,但可能使用了错误的关键字。感谢您的帮助。

【问题讨论】:

标签: perl getopt


【解决方案1】:

Getopts::Long - Options with multiple values 中所述:

#!/usr/bin/perl
use strict;
use warnings;

use Getopt::Long;

GetOptions(
    "coords=s" => \my @coords,
);

print "$_\n" for @coords;

执行使用:

my_grid.pl --coords=10,12 --coords=-18,30 --coords=4,-25

输出:

10,12
-18,30
4,-25

【讨论】:

  • getopt 很长。谢谢你们俩。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多