I want to write a Perl script that parses GNU style long and style command-line options. I also want to provide a nice --help output.
I get the impressions that I should use Getopt::Long
, which itself recommends I use Pod::Usage
to provide my help text.
Here is the code I came up with:
#!/usr/bin/perl
use Getopt::Long;
use Pod::Usage;
Getopt::Long::Configure qw(gnu_getopt auto_version auto_help);
my %args = ();
GetOptions(\%args,
"help|h",
"dry_run|n") or pod2uase(2);
pod2usage(1) if $args{"help"};
__END__
=head1 NAME
pcompl.pl - Compile sourcefiles
=head1 SYNOPSIS
pcomp.pl [options]
Compile the sourcefiles.
options:
-h, --help Show this help message and exit
-n, --dry_run Don't actually execute commands, just print them
=cut
Here is the output I get:
%: ./pcomp.pl --help
Usage:
pcomp.pl [options]
Compile the sourcefiles.
options:
-h, --help show this help message and exit
-n, --dry_run Don't actually execute commands, just print them
<newline>
My frustrations with the output (1-4) and the code (5-7):
- I can't make it so that
Compile the sourcefiles
comes on the line afterpcomp.pl [options]
. - I can't make it so that everything isn't indented.
- I can't make it so that
options
isn't indented more thanCompile the sourcefiles
. If I remove the indent then the following lines have to be separated by a space. - I can't make it so that it doesn't output a newline at the end of the entire thing.
- I really have to hand list the options twice? I can't put the help text with the options?
- I really have to have all the newlines between the help text?
- I would prefer an object-oriented syntax. The
Getopt::Long
documentation did not provide a full example.
I'm not attached to Getopt::Long
or Pod::Usage
. I just want nice code and output.