Perl Getopt Using Same Option Multiple Times

本秂侑毒 提交于 2019-12-31 04:28:09

问题


In Perl getopts, is it possible to use the same option multiple times but with different values ? I want to give the user the option of entering different grid coordinates but uses the same option name to minimize confusion.

Ex:

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

The script would then perform a set of actions on those different pairs. There will always be at least one pair but there is no knowing how many pairs from situation to situation.

I would like to avoid: --coords1= --coords2= --coords3= and so on. I do not know how to deal with the unknown quantity of coords pairs with that 1 and 2 and 3 method anyway. I have used getopts in previous projects but am getting into more complex demands/issues. I tried to search for solutions/examples but probably used the wrong keywords. Thnx for any assist.

Rod


回答1:


As documented in 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;

Executed using:

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

Outputs:

10,12
-18,30
4,-25


来源:https://stackoverflow.com/questions/25346520/perl-getopt-using-same-option-multiple-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!