2014-04-01 55 views
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沒有打印,腳本退出。

+2

如果您切換回使用合法的POD,該怎麼辦? – ikegami

+0

我很抱歉,我對perl顯然很陌生 - 你是什麼意思的合法POD? – MrDuk

+0

你知道你說過的部分「幾乎從在線示例複製/粘貼」?那麼,你所做的每一個改變似乎都是一個錯誤。 – ikegami

回答

2

如前所述,吊艙文件的間距很重要。此外,沒有必要在摘要中複製選項,而只需將它們留在選項部分。

以下是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

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程序檢查莢並確保一切正確。這應該與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後不跳過線。

+0

感謝您解釋更多關於POD語法的內容。這是我不完全瞭解所有規則的一個領域,我只是通過適當使用Cntl-C&Cntl-V來遵循它們。 – Miller

+1

POD的規則非常簡單。請參閱[Perlpod](http://perldoc.perl。org/perlpod.html),[Perlpodspec](http://perldoc.perl.org/perlpodspec.html)和[Perlpodstyle](http://perldoc.perl.org/perlpodstyle.html)。主要的問題是,這些文檔都沒有清楚地表明你需要在每個_commands_之間有一個空行。我個人認爲POD很棒。這很簡單,但功能強大。然而,它現在應該退休了(或者至少允許)Markdown,它同樣強大,並且更易於以原始形式閱讀。 –

+1

[Pod :: Usage](http://perldoc.perl.org/Pod/Usage.html)類似_breaks_ POD的工作方式,因爲它需要_SYNOPSIS_和_OPTIONS_或_ARGUMENTS_或_OPTIONS AND ARGUMENTS_的特定部分名稱。這適用於manpage風格和_in English_中的POD文檔。但是,如果你把程序的工作方式稱爲_DESCRIPTION_或者_USAGE_,那麼所有的問題都會打破。您可以將'--verbose'設置爲'99',並提供您想要的部分,但這不是很方便。 –