我快要疯了,所以我来了:)
我正在尝试为我的 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/