Getopt-Long

 view release on metacpan or  search on metacpan

lib/Getopt/Long/Parser.pm  view on Meta::CPAN

use warnings;

package Getopt::Long::Parser;

# Must match Getopt::Long::VERSION!
our $VERSION = 2.58;

=head1 NAME

Getopt::Long::Parser - Getopt::Long object-oriented interface

=head1 SYNOPSIS

    use Getopt::Long::Parser;
    my $p = Getopt::Long::Parser->new;
    $p->configure( %options );
    if ( $p->getoptions( @options ) ) { ... }
    if ( $p->getoptionsfromarray( \@array, @options ) ) { ... }

Configuration options can be passed to the constructor:

    my $p = Getopt::Long::Parser->new( config => [ %options ] );

=head1 DESCRIPTION

C<Getopt::Long::Parser> is an object-oriented interface to
L<Getopt::Long>. See its documentation for configuration and use.

Note that C<Getopt::Long> and C<Getopt::Long::Parser> are not
object-oriented.
C<Getopt::Long::Parser> emulates an object-oriented interface,
which should be okay for most purposes.

=head1 CONSTRUCTOR

    my $p = Getopt::Long::Parser->new( %options );

The constructor takes an optional hash with parameters.

=over 4

=item config

An array reference with configuration settings.
See L<Getopt::Long/"Configuring Getopt::Long"> for all possible settings.

=back

=cut

# Getopt::Long has a stub for Getopt::Long::Parser::new.
use Getopt::Long ();
no warnings 'redefine';

sub new {
    my $that = shift;
    my $class = ref($that) || $that;
    my %atts = @_;

    # Register the callers package.
    my $self = { caller_pkg => (caller)[0] };

    bless ($self, $class);

    my $default_config = Getopt::Long::_default_config();

    # Process config attributes.
    if ( defined $atts{config} ) {
	my $save = Getopt::Long::Configure ($default_config, @{$atts{config}});
	$self->{settings} = Getopt::Long::Configure ($save);
	delete ($atts{config});
    }
    # Else use default config.
    else {
	$self->{settings} = $default_config;
    }

    if ( %atts ) {		# Oops
	die(__PACKAGE__.": unhandled attributes: ".
	    join(" ", sort(keys(%atts)))."\n");
    }

    $self;
}

use warnings 'redefine';

=head1 METHODS

In the examples, C<$p> is assumed to be the result of a call to the constructor.

=head2 configure

    $p->configure( %settings );

Update the current config settings.
See L<Getopt::Long/"Configuring Getopt::Long"> for all possible settings.

=cut

sub configure {
    my ($self) = shift;

    # Restore settings, merge new settings in.
    my $save = Getopt::Long::Configure ($self->{settings}, @_);

    # Restore orig config and save the new config.
    $self->{settings} = Getopt::Long::Configure ($save);
}

=head2 getoptionsfromarray

    my $res = $p->getoptionsfromarray( $aref, @opts );

=head2 getoptions

    my $res = $p->getoptions( @opts );

The same as C<getoptionsfromarray( \@ARGV, @opts )>.

=cut



( run in 1.268 second using v1.01-cache-2.11-cpan-e1769b4cff6 )