perl - 如何让 Getopt::Long + pod2usage 工作?

标签 perl getopt manual

我快要疯了,所以我来了:)

我正在尝试为我的 Perl 程序制作文档,但我从未设法让 Getopt::Long 和 pod2man 工作。

这是我为测试目的编写的一个简单程序:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;

Getopt::Long::Configure ("bundling");

my $option = "";
my $verbose = 1;
my $quiet = 0;
my $man = 0;
my $help = 0;

GetOptions (
        "option=s" => \$option,
        "verbose" => sub { $verbose = 1; $quiet = 0 },
        "quiet|noverbose" => sub { $verbose = 0; $quiet = 1 },
        "help|?" => \$help,
        man => \$man
) or pod2usage("Error in command line.\n");

pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;

print "my programm here\n";

__END__
=head1 NAME

my-prog.pl - Customized mysqldump utility

=head1 SYNOPSIS

        my-prog.pl [OPTIONS]
        Options:
                -help brief help message
                -man full documentation

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--option>

Option description

=item B<--verbose>

Option description

=back

=head1 DESCRIPTION

B<my-prog.pl> is doing something.

=head1 AUTHOR

B<Me>

=cut

不幸的是,当我这样做时:

./my-prog.pl --help

什么都没有出现。更糟糕的是,当我这样做时:

./my-prog.pl --man

我得到了一个 curses 页面,其中包含我的整个程序(我程序的每一行,而不仅仅是帮助手册)。

在我发疯并返回自定义 print_help() 子例程之前,您能帮帮我吗?

提前致谢:)

编辑 1:感谢@toolic 修改了程序。现在,我的 --help 工作正常,但是 --man 在一个类似“man”的页面中向我展示了我程序的全部源代码。我在 Debian Wheezy 上使用 Perl 5.14.2 运行此脚本。

最佳答案

您需要更改 POD。添加空行并仅对逐字 段落使用缩进。引用perldoc perlpod .我的编辑器很好地突出了 POD 的语法,使错误更加明显。

=head1 NAME

my-prog.pl - Customized mysqldump utility

=head1 SYNOPSIS

        my-prog.pl [OPTIONS]
        Options:
                -help brief help message
                -man full documentation

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--option>

Option description

=item B<--verbose>

Option description

=back

=head1 DESCRIPTION

B<my-prog.pl> is doing something.

=head1 AUTHOR

B<Me>

=cut

关于perl - 如何让 Getopt::Long + pod2usage 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104215/

相关文章:

c - 当有错误的命令行参数时,如何使 getopt_long() 不打印任何内容?

c - Getopt- 为参数传递字符串参数

perl - 将带有 add_to_$rel 的对象插入到与 DBIx::Class 的多对多关系中

c - 如何在 C 编程 getopt_long 中将转义序列(如制表符和换行符)作为命令行参数传递?

mysql - 模板工具包显示唯一行

kernel - 在哪里可以找到 perf 事件文档

sql - ANSI SQL 手册

regex - 使用 Perl 的 RegEx 查找集合的任何排列

perl - 如何在 Perl 中使用超时读取实现 'tail -f'?