I've made some changes to this since we had last discussed it.  The 
new methods allow you to do without a main::usage() subroutine, and 
provide some DWIM functionality.  Sorry, it is not on CPAN yet.

Any feedback on the interface/usability would be appreciated.  Below 
is the synopsis, the code is available at:
  http://ericwilhelm.homeip.net/svn/Getopt-Helpful/trunk/code/Getopt/Helpful.pm

=head1 SYNOPSIS

This module provides methods which integrate help messages into a
Getopt::Long option spec.  This gathers documentation and declaration
into one place and allows you to utilize perl code to state the 
default values of options in your online help messages (helping you 
utilize the single-point-of-truth principle.)

Additionally, it provides DWIM methods (Get) which allow you to cut 
some standard error-checking code from your scripts.  There is even a 
handy usage() method which eliminates that silly block of code from 
the beginning.

  #!/usr/bin/perl

  use warnings;
  use strict;

  use Getopt::Helpful;
  my $var1 = "default";
  my $req_arg;

  # declare this as 'our' or $main::verbose
  our $verbose = 0;

  # every option must be passed into the constructor...
  my $hopt = Getopt::Helpful->new(
    usage => 'CALLER <argument> [options]',
    [
      'var=s', \$default,
      '<setting>',
      "setting for \$var1 (default: '$var1')"
    ],
    [
      'a|arg, \$req_arg,
      '<argument>',
      'required argument'
    ],
    '+verbose',
    '+help',
    );

  # call GetOptions() behind the scenes (with error-checking)
  $hopt->Get();
  $req_arg or ($req_arg = shift);

  # usage() called with a message results in non-zero exit code
  $req_arg or $hopt->usage('missing required argument');
  $verbose and warn "doing stuff now\n";
  # now do stuff...

=cut

Thanks,
Eric

Reply via email to