Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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):

  1. I can't make it so that Compile the sourcefiles comes on the line after pcomp.pl [options].
  2. I can't make it so that everything isn't indented.
  3. I can't make it so that options isn't indented more than Compile the sourcefiles. If I remove the indent then the following lines have to be separated by a space.
  4. I can't make it so that it doesn't output a newline at the end of the entire thing.
  5. I really have to hand list the options twice? I can't put the help text with the options?
  6. I really have to have all the newlines between the help text?
  7. 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.

share|improve this question

closed as off topic by Jeff Vanzella, palacsint, Brian Reichle, Bobson, svick Jan 17 at 15:43

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

There are plenty of 'getopt' modules on CPAN.

Try looking at Getopt::Long::Descriptive. It may resolve some issues, but it may raise others.

use Getopt::Long::Descriptive;
my ($opt, $usage) = describe_options(
    "pcomp.pl [options]\nCompile the sourcefiles.\n",
    ['help|h',    "Show this help message and exit"],
    ['dry_run|n', "Don't actually execute commands, just print them"],
);
print($usage->text), exit if $opt->help;

Perhaps also check out MooX::Options or MooseX::Getopt, which build on Getopt::Long::Descriptive and give you syntactical sugar that allows you to code the app like an OO class with the options declared like class attributes. Note that the module dependency is much larger, and perhaps a slower startup time, particularly with MooseX::Getopt.

share|improve this answer
Those look potentially helpful. Sadly those packages are not installed on my site. After a couple hours of trying to install them to my local library I give up on libraries that don't come bundled with version 5.8.8. – Scott Jan 17 at 16:15
This presentation mentions that their are at least 63 Getopt modules: slideshare.net/nickpatch/… – Scott Jan 17 at 16:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.