1

我有:

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,脚本就会退出。

4

2 回答 2

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
于 2014-04-01T18:31:31.180 回答
1

米勒给出了答案。

当您创建 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.

现在,您没有名为NAMEor的标头DESCRIPTION,因此您的 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

于 2014-04-01T20:40:03.703 回答