Pod ::用法无法正常工作

时间:2014-04-01 16:26:02

标签: perl perl-pod

我有:

my $man = 0;
my $help = 0;
## Parse options and print usage if there is a syntax error,
## or if usage was explicitly requested.
GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;

#-----------------------------------------------------------------
#----------------  Documentation / Usage / Help ------------------
=head1 NAME
  sample - Using GetOpt::Long and Pod::Usage

=head1 SYNOPSIS
  sample [options] [file ...]
   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.
  =back

=head1 DESCRIPTION
  B<This program> will read the given input file(s) and do something
  useful with the contents thereof.
=cut

从在线示例中复制/粘贴相当多。但是,当我执行script.pl --help打印时,脚本将退出。

2 个答案:

答案 0 :(得分:2)

如前所述,pod文档的间距很重要。此外,没有必要在概要中复制您的选项,而只是将它们留在选项部分。

以下是Pod::Usage

的试用版的清理版
use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);

## Parse options and print usage if there is a syntax error,
## or if usage was explicitly requested.

GetOptions(
    'help|?'    => \my $help,
    'man'       => \my $man,
) or pod2usage(-verbose => 0);
pod2usage(-verbose => 1) if $help;
pod2usage(-verbose => 2) if $man;

## Check for File
pod2usage("$0: No filename specified.\n") unless @ARGV;

#-----------------------------------------------------------------
#----------------  Documentation / Usage / Help ------------------
=head1 NAME

sample - Using GetOpt::Long and Pod::Usage

=head1 SYNOPSIS

sample [file]

=head1 OPTIONS

=over 8

=item B<--help>  

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B<This program> will read the given input file(s) and do something
useful with the contents thereof.

=cut

答案 1 :(得分:1)

Miller有答案。

创建POD文档时,每个段落之间需要一个空行,包括命令段落。 POD文档没有说清楚。此外,所有POD命令段落必须从第一列开始。例如,这是错误的:

=head1 NAME
foo.cmd
=head1 DESCRIPTION
This is my description of my command.
....

我需要解决这个问题:

=head1 NAME

foo.cmd

=head1 DESCRIPTION

This is my description.

否则,POD会这样解释:

=head1 NAME foo.cmd =head1 DESCRIPTION This is my description of my command.

现在,您没有名为NAMEDESCRIPTION的标题,因此您的pod2usage不会打印。

我还需要在第1列开始我的命令。这不会起作用:

=over 4

    =item *
        blah, blah, blah...

我需要这样做:

=over 4

=item *

blah, blah, blah

=back

您可以运行podchecker程序来检查您的pod并确保一切正确。它应与perl命令位于同一目录中。我将您的pod文档放入test.pod并运行此命令:

$ podchecker test.pod
*** WARNING: empty section in previous paragraph at line 4 in file test.pod
*** WARNING: empty section in previous paragraph at line 10 in file test.pod
*** ERROR: Apparent command =cut not preceded by blank line at line 21 in file test.pod
*** WARNING: empty section in previous paragraph at line 18 in file test.pod
test.pod has 1 pod syntax error.

empty section in previous paragraph警告来自于=head1之后没有跳过一行。