Perl Getopt 多次使用相同的选项 [英] Perl Getopt Using Same Option Multiple Times

查看:56
本文介绍了Perl Getopt 多次使用相同的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如:

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.

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

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.

推荐答案

Getopts::Long - 具有多个值的选项:

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;

使用:

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

输出:

10,12
-18,30
4,-25

这篇关于Perl Getopt 多次使用相同的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆