【问题标题】:Right way running Getopt::Long::GetOptions in a subroutine在子例程中运行 Getopt::Long::GetOptions 的正确方法
【发布时间】:2021-04-16 21:51:13
【问题描述】:

您好,我正在使用 Debian Linux 和 Perl 5.28 并尝试在子例程(类的方法)中运行。

调用部分由工具包对象$TK产生:

# Usage sugar: help, man and version for the CLI
my $IS_MAN;  # Flag show man page
my $IS_HELP; # Flag show help page
my $IS_VER;  # Flag show version

# Variables for the CLI
my $OUTPUT;    # IPC Variable to manage an operation status
my $RESPONSE;  # IPC Variable to state a response type INFO, WARN, ERROR...
my $COMMAND;   # Current command to address a routine in this tool
my $BCK_NAME;  # Name to manage the backup session under an ID
my $ARC_SIZE;  # Size of the expected archive size
my $TAPE_SIZE; # Size of the expected tape volume
my $WORK_PATH; # Work path canonical usually given by $ENV{TOOL_BACKUP}

# Read the param's into vars
$TK->parseCommandline(
    {            
    "command|c=s"      => \$COMMAND,
    "help|h"           => \$IS_HELP,
    "man|m"            => \$IS_MAN,
    "version|v"        => \$IS_VER,
    "output|o=s"       => \$OUTPUT,
    "response|r=s"     => \$RESPONSE,
    "backup-name|B=s"  => \$BCK_NAME,
    'tape-size|T=s'    => \$TAPE_SIZE,
    'archive-size|A=s' => \$ARC_SIZE,
    'work-path|W=s'    => \$WORK_PATH,
   } 
);

$TK 类应运行命令行解析器(方法),并带有明确定义的错误退出程序和规范输出。错误捕捉效果很好。但是我无法通过有效的开关获取 CLI 参数。

sub parseCommandline($$) {
    my ($self, $opt) = @_;

    # Catch errors thrown by GetOptions
    my $sigFun = $SIG{__WARN__};
    my @errs =();
    $SIG{__WARN__} = sub {
        my $msg = shift;
        chomp($msg);
        push( @errs, $msg );
     };

    # Dump the predefined switches
    print Dumper($opt);

    # Get the options
    GetOptions( $opt)
        or exitFatalConfig($self,
            join( ";\n", @errs ) . "!");

    # Restore the warnings
    $SIG{__WARN__} = $sigFun;

    # Go into init-runtime mode
    $self->runtime->{+KEY_COMMAND} = 'init-runtime';
}
  

尽管启用了选项 -o,但该方法始终标记错误。

IO.MAGIC:  TEST.TOOL.V1.2
IO.COMMAND:  ./Test-App -o TEST.VARIABLE
$VAR1 = {
          'tape-size|T=s' => \undef,
          'backup-name|B=s' => \undef,
          'output|o=s' => \undef,   <------ HERE 
          'work-path|W=s' => \undef,
          'response|r=s' => \undef,
          'help|h' => \undef,
          'man|m' => \undef,
          'version|v' => \undef,
          'command|c=s' => \undef,
          'archive-size|A=s' => \undef
        };
EOF.TEST.TOOL.V1.2

STATUS: FAILED
  PROGRAM:  ./Test-App
  MESSAGE:  Unknown option: o!
EOF.STATUS

这段代码有什么问题?

【问题讨论】:

    标签: perl subroutine getopt-long


    【解决方案1】:
    GetOptions( $opt)
    

    应该是

    GetOptions(%$opt)
    

    【讨论】:

    • 谢谢,它的工作原理,如此简单。主题是否包含在 POD 中?我只在 'Parsing options from an random array' 中发现了一些在参数转换方面提到 $ret = GetOptions(\%opts, ... ); 的内容,作为对 $opts 本质的暗示。
    • 关于“POD 中是否涵盖了该主题?”,文档中到处都是。关于您在文档中看到的第一件事是GetOptions("length=i" =&gt; \$length, "file=s" =&gt; \$data, "verbose" =&gt; \$verbose)。但这不是你所传递的。您没有传递这样的东西,而是传递了一个标量(对哈希的引用)。
    • 通过将哈希引用作为第一个(也是唯一的)参数传递,您使用的是“将选项值存储在哈希中”(GetOptions(\%h, 'length=i', "file=s", "verbose"))中记录的方法,除非您使用哈希引用选项的定义为零。这就是它无法识别 -o 的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多