perl - 我如何使用 Perl 的 Getopt::Long 处理 -r=<pattern>?

标签 perl getopt-long

我正在使用 Getopt::Long 在 Perl 中解析命令行选项.我被迫使用前缀 - (一个破折号)用于短命令 ( -s ) 和 -- (双破折号)表示长命令(例如 --input=file )。

我的问题是有一个特殊选项(-r=<pattern>)所以它是一个长选项,因为它需要参数,但它必须有一个破折号(-)前缀而不是双破折号(--)其他多头选项。可以设置 Getopt::Long接受这些?

最佳答案

默认情况下,Getopt::Long可互换地接受单破折号 (-) 或双破折号 (--)。所以,你可以只使用 --r=foo。尝试时会出现任何错误吗?

use strict;
use warnings;
use Getopt::Long;
my $input = 2;
my $s = 0;
my $r = 3;
GetOptions(
    'input=s' => \$input,
    's'       => \$s,
    'r=s'     => \$r,
);
print "input=$input\n";
print "s=$s\n";
print "r=$r\n";

这些示例命令行产生相同的结果:

my_program.pl --r=5
my_program.pl --r 5
my_program.pl  -r=5
my_program.pl  -r 5

input=2
s=0
r=5

关于perl - 我如何使用 Perl 的 Getopt::Long 处理 -r=<pattern>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2455555/

相关文章:

Perl Mojolicious 渲染大文本文件

regex - s/\G/0/g 是什么意思?

perl - 如何检查我的 Perl 安装是 32 位还是 64 位?

perl - 在散列中添加 Getopt::Long 选项,即使使用重复说明符

C++ boost::program_options 读取与 getopt_long 兼容的参数

linux - 在 Bash 中使用基于 Perl 的重命名命令和查找

mysql - Perl DBI 的 'SELECT COUNT(*)' 返回什么?

perl - 使用Getopt解析args时如何允许未定义的选项

c - 长选项作为 getopt_long 的静态变量

linux - getopt_long 没有按预期工作