【问题标题】:Pod::Usage not working correctlyPod::Usage 不能正常工作
【发布时间】:2014-04-01 16:26:01
【问题描述】:

我有:

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 时,什么也没有打印出来,并且脚本退出了。

【问题讨论】:

  • 如果切换回使用合法的 POD 会怎样?
  • 对不起,我显然对 perl 很陌生 - 你所说的合法​​ POD 是什么意思?
  • 您知道您所说的“几乎从在线示例复制/粘贴”的部分吗?嗯,看来你所做的每一个改变都是一个错误。
  • 尤其是缩进严重破坏了它。在 POD 中,缩进文本是逐字块,因此缩进的 =item 根本不是 POD item。您可以通过在您的文件上运行 pod2text 来验证这一点。
  • 谢谢,我不知道 perl 中的间距是这样的问题

标签: perl perl-pod


【解决方案1】:

如前所述,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

【讨论】:

    【解决方案2】:

    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 之后没有跳过一行。

    【讨论】:

    • 感谢您参与解释有关 POD 语法的更多信息。这是一个我不完全了解所有规则的领域,我只是通过适当使用 Cntl-C 和 Cntl-V 来遵循它们
    • POD 的规则非常简单。请参阅 PerlpodPerlpodspecPerlpodstyle。主要问题是这些文档都没有明确说明您需要在每个 命令 之间添加一个空行。就个人而言,我认为 POD 很棒。这很简单,但功能强大。但是,它现在应该被淘汰(或至少允许)Markdown,它同样强大,并且更容易以原始形式阅读。
    • Pod::Usage 有点 break POD 的工作方式,因为它需要 SYNOPSISOPTIONS 的特定部分名称,或 参数选项和参数。这适用于联机帮助页样式和英文的 POD 文档。但是,如果您将程序的工作方式置于名为 DESCRIPTION 或者可能是 USAGE 的设置下,那么所有事情都会变得松散。你可以将--verbose设置为99,并提供你想要的部分,但这不是很方便。
    猜你喜欢
    • 2013-03-30
    • 2010-11-30
    • 2021-10-02
    • 2017-08-06
    • 2013-11-22
    • 1970-01-01
    • 2013-09-10
    • 2017-04-05
    • 2013-07-25
    相关资源
    最近更新 更多