How to get Getopt::Long + pod2usage working?

2019-07-16 03:49发布

问题:

I'm about to go nuts so here I am :)

I'm trying to make documentation for my Perl program, but I never manage to get Getopt::Long and pod2man working.

Here is a simple program I wrote for testing purpose:

#!/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

Unfortunately, when I do this :

./my-prog.pl --help

Nothing appears. Worse, when I do this :

./my-prog.pl --man

I get kind of a curses page with my whole program in it (every line of my program, not just the help manual).

Before I go crazy and just go back with a custom print_help() subroutine, could you please help me?

Thanks in advance :)

EDIT 1: Program modified thanks to @toolic. Now, my --help works fine but --man is showing me the whole source code of my program in a "man" like page. I run this script with Perl 5.14.2 on Debian Wheezy.

回答1:

You need to change your POD. Add blank lines and only use indentation for verbatim paragraphs. Refer to perldoc perlpod. My editor does a nice job of syntax highlighting of the POD to make mistakes more visibly obvious.

=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


回答2:

On several my computers (Debian 9) it depends of perldoc presence. I've tried to see full manual made of my script but got source code plus small notice after I returned to command line:

$ ./get-structure.pl --man
You need to install the perl-doc package to use this program.

I've installed perldoc and now I see what I need:

$ perldoc ./get-structure.pl
You need to install the perl-doc package to use this program.
$ sudo apt install perl-doc
...
$ ./get-structure.pl --man
SYNOPSIS
    ./structure-to-csv.pl [options]

OPTIONS
    --departments
        Specify output file for departments.
...