Getopt-Helpful
view release on metacpan or search on metacpan
lib/Getopt/Helpful.pm view on Meta::CPAN
$self->{table} = [@rows];
return($self);
} # end subroutine new definition
########################################################################
=head1 DWIM
Do What I Mean methods.
=head2 Get
Calls GetOptions() with the options builtin to $helper (and optionally a
list of other helpers or a hash of plain-old options.)
If GetOptions() returns false, we die (hinting at how to get help if
'+help' was one of the options given to the constructor.)
$helper->Get();
# multiple helper objects:
$helper->Get(@other_helpers);
# mixed method (hash ref must come first)
$helper->Get(\%opts, @other_helpers);
=cut
sub Get {
my $self = shift;
my @others = @_;
#package main; # XXX what was the point of that?
require Getopt::Long;
my %opts = $self->opts();
# XXX this area needs some test coverage, but is still waiting on
# Getopt::Crazy to get done.
foreach my $obj (@others) {
eval {$obj->can('opts');};
if(! $@) {
# if it can('opts'), just do that
%opts = (%opts, $obj->opts);
}
else {
# it had better be a hash or it is useless here
(ref($obj) eq ("HASH")) or croak("non-helpful object");
%opts = (%opts, %$obj);
}
}
# XXX should we save @ARGV? (probably so...)
# (it is required if we're going to make help behave properly,
# and for config-file processing.)
@ARGV = $self->juggle_list(\@ARGV, \%opts);
# XXX why bother with Getopt::Long at this point?
unless(Getopt::Long::GetOptions(%opts)) {
# does -h work?
my $message = ($self->{has}{help} ? " (-h for help)" : "");
die "invalid options$message\n";
}
} # end subroutine Get definition
########################################################################
=head2 Get_from
Equivalent to Get(@extra), but treats @args as a localized @ARGV.
$hopt->Get_from(\@args, @extra);
=cut
sub Get_from {
my $self = shift;
my ($args, @extra) = @_;
local @ARGV = @$args;
$self->Get(@extra);
@$args = @ARGV;
} # end subroutine Get_from definition
########################################################################
=head2 ordered
Not finished yet. The idea is to have one or more arguments that may be
just an ordered list on the command line so that your program could be
called as:
program infile outfile --option "these are options"
or
program -o outfile -i infile --option "these are options"
Still working on what this should look like, whether it should die if
these things are unset, where it should set them (in the references?),
how to set default values, etc...
$helper->ordered('first_option+r7qe', 'second_option');
=cut
sub ordered {
my $self = shift;
die "not finished";
} # end subroutine ordered definition
########################################################################
=head1 Methods
=head2 opts
Makes a hash of the first two columns of the table. Better to use Get()
instead.
GetOptions(
$helper->opts()
) or die "invalid arguments (-h for help)\n";
=cut
sub opts {
my $self = shift;
my @other;
if($self->{conf_table}) {
@other = @{$self->{conf_table}};
}
my %hash = map({$_->[0] => $_->[1]} @{$self->{table}}, @other);
# print "hash has keys: ", join(", ", keys(%hash)), "\n";
return(%hash);
} # end subroutine opts definition
########################################################################
=head2 help_table
Returns a list of array refs of the first, third, and fourth columns of
the table (e.g. you don't need the variable refs for this.)
my @table = $helper->help_table();
( run in 0.786 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )