【问题标题】:Unknown Option when using Getopt::Long Perl使用 Getopt::Long Perl 时的未知选项
【发布时间】:2016-08-24 23:29:51
【问题描述】:

我有一个 perl 文件,它基本上接受如下输入:

use strict;
use File::Copy;
use File::Path;
use Sys::Hostname;
use Getopt::Long;
use File::Spec;
use File::Compare;
use MIME::Base64;
use Digest::MD5 qw(md5_hex);
my %hmpParams;
my $keybindings;
my $keybindings1;
my $keybindings2;
my $filename;
my $osname;
my $copy="cp";

&parseCommandLineParams();

while ((my $k, my $v) = each %hmpParams ){
        print($k . " = " . $v . "\n");
}


sub parseCommandLineParams(){
GetOptions ( 'bindings:s' => \$hmpParams{bindings},
             'filename:s' => \$hmpParams{filename},
             'key:s' => \$hmpParams{key},
             'bindings1:s' => \$hmpParams{bindings1},
             'bindings2:s' => \$hmpParams{bindings2}
           );
}

我正在执行这个 Perl 文件如下:

perl test.pl "-bindings" "+WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=" "-filename" "/tmp/keyMapperFile_2016-08-24-04:43:06" "-key" "avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd" "-bindings1" "" "-bindings2" ""

输出:

Unknown option: wbaobtud/uum7uucg2t+0zvoceuu/x24ovykwji2ym
bindings2 = 
filename = /tmp/keyMapperFile_2016-08-24-04:43:06
bindings = 
bindings1 = 
key = avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd

我看到,只要选项的值前面有 + 号,它就不会将其识别为值,而是将其视为选项。如何解决此错误?这背后的原因是什么?

【问题讨论】:

  • 从参数名称中删除双引号。喜欢perl test.pl --bindings "+WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=" --filename ...
  • @ChankeyPathak 将无效。 (它们将在传递给脚本之前被 shell 删除)。
  • 啊,我明白了。谢谢!

标签: perl


【解决方案1】:

您需要禁用getopt_compat 选项以防止前导+ 被视为等同于--

getopt_compat
            Allow "+" to start options. Default is enabled unless
            environment variable POSIXLY_CORRECT has been set, in which
            case "getopt_compat" is disabled.

use Getopt::Long qw(:config no_getopt_compat);

use Getopt::Long;
Getopt::Long::Configure("no_getopt_compat");

【讨论】:

  • 或者作为选项注释状态,您可以调用命令并通过环境变量强制执行 POSIXLY_CORRECT:POSIXLY_CORRECT=1 perl test.pl "-bindings" "+WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=" ....
【解决方案2】:

潜在的问题是,当您打算使用 =s 时,您使用了 :s。既然您说省略--bindings 的参数是完全有效的,并且由于+ 可以是一个有效的选项开始,它认为您的意思是使用不带参数的--bindings

修复:

sub parseCommandLineParams {
    GetOptions(
        'bindings=s'  => \$hmpParams{bindings},
        'bindings1=s' => \$hmpParams{bindings1},
        'bindings2=s' => \$hmpParams{bindings2},
        'filename=s'  => \$hmpParams{filename},
        'key=s'       => \$hmpParams{key},
    )
        or die("usage\n");
}

sub parseCommandLineParams {
    GetOptions(
        \%hmpParams,
        'bindings=s',
        'bindings1=s',
        'bindings2=s',
        'filename=s',
        'key=s',
    )
        or die("usage\n");
}

顺便说一句,我还在 POSIX 兼容模式下使用 Getopt::Long,方法如下:

use Getopt::Long qw( :config posix_default );

在这种模式下,+ 不被视为选项的开始。

perl test.pl                                                      \
    --bindings +WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=       \
    --filename /tmp/keyMapperFile_2016-08-24-04:43:06             \
    --key avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd \
    --bindings1 ''                                                \
    --bindings2 ''

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多