【问题标题】:Getopt::Lazy does not print usage message or anything elseGetopt::Lazy 不打印使用消息或其他任何内容
【发布时间】:2015-10-09 12:37:16
【问题描述】:

我在使用 perls Getopt::Lazy 模块时需要一些帮助。我尝试了 cpan 页面中的示例:

#!/usr/bin/perl
#
#use warnings;
#use strict;
use Getopt::Lazy
        'help|h' => 'Show this help screen',
        'verbose|v' => 'Show verbose output',
        'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
        'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
        -summary => 'a simple example usage of Getopt::Lazy',
        -usage => '%c %o file1 [file2 ..]',
        ;
getopt;
print usage and exit 1 unless @ARGV;

当我将它放入一个文件并像 ./mygetopt.pl -h 一样执行它时,我希望打印帮助消息,但没有任何反应。当我在没有 -h 参数的情况下调用它时,我希望它会打印使用消息。不会发生这样的事情。此外,当我使用严格和警告时,我会收到类似

的消息
Unquoted string "usage" may clash with future reserved word at ./mygetopt.pl line 14.
Bareword "getopt" not allowed while "strict subs" in use at ./mygetopt.pl line 13.
Execution of ./mygetopt.pl aborted due to compilation errors.

我做错了什么?

【问题讨论】:

标签: perl getopt-long


【解决方案1】:

该模块的SYNOPSIS 与实际代码完全不一致。代码中没有称为getoptusage 的函数。它们实际上被称为GetOptionsGetopt::Lazy::show_help(是的,一个被导出,另一个不是 - 谁知道为什么)。像这样重写示例将起作用:

#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Lazy
        'help|h' => 'Show this help screen',
        'verbose|v' => 'Show verbose output',
        'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
        'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
        -summary => 'a simple example usage of Getopt::Lazy',
        -usage => '%c %o file1 [file2 ..]',
        ;
GetOptions;
Getopt::Lazy::show_help and exit 1 unless @ARGV;

但考虑到文档的质量以及该模块显然在七年半前被其作者抛弃,我不会让它靠近我的任何项目。建议您使用上述 cmets 中其他人建议的解决方案之一。

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2020-11-23
    • 1970-01-01
    • 2019-05-26
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多