3
3
# Getopt::Long.pm -- Universal options parsing
4
4
# Author : Johan Vromans
5
5
# Created On : Tue Sep 11 15:00:12 1990
6
- # Last Modified By: Johan Vromans
7
- # Last Modified On: Thu Nov 17 17:45:27 2022
8
- # Update Count : 1777
6
+ # Last Modified On: Sat Nov 11 17:48:41 2023
7
+ # Update Count : 1808
9
8
# Status : Released
10
9
11
10
# ############### Module Preamble ################
12
11
13
- # There are no CPAN testers for very old versions of Perl.
14
- # Getopt::Long is reported to run under 5.8.
15
- use 5.004;
12
+ # Getopt::Long is reported to run under 5.6.1. Thanks Tux!
13
+ use 5.006001;
16
14
17
15
use strict;
18
16
use warnings;
19
17
20
18
package Getopt::Long ;
21
19
22
- use vars qw( $VERSION) ;
23
- $VERSION = 2.54;
24
- # For testing versions only.
25
- use vars qw( $VERSION_STRING) ;
26
- $VERSION_STRING = " 2.54" ;
20
+ our $VERSION = 2.57;
27
21
28
22
use Exporter;
29
- use vars qw( @ISA @EXPORT @EXPORT_OK) ;
30
- @ISA = qw( Exporter) ;
23
+ use base qw( Exporter) ;
31
24
32
25
# Exported subroutines.
33
26
sub GetOptions (@); # always
@@ -37,21 +30,24 @@ sub Configure(@); # on demand
37
30
sub HelpMessage (@); # on demand
38
31
sub VersionMessage (@); # in demand
39
32
33
+ our @EXPORT ;
34
+ our @EXPORT_OK ;
35
+ # Values for $order. See GNU getopt.c for details.
36
+ our ($REQUIRE_ORDER , $PERMUTE , $RETURN_IN_ORDER );
40
37
BEGIN {
41
- # Init immediately so their contents can be used in the 'use vars' below.
38
+ ( $REQUIRE_ORDER , $PERMUTE , $RETURN_IN_ORDER ) = (0..2);
42
39
@EXPORT = qw( &GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER) ;
43
40
@EXPORT_OK = qw( &HelpMessage &VersionMessage &Configure
44
41
&GetOptionsFromArray &GetOptionsFromString) ;
45
42
}
46
43
47
44
# User visible variables.
48
- use vars @EXPORT , @EXPORT_OK ;
49
- use vars qw( $error $debug $major_version $minor_version) ;
45
+ our ($error , $debug , $major_version , $minor_version );
50
46
# Deprecated visible variables.
51
- use vars qw ( $autoabbrev $getopt_compat $ignorecase $bundling $order
52
- $passthrough) ;
47
+ our ($autoabbrev , $getopt_compat , $ignorecase , $bundling , $order ,
48
+ $passthrough );
53
49
# Official invisible variables.
54
- use vars qw ( $genprefix $caller $gnu_compat $auto_help $auto_version $longprefix) ;
50
+ our ($genprefix , $caller , $gnu_compat , $auto_help , $auto_version , $longprefix );
55
51
56
52
# Really invisible variables.
57
53
my $bundling_values ;
@@ -125,97 +121,29 @@ sub import {
125
121
126
122
# ############### Initialization ################
127
123
128
- # Values for $order. See GNU getopt.c for details.
129
- ($REQUIRE_ORDER , $PERMUTE , $RETURN_IN_ORDER ) = (0..2);
130
124
# Version major/minor numbers.
131
125
($major_version , $minor_version ) = $VERSION =~ / ^(\d +)\. (\d +)/ ;
132
126
133
127
ConfigDefaults();
134
128
135
- # ############### OO Interface ################
136
-
137
- package Getopt::Long::Parser ;
138
-
139
129
# Store a copy of the default configuration. Since ConfigDefaults has
140
130
# just been called, what we get from Configure is the default.
141
131
my $default_config = do {
142
132
Getopt::Long::Configure ()
143
133
};
144
134
145
- sub new {
146
- my $that = shift ;
147
- my $class = ref ($that ) || $that ;
148
- my %atts = @_ ;
149
-
150
- # Register the callers package.
151
- my $self = { caller_pkg => (caller )[0] };
152
-
153
- bless ($self , $class );
154
-
155
- # Process config attributes.
156
- if ( defined $atts {config } ) {
157
- my $save = Getopt::Long::Configure ($default_config , @{$atts {config }});
158
- $self -> {settings } = Getopt::Long::Configure ($save );
159
- delete ($atts {config });
160
- }
161
- # Else use default config.
162
- else {
163
- $self -> {settings } = $default_config ;
164
- }
165
-
166
- if ( %atts ) { # Oops
167
- die (__PACKAGE__ ." : unhandled attributes: " .
168
- join (" " , sort (keys (%atts )))." \n " );
169
- }
135
+ # For the parser only.
136
+ sub _default_config { $default_config }
170
137
171
- $self ;
172
- }
173
-
174
- sub configure {
175
- my ($self ) = shift ;
176
-
177
- # Restore settings, merge new settings in.
178
- my $save = Getopt::Long::Configure ($self -> {settings }, @_ );
179
-
180
- # Restore orig config and save the new config.
181
- $self -> {settings } = Getopt::Long::Configure ($save );
182
- }
183
-
184
- sub getoptions {
185
- my ($self ) = shift ;
186
-
187
- return $self -> getoptionsfromarray(\@ARGV , @_ );
188
- }
189
-
190
- sub getoptionsfromarray {
191
- my ($self ) = shift ;
192
-
193
- # Restore config settings.
194
- my $save = Getopt::Long::Configure ($self -> {settings });
195
-
196
- # Call main routine.
197
- my $ret = 0;
198
- $Getopt::Long::caller = $self -> {caller_pkg };
138
+ # ############### Back to Normal ################
199
139
200
- eval {
201
- # Locally set exception handler to default, otherwise it will
202
- # be called implicitly here, and again explicitly when we try
203
- # to deliver the messages.
204
- local ($SIG {__DIE__ }) = ' DEFAULT' ;
205
- $ret = Getopt::Long::GetOptionsFromArray (@_ );
206
- };
207
-
208
- # Restore saved settings.
209
- Getopt::Long::Configure ($save );
210
-
211
- # Handle errors and return value.
212
- die ($@ ) if $@ ;
213
- return $ret ;
140
+ # The ooparser was traditionally part of the main package.
141
+ no warnings ' redefine' ;
142
+ sub Getopt ::Long::Parser::new {
143
+ require Getopt::Long::Parser;
144
+ goto &Getopt::Long::Parser::new;
214
145
}
215
-
216
- package Getopt::Long ;
217
-
218
- # ############### Back to Normal ################
146
+ use warnings ' redefine' ;
219
147
220
148
# Indices in option control info.
221
149
# Note that ParseOptions uses the fields directly. Search for 'hard-wired'.
@@ -305,7 +233,7 @@ sub GetOptionsFromArray(@) {
305
233
# Avoid some warnings if debugging.
306
234
local ($^W) = 0;
307
235
print STDERR
308
- (" Getopt::Long $Getopt::Long::VERSION_STRING " ,
236
+ (" Getopt::Long $VERSION " ,
309
237
" called from package \" $pkg \" ." ,
310
238
" \n " ,
311
239
" argv: " ,
@@ -806,11 +734,15 @@ sub OptCtl ($) {
806
734
sub ParseOptionSpec ($$) {
807
735
my ($opt , $opctl ) = @_ ;
808
736
737
+ # Allow period in option name unless passing through,
738
+ my $op = $passthrough
739
+ ? qr / (?: \w +[-\w ]* )/ x : qr / (?: \w +[-.\w ]* )/ x ;
740
+
809
741
# Match option spec.
810
742
if ( $opt !~ m ; ^
811
743
(
812
744
# Option name
813
- (?: \w +[- \w ]* )
745
+ $op
814
746
# Aliases
815
747
(?: \| (?: . [^|!+=:]* )? )*
816
748
)?
@@ -929,7 +861,8 @@ sub ParseOptionSpec ($$) {
929
861
}
930
862
}
931
863
932
- if ( $dups && $^W ) {
864
+ if ( $dups ) {
865
+ # Warn now. Will become fatal in a future release.
933
866
foreach ( split (/ \n +/ , $dups ) ) {
934
867
warn ($_ ." \n " );
935
868
}
@@ -1495,9 +1428,7 @@ sub VersionMessage(@) {
1495
1428
$0 , defined $v ? " version $v " : (),
1496
1429
" \n " ,
1497
1430
" (" , __PACKAGE__ , " ::" , " GetOptions" ,
1498
- " version " ,
1499
- defined ($Getopt::Long::VERSION_STRING )
1500
- ? $Getopt::Long::VERSION_STRING : $VERSION , " ;" ,
1431
+ " version $VERSION ," ,
1501
1432
" Perl version " ,
1502
1433
$] >= 5.006 ? sprintf (" %vd " , $^V) : $] ,
1503
1434
" )\n " );
@@ -1515,7 +1446,7 @@ sub VersionMessage(@) {
1515
1446
sub HelpMessage (@) {
1516
1447
eval {
1517
1448
require Pod::Usage;
1518
- import Pod::Usage;
1449
+ Pod::Usage-> import ;
1519
1450
1;
1520
1451
} || die (" Cannot provide help: cannot load Pod::Usage\n " );
1521
1452
@@ -1941,7 +1872,9 @@ and the argument specification.
1941
1872
1942
1873
The name specification contains the name of the option, optionally
1943
1874
followed by a list of alternative names separated by vertical bar
1944
- characters.
1875
+ characters. The name is made up of alphanumeric characters, hyphens,
1876
+ underscores. If C<pass_through > is disabled, a period is also allowed in
1877
+ option names.
1945
1878
1946
1879
length option name is "length"
1947
1880
length|size|l name is "length", aliases are "size" and "l"
@@ -2048,18 +1981,7 @@ option will be incremented.
2048
1981
2049
1982
=head2 Object oriented interface
2050
1983
2051
- Getopt::Long can be used in an object oriented way as well:
2052
-
2053
- use Getopt::Long;
2054
- $p = Getopt::Long::Parser->new;
2055
- $p->configure(...configuration options...);
2056
- if ($p->getoptions(...options descriptions...)) ...
2057
- if ($p->getoptionsfromarray( \@array, ...options descriptions...)) ...
2058
-
2059
- Configuration options can be passed to the constructor:
2060
-
2061
- $p = new Getopt::Long::Parser
2062
- config => [...configuration options...];
1984
+ See L<Getopt::Long::Parser> .
2063
1985
2064
1986
=head2 Callback object
2065
1987
@@ -2389,11 +2311,12 @@ POSIXLY_CORRECT has been set, in which case C<getopt_compat> is disabled.
2389
2311
2390
2312
C<gnu_compat > controls whether C<--opt= > is allowed, and what it should
2391
2313
do. Without C<gnu_compat > , C<--opt= > gives an error. With C<gnu_compat > ,
2392
- C<--opt= > will give option C<opt > and empty value.
2314
+ C<--opt= > will give option C<opt > an empty value.
2393
2315
This is the way GNU getopt_long() does it.
2394
2316
2395
- Note that C<--opt value > is still accepted, even though GNU
2396
- getopt_long() doesn't.
2317
+ Note that for options with optional arguments, C<--opt value > is still
2318
+ accepted, even though GNU getopt_long() requires writing C<--opt=value >
2319
+ in this case.
2397
2320
2398
2321
=item gnu_getopt
2399
2322
@@ -2677,7 +2600,7 @@ When no destination is specified for an option, GetOptions will store
2677
2600
the resultant value in a global variable named C<opt_ >I<XXX > , where
2678
2601
I<XXX > is the primary name of this option. When a program executes
2679
2602
under C<use strict > (recommended), these variables must be
2680
- pre-declared with our() or C< use vars > .
2603
+ pre-declared with our().
2681
2604
2682
2605
our $opt_length = 0;
2683
2606
GetOptions ('length=i'); # will store in $opt_length
2805
2728
2806
2729
=head1 COPYRIGHT AND DISCLAIMER
2807
2730
2808
- This program is Copyright 1990,2015 by Johan Vromans.
2731
+ This program is Copyright 1990,2015,2023 by Johan Vromans.
2809
2732
This program is free software; you can redistribute it and/or
2810
2733
modify it under the terms of the Perl Artistic License or the
2811
2734
GNU General Public License as published by the Free Software
0 commit comments