| 1 | #! perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # Parser.pm -- Getopt::Long object-oriented interface | ||||||||||||||||||||
| 4 | # Author : Johan Vromans | ||||||||||||||||||||
| 5 | # Created On : Thu Nov 9 10:37:00 2023 | ||||||||||||||||||||
| 6 | # Last Modified On: Tue Jun 11 13:17:57 2024 | ||||||||||||||||||||
| 7 | # Update Count : 16 | ||||||||||||||||||||
| 8 | # Status : Released | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package Getopt::Long::Parser; | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # Must match Getopt::Long::VERSION! | ||||||||||||||||||||
| 16 | 3 | our $VERSION = 2.58; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Getopt::Long::Parser - Getopt::Long object-oriented interface | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | use Getopt::Long::Parser; | ||||||||||||||||||||
| 25 | my $p = Getopt::Long::Parser->new; | ||||||||||||||||||||
| 26 | $p->configure( %options ); | ||||||||||||||||||||
| 27 | if ( $p->getoptions( @options ) ) { ... } | ||||||||||||||||||||
| 28 | if ( $p->getoptionsfromarray( \@array, @options ) ) { ... } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Configuration options can be passed to the constructor: | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | my $p = Getopt::Long::Parser->new( config => [ %options ] ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | C<Getopt::Long::Parser> is an object-oriented interface to | ||||||||||||||||||||
| 37 | L<Getopt::Long>. See its documentation for configuration and use. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | Note that C<Getopt::Long> and C<Getopt::Long::Parser> are not | ||||||||||||||||||||
| 40 | object-oriented. | ||||||||||||||||||||
| 41 | C<Getopt::Long::Parser> emulates an object-oriented interface, | ||||||||||||||||||||
| 42 | which should be okay for most purposes. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | my $p = Getopt::Long::Parser->new( %options ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | The constructor takes an optional hash with parameters. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =item config | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | An array reference with configuration settings. | ||||||||||||||||||||
| 55 | See L<Getopt::Long/"Configuring Getopt::Long"> for all possible settings. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =back | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # Getopt::Long has a stub for Getopt::Long::Parser::new. | ||||||||||||||||||||
| 62 | 3 | ▶ | use Getopt::Long (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||
| 66 | 3 | my $that = shift; | |||||||||||||||||||
| 67 | 3 | ▶ | my $class = ref($that) || $that; | ||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 68 | 3 | my %atts = @_; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # Register the callers package. | ||||||||||||||||||||
| 71 | 3 | my $self = { caller_pkg => (caller)[0] }; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 3 | bless ($self, $class); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 3 | my $default_config = Getopt::Long::_default_config(); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Process config attributes. | ||||||||||||||||||||
| 78 | 3 | ▶ | if ( defined $atts{config} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 1 | my $save = Getopt::Long::Configure ($default_config, @{$atts{config}}); | |||||||||||||||||||
| 80 | 1 | $self->{settings} = Getopt::Long::Configure ($save); | |||||||||||||||||||
| 81 | 1 | delete ($atts{config}); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | # Else use default config. | ||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||
| 85 | 2 | $self->{settings} = $default_config; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 3 | ▶ | if ( %atts ) { # Oops | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | die(__PACKAGE__.": unhandled attributes: ". | |||||||||||||||||||
| 90 | join(" ", sort(keys(%atts)))."\n"); | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 3 | $self; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 3 | ▶ | use warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 METHODS | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | In the examples, C<$p> is assumed to be the result of a call to the constructor. | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =head2 configure | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | $p->configure( %settings ); | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | Update the current config settings. | ||||||||||||||||||||
| 107 | See L<Getopt::Long/"Configuring Getopt::Long"> for all possible settings. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub configure { | ||||||||||||||||||
|
Subroutine
configure: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 112 | 0 | my ($self) = shift; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # Restore settings, merge new settings in. | ||||||||||||||||||||
| 115 | 0 | my $save = Getopt::Long::Configure ($self->{settings}, @_); | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # Restore orig config and save the new config. | ||||||||||||||||||||
| 118 | 0 | $self->{settings} = Getopt::Long::Configure ($save); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head2 getoptionsfromarray | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | my $res = $p->getoptionsfromarray( $aref, @opts ); | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 getoptions | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | my $res = $p->getoptions( @opts ); | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | The same as C<getoptionsfromarray( \@ARGV, @opts )>. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 1 | ▶ | sub getoptions { | ||||||||||||||||||
|
Subroutine
getoptions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 134 | 1 | my ($self) = shift; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 1 | return $self->getoptionsfromarray(\@ARGV, @_); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 2 | ▶ | sub getoptionsfromarray { | ||||||||||||||||||
|
Subroutine
getoptionsfromarray: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 140 | 2 | my ($self) = shift; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # Restore config settings. | ||||||||||||||||||||
| 143 | 2 | my $save = Getopt::Long::Configure ($self->{settings}); | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # Call main routine. | ||||||||||||||||||||
| 146 | 2 | my $ret = 0; | |||||||||||||||||||
| 147 | 2 | $Getopt::Long::caller = $self->{caller_pkg}; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 2 | eval { | |||||||||||||||||||
| 150 | # Locally set exception handler to default, otherwise it will | ||||||||||||||||||||
| 151 | # be called implicitly here, and again explicitly when we try | ||||||||||||||||||||
| 152 | # to deliver the messages. | ||||||||||||||||||||
| 153 | 2 | local ($SIG{__DIE__}) = 'DEFAULT'; | |||||||||||||||||||
| 154 | 2 | $ret = Getopt::Long::GetOptionsFromArray (@_); | |||||||||||||||||||
| 155 | }; | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | # Restore saved settings. | ||||||||||||||||||||
| 158 | 2 | Getopt::Long::Configure ($save); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | # Handle errors and return value. | ||||||||||||||||||||
| 161 | 2 | ▶ | die ($@) if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 2 | return $ret; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 SEE ALSO | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | L<Getopt::Long> | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head1 AUTHOR | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Johan Vromans <jvromans@squirrel.nl> | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =head1 COPYRIGHT AND DISCLAIMER | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | This program is Copyright 1990,2015,2023 by Johan Vromans. | ||||||||||||||||||||
| 176 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||
| 177 | modify it under the same terms as Perl. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 3 | 1; | |||||||||||||||||||