将数组传递给Perl中的getopt

问题描述 投票:0回答:1

我正在尝试在命令行中将数组传递给Perl。

我正在阅读https://perldoc.perl.org/Getopt/Long.html中的说明

我的脚本是

#!/usr/bin/env perl

use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use Getopt::Long 'GetOptions';

my @array;
GetOptions ("a=s@" => \@array);#'file' indicates string on command line, '=s' means it must be a string
if (defined $array[0]) {#http://perldoc.perl.org/Getopt/Long.html#Options-with-values
    my @z = split(/,/,join(',',@array));
    say 'The array is ' . join (', ', @z);
}

使用命令行输出的>]

con@VB:~/Scripts$ perl array_getopt.pl -a v c d
The array is v

这是不正确的,因为它错过了cd

我也尝试过GetOptions ("a=s" => \@array);具有相同的错误。

[我在该页面上看到一些内容,说我必须一遍又一遍地重复相同的标签,如最终用户不喜欢的perl array_getopt.pl -a v -a c -a d

如何将信息传递给命令行,以便-a v c d传递给数组?

我正在尝试在命令行将数组传递给Perl。我正在从https://perldoc.perl.org/Getopt/Long.html阅读说明,我的脚本是#!/ usr / bin / env perl use strict;使用警告...

perl getopt-long
1个回答
1
投票

使用s{,}而不是s@perldoc Getopt::LongOptions with multiple values段中描述了此选项:

© www.soinside.com 2019 - 2024. All rights reserved.