我希望我的脚本在使用 --help
命令行选项运行时打印帮助消息。基于 Getopt::Std
documentation ,这个 sub 应该可以解决问题:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;
use Getopt::Std;
sub HELP_MESSAGE {
say "HELP MESSAGE";
}
但它什么也不打印。出于好奇,我也尝试添加此内容:
for (@ARGV) {
HELP_MESSAGE() if /--help/;
}
它实际上有效,但似乎相当草率。我知道使用
-h
标志会很简单,但我想同时拥有。
最佳答案
documentation of Getopt::Std 说
If
-
is not a recognized switch letter,getopts()
supports arguments--help
and--version
. Ifmain::HELP_MESSAGE()
and/ormain::VERSION_MESSAGE()
are defined, they are called; ...
所以试试这个:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $VERSION = 0.1;
getopts(''); # <<< You forgot this line, and `getopt()` DOES NOT work
sub HELP_MESSAGE {
say "HELP MESSAGE";
}
测试运行:
$ ./t00.pl --help
./t00.pl version 0.1 calling Getopt::Std::getopts (version 1.07),
running under Perl version 5.16.3.
HELP MESSAGE
关于perl - 如何使用 Getopt::Std 响应 --help 标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956620/