line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#! perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Getopt::Long.pm -- Universal options parsing |
4
|
|
|
|
|
|
|
# Author : Johan Vromans |
5
|
|
|
|
|
|
|
# Created On : Tue Sep 11 15:00:12 1990 |
6
|
|
|
|
|
|
|
# Last Modified By: Johan Vromans |
7
|
|
|
|
|
|
|
# Last Modified On: Fri Aug 19 17:35:14 2022 |
8
|
|
|
|
|
|
|
# Update Count : 1756 |
9
|
|
|
|
|
|
|
# Status : Released |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
################ Module Preamble ################ |
12
|
|
|
|
|
|
|
|
13
|
6
|
|
|
6
|
|
3342
|
use 5.004; |
|
6
|
|
|
|
|
36
|
|
14
|
|
|
|
|
|
|
|
15
|
6
|
|
|
6
|
|
27
|
use strict; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
102
|
|
16
|
6
|
|
|
6
|
|
20
|
use warnings; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
227
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
package Getopt::Long; |
19
|
|
|
|
|
|
|
|
20
|
6
|
|
|
6
|
|
29
|
use vars qw($VERSION); |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
451
|
|
21
|
|
|
|
|
|
|
$VERSION = 2.52_002; |
22
|
|
|
|
|
|
|
# For testing versions only. |
23
|
6
|
|
|
6
|
|
34
|
use vars qw($VERSION_STRING); |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
226
|
|
24
|
|
|
|
|
|
|
$VERSION_STRING = "2.52_2"; |
25
|
|
|
|
|
|
|
|
26
|
6
|
|
|
6
|
|
30
|
use Exporter; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
265
|
|
27
|
6
|
|
|
6
|
|
30
|
use vars qw(@ISA @EXPORT @EXPORT_OK); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
727
|
|
28
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Exported subroutines. |
31
|
|
|
|
|
|
|
sub GetOptions(@); # always |
32
|
|
|
|
|
|
|
sub GetOptionsFromArray(@); # on demand |
33
|
|
|
|
|
|
|
sub GetOptionsFromString(@); # on demand |
34
|
|
|
|
|
|
|
sub Configure(@); # on demand |
35
|
|
|
|
|
|
|
sub HelpMessage(@); # on demand |
36
|
|
|
|
|
|
|
sub VersionMessage(@); # in demand |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
BEGIN { |
39
|
|
|
|
|
|
|
# Init immediately so their contents can be used in the 'use vars' below. |
40
|
6
|
|
|
6
|
|
23
|
@EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER); |
41
|
6
|
|
|
|
|
162
|
@EXPORT_OK = qw(&HelpMessage &VersionMessage &Configure |
42
|
|
|
|
|
|
|
&GetOptionsFromArray &GetOptionsFromString); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# User visible variables. |
46
|
6
|
|
|
6
|
|
33
|
use vars @EXPORT, @EXPORT_OK; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
495
|
|
47
|
6
|
|
|
6
|
|
34
|
use vars qw($error $debug $major_version $minor_version); |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
403
|
|
48
|
|
|
|
|
|
|
# Deprecated visible variables. |
49
|
6
|
|
|
|
|
333
|
use vars qw($autoabbrev $getopt_compat $ignorecase $bundling $order |
50
|
6
|
|
|
6
|
|
31
|
$passthrough); |
|
6
|
|
|
|
|
8
|
|
51
|
|
|
|
|
|
|
# Official invisible variables. |
52
|
6
|
|
|
6
|
|
36
|
use vars qw($genprefix $caller $gnu_compat $auto_help $auto_version $longprefix); |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
4541
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Really invisible variables. |
55
|
|
|
|
|
|
|
my $bundling_values; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Public subroutines. |
58
|
|
|
|
|
|
|
sub config(@); # deprecated name |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Private subroutines. |
61
|
|
|
|
|
|
|
sub ConfigDefaults(); |
62
|
|
|
|
|
|
|
sub ParseOptionSpec($$); |
63
|
|
|
|
|
|
|
sub OptCtl($); |
64
|
|
|
|
|
|
|
sub FindOption($$$$$); |
65
|
|
|
|
|
|
|
sub ValidValue ($$$$$); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
################ Local Variables ################ |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# $requested_version holds the version that was mentioned in the 'use' |
70
|
|
|
|
|
|
|
# or 'require', if any. It can be used to enable or disable specific |
71
|
|
|
|
|
|
|
# features. |
72
|
|
|
|
|
|
|
my $requested_version = 0; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
################ Resident subroutines ################ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub ConfigDefaults() { |
77
|
|
|
|
|
|
|
# Handle POSIX compliancy. |
78
|
13
|
50
|
|
13
|
0
|
50
|
if ( defined $ENV{"POSIXLY_CORRECT"} ) { |
79
|
0
|
|
|
|
|
0
|
$genprefix = "(--|-)"; |
80
|
0
|
|
|
|
|
0
|
$autoabbrev = 0; # no automatic abbrev of options |
81
|
0
|
|
|
|
|
0
|
$bundling = 0; # no bundling of single letter switches |
82
|
0
|
|
|
|
|
0
|
$getopt_compat = 0; # disallow '+' to start options |
83
|
0
|
|
|
|
|
0
|
$order = $REQUIRE_ORDER; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
13
|
|
|
|
|
20
|
$genprefix = "(--|-|\\+)"; |
87
|
13
|
|
|
|
|
15
|
$autoabbrev = 1; # automatic abbrev of options |
88
|
13
|
|
|
|
|
15
|
$bundling = 0; # bundling off by default |
89
|
13
|
|
|
|
|
17
|
$getopt_compat = 1; # allow '+' to start options |
90
|
13
|
|
|
|
|
19
|
$order = $PERMUTE; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
# Other configurable settings. |
93
|
13
|
|
|
|
|
32
|
$debug = 0; # for debugging |
94
|
13
|
|
|
|
|
15
|
$error = 0; # error tally |
95
|
13
|
|
|
|
|
16
|
$ignorecase = 1; # ignore case when matching options |
96
|
13
|
|
|
|
|
16
|
$passthrough = 0; # leave unrecognized options alone |
97
|
13
|
|
|
|
|
13
|
$gnu_compat = 0; # require --opt=val if value is optional |
98
|
13
|
|
|
|
|
16
|
$longprefix = "(--)"; # what does a long prefix look like |
99
|
13
|
|
|
|
|
20
|
$bundling_values = 0; # no bundling of values |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Override import. |
103
|
|
|
|
|
|
|
sub import { |
104
|
6
|
|
|
6
|
|
37
|
my $pkg = shift; # package |
105
|
6
|
|
|
|
|
11
|
my @syms = (); # symbols to import |
106
|
6
|
|
|
|
|
11
|
my @config = (); # configuration |
107
|
6
|
|
|
|
|
9
|
my $dest = \@syms; # symbols first |
108
|
6
|
|
|
|
|
10
|
for ( @_ ) { |
109
|
8
|
100
|
|
|
|
20
|
if ( $_ eq ':config' ) { |
110
|
3
|
|
|
|
|
5
|
$dest = \@config; # config next |
111
|
3
|
|
|
|
|
5
|
next; |
112
|
|
|
|
|
|
|
} |
113
|
5
|
|
|
|
|
12
|
push(@$dest, $_); # push |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
# Hide one level and call super. |
116
|
6
|
|
|
|
|
11
|
local $Exporter::ExportLevel = 1; |
117
|
6
|
100
|
|
|
|
20
|
push(@syms, qw(&GetOptions)) if @syms; # always export GetOptions |
118
|
6
|
|
|
|
|
8
|
$requested_version = 0; |
119
|
6
|
|
|
|
|
485
|
$pkg->SUPER::import(@syms); |
120
|
|
|
|
|
|
|
# And configure. |
121
|
6
|
100
|
|
|
|
2033
|
Configure(@config) if @config; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
################ Initialization ################ |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Values for $order. See GNU getopt.c for details. |
127
|
|
|
|
|
|
|
($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2); |
128
|
|
|
|
|
|
|
# Version major/minor numbers. |
129
|
|
|
|
|
|
|
($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
ConfigDefaults(); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
################ OO Interface ################ |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
package Getopt::Long::Parser; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Store a copy of the default configuration. Since ConfigDefaults has |
138
|
|
|
|
|
|
|
# just been called, what we get from Configure is the default. |
139
|
|
|
|
|
|
|
my $default_config = do { |
140
|
|
|
|
|
|
|
Getopt::Long::Configure () |
141
|
|
|
|
|
|
|
}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub new { |
144
|
2
|
|
|
2
|
|
43
|
my $that = shift; |
145
|
2
|
|
33
|
|
|
9
|
my $class = ref($that) || $that; |
146
|
2
|
|
|
|
|
5
|
my %atts = @_; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Register the callers package. |
149
|
2
|
|
|
|
|
7
|
my $self = { caller_pkg => (caller)[0] }; |
150
|
|
|
|
|
|
|
|
151
|
2
|
|
|
|
|
5
|
bless ($self, $class); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Process config attributes. |
154
|
2
|
100
|
|
|
|
4
|
if ( defined $atts{config} ) { |
155
|
1
|
|
|
|
|
2
|
my $save = Getopt::Long::Configure ($default_config, @{$atts{config}}); |
|
1
|
|
|
|
|
3
|
|
156
|
1
|
|
|
|
|
3
|
$self->{settings} = Getopt::Long::Configure ($save); |
157
|
1
|
|
|
|
|
4
|
delete ($atts{config}); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
# Else use default config. |
160
|
|
|
|
|
|
|
else { |
161
|
1
|
|
|
|
|
2
|
$self->{settings} = $default_config; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
2
|
50
|
|
|
|
5
|
if ( %atts ) { # Oops |
165
|
0
|
|
|
|
|
0
|
die(__PACKAGE__.": unhandled attributes: ". |
166
|
|
|
|
|
|
|
join(" ", sort(keys(%atts)))."\n"); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
4
|
$self; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub configure { |
173
|
0
|
|
|
0
|
|
0
|
my ($self) = shift; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Restore settings, merge new settings in. |
176
|
0
|
|
|
|
|
0
|
my $save = Getopt::Long::Configure ($self->{settings}, @_); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Restore orig config and save the new config. |
179
|
0
|
|
|
|
|
0
|
$self->{settings} = Getopt::Long::Configure ($save); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub getoptions { |
183
|
1
|
|
|
1
|
|
7
|
my ($self) = shift; |
184
|
|
|
|
|
|
|
|
185
|
1
|
|
|
|
|
4
|
return $self->getoptionsfromarray(\@ARGV, @_); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub getoptionsfromarray { |
189
|
2
|
|
|
2
|
|
4
|
my ($self) = shift; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Restore config settings. |
192
|
2
|
|
|
|
|
5
|
my $save = Getopt::Long::Configure ($self->{settings}); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# Call main routine. |
195
|
2
|
|
|
|
|
4
|
my $ret = 0; |
196
|
2
|
|
|
|
|
3
|
$Getopt::Long::caller = $self->{caller_pkg}; |
197
|
|
|
|
|
|
|
|
198
|
2
|
|
|
|
|
3
|
eval { |
199
|
|
|
|
|
|
|
# Locally set exception handler to default, otherwise it will |
200
|
|
|
|
|
|
|
# be called implicitly here, and again explicitly when we try |
201
|
|
|
|
|
|
|
# to deliver the messages. |
202
|
2
|
|
|
|
|
9
|
local ($SIG{__DIE__}) = 'DEFAULT'; |
203
|
2
|
|
|
|
|
4
|
$ret = Getopt::Long::GetOptionsFromArray (@_); |
204
|
|
|
|
|
|
|
}; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# Restore saved settings. |
207
|
2
|
|
|
|
|
5
|
Getopt::Long::Configure ($save); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
# Handle errors and return value. |
210
|
2
|
50
|
|
|
|
4
|
die ($@) if $@; |
211
|
2
|
|
|
|
|
6
|
return $ret; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
package Getopt::Long; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
################ Back to Normal ################ |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Indices in option control info. |
219
|
|
|
|
|
|
|
# Note that ParseOptions uses the fields directly. Search for 'hard-wired'. |
220
|
6
|
|
|
6
|
|
41
|
use constant CTL_TYPE => 0; |
|
6
|
|
|
|
|
24
|
|
|
6
|
|
|
|
|
574
|
|
221
|
|
|
|
|
|
|
#use constant CTL_TYPE_FLAG => ''; |
222
|
|
|
|
|
|
|
#use constant CTL_TYPE_NEG => '!'; |
223
|
|
|
|
|
|
|
#use constant CTL_TYPE_INCR => '+'; |
224
|
|
|
|
|
|
|
#use constant CTL_TYPE_INT => 'i'; |
225
|
|
|
|
|
|
|
#use constant CTL_TYPE_INTINC => 'I'; |
226
|
|
|
|
|
|
|
#use constant CTL_TYPE_XINT => 'o'; |
227
|
|
|
|
|
|
|
#use constant CTL_TYPE_FLOAT => 'f'; |
228
|
|
|
|
|
|
|
#use constant CTL_TYPE_STRING => 's'; |
229
|
|
|
|
|
|
|
|
230
|
6
|
|
|
6
|
|
35
|
use constant CTL_CNAME => 1; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
313
|
|
231
|
|
|
|
|
|
|
|
232
|
6
|
|
|
6
|
|
34
|
use constant CTL_DEFAULT => 2; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
269
|
|
233
|
|
|
|
|
|
|
|
234
|
6
|
|
|
6
|
|
33
|
use constant CTL_DEST => 3; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
285
|
|
235
|
6
|
|
|
6
|
|
31
|
use constant CTL_DEST_SCALAR => 0; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
280
|
|
236
|
6
|
|
|
6
|
|
32
|
use constant CTL_DEST_ARRAY => 1; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
270
|
|
237
|
6
|
|
|
6
|
|
31
|
use constant CTL_DEST_HASH => 2; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
265
|
|
238
|
6
|
|
|
6
|
|
30
|
use constant CTL_DEST_CODE => 3; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
242
|
|
239
|
|
|
|
|
|
|
|
240
|
6
|
|
|
6
|
|
30
|
use constant CTL_AMIN => 4; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
242
|
|
241
|
6
|
|
|
6
|
|
29
|
use constant CTL_AMAX => 5; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
343
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# FFU. |
244
|
|
|
|
|
|
|
#use constant CTL_RANGE => ; |
245
|
|
|
|
|
|
|
#use constant CTL_REPEAT => ; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# Rather liberal patterns to match numbers. |
248
|
6
|
|
|
6
|
|
100
|
use constant PAT_INT => "[-+]?_*[0-9][0-9_]*"; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
389
|
|
249
|
6
|
|
|
|
|
347
|
use constant PAT_XINT => |
250
|
|
|
|
|
|
|
"(?:". |
251
|
|
|
|
|
|
|
"[-+]?_*[1-9][0-9_]*". |
252
|
|
|
|
|
|
|
"|". |
253
|
|
|
|
|
|
|
"0x_*[0-9a-f][0-9a-f_]*". |
254
|
|
|
|
|
|
|
"|". |
255
|
|
|
|
|
|
|
"0b_*[01][01_]*". |
256
|
|
|
|
|
|
|
"|". |
257
|
|
|
|
|
|
|
"0[0-7_]*". |
258
|
6
|
|
|
6
|
|
33
|
")"; |
|
6
|
|
|
|
|
7
|
|
259
|
6
|
|
|
|
|
41063
|
use constant PAT_FLOAT => |
260
|
|
|
|
|
|
|
"[-+]?". # optional sign |
261
|
|
|
|
|
|
|
"(?=\\.?[0-9])". # must start with digit or dec.point |
262
|
|
|
|
|
|
|
"[0-9_]*". # digits before the dec.point |
263
|
|
|
|
|
|
|
"(\\.[0-9_]*)?". # optional fraction |
264
|
6
|
|
|
6
|
|
32
|
"([eE][-+]?[0-9_]+)?"; # optional exponent |
|
6
|
|
|
|
|
7
|
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub GetOptions(@) { |
267
|
|
|
|
|
|
|
# Shift in default array. |
268
|
14
|
|
|
14
|
|
144
|
unshift(@_, \@ARGV); |
269
|
|
|
|
|
|
|
# Try to keep caller() and Carp consistent. |
270
|
14
|
|
|
|
|
37
|
goto &GetOptionsFromArray; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub GetOptionsFromString(@) { |
274
|
3
|
|
|
3
|
|
69
|
my ($string) = shift; |
275
|
3
|
|
|
|
|
428
|
require Text::ParseWords; |
276
|
3
|
|
|
|
|
1132
|
my $args = [ Text::ParseWords::shellwords($string) ]; |
277
|
3
|
|
66
|
|
|
447
|
$caller ||= (caller)[0]; # current context |
278
|
3
|
|
|
|
|
8
|
my $ret = GetOptionsFromArray($args, @_); |
279
|
3
|
100
|
|
|
|
8
|
return ( $ret, $args ) if wantarray; |
280
|
2
|
100
|
|
|
|
3
|
if ( @$args ) { |
281
|
1
|
|
|
|
|
3
|
$ret = 0; |
282
|
1
|
|
|
|
|
11
|
warn("GetOptionsFromString: Excess data \"@$args\" in string \"$string\"\n"); |
283
|
|
|
|
|
|
|
} |
284
|
2
|
|
|
|
|
10
|
$ret; |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub GetOptionsFromArray(@) { |
288
|
|
|
|
|
|
|
|
289
|
20
|
|
|
20
|
|
75
|
my ($argv, @optionlist) = @_; # local copy of the option descriptions |
290
|
20
|
|
|
|
|
28
|
my $argend = '--'; # option list terminator |
291
|
20
|
|
|
|
|
26
|
my %opctl = (); # table of option specs |
292
|
20
|
|
66
|
|
|
79
|
my $pkg = $caller || (caller)[0]; # current context |
293
|
|
|
|
|
|
|
# Needed if linkage is omitted. |
294
|
20
|
|
|
|
|
34
|
my @ret = (); # accum for non-options |
295
|
20
|
|
|
|
|
41
|
my %linkage; # linkage |
296
|
|
|
|
|
|
|
my $userlinkage; # user supplied HASH |
297
|
20
|
|
|
|
|
0
|
my $opt; # current option |
298
|
20
|
|
|
|
|
28
|
my $prefix = $genprefix; # current prefix |
299
|
|
|
|
|
|
|
|
300
|
20
|
|
|
|
|
26
|
$error = ''; |
301
|
|
|
|
|
|
|
|
302
|
20
|
50
|
|
|
|
35
|
if ( $debug ) { |
303
|
|
|
|
|
|
|
# Avoid some warnings if debugging. |
304
|
0
|
|
|
|
|
0
|
local ($^W) = 0; |
305
|
0
|
0
|
|
|
|
0
|
print STDERR |
|
|
0
|
|
|
|
|
|
306
|
|
|
|
|
|
|
("Getopt::Long $Getopt::Long::VERSION_STRING ", |
307
|
|
|
|
|
|
|
"called from package \"$pkg\".", |
308
|
|
|
|
|
|
|
"\n ", |
309
|
|
|
|
|
|
|
"argv: ", |
310
|
|
|
|
|
|
|
defined($argv) |
311
|
|
|
|
|
|
|
? UNIVERSAL::isa( $argv, 'ARRAY' ) ? "(@$argv)" : $argv |
312
|
|
|
|
|
|
|
: "", |
313
|
|
|
|
|
|
|
"\n ", |
314
|
|
|
|
|
|
|
"autoabbrev=$autoabbrev,". |
315
|
|
|
|
|
|
|
"bundling=$bundling,", |
316
|
|
|
|
|
|
|
"bundling_values=$bundling_values,", |
317
|
|
|
|
|
|
|
"getopt_compat=$getopt_compat,", |
318
|
|
|
|
|
|
|
"gnu_compat=$gnu_compat,", |
319
|
|
|
|
|
|
|
"order=$order,", |
320
|
|
|
|
|
|
|
"\n ", |
321
|
|
|
|
|
|
|
"ignorecase=$ignorecase,", |
322
|
|
|
|
|
|
|
"requested_version=$requested_version,", |
323
|
|
|
|
|
|
|
"passthrough=$passthrough,", |
324
|
|
|
|
|
|
|
"genprefix=\"$genprefix\",", |
325
|
|
|
|
|
|
|
"longprefix=\"$longprefix\".", |
326
|
|
|
|
|
|
|
"\n"); |
327
|
|
|
|
|
|
|
} |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
# Check for ref HASH as first argument. |
330
|
|
|
|
|
|
|
# First argument may be an object. It's OK to use this as long |
331
|
|
|
|
|
|
|
# as it is really a hash underneath. |
332
|
20
|
|
|
|
|
30
|
$userlinkage = undef; |
333
|
20
|
100
|
66
|
|
|
85
|
if ( @optionlist && ref($optionlist[0]) and |
|
|
|
66
|
|
|
|
|
334
|
|
|
|
|
|
|
UNIVERSAL::isa($optionlist[0],'HASH') ) { |
335
|
6
|
|
|
|
|
10
|
$userlinkage = shift (@optionlist); |
336
|
6
|
50
|
|
|
|
20
|
print STDERR ("=> user linkage: $userlinkage\n") if $debug; |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
# See if the first element of the optionlist contains option |
340
|
|
|
|
|
|
|
# starter characters. |
341
|
|
|
|
|
|
|
# Be careful not to interpret '<>' as option starters. |
342
|
20
|
50
|
33
|
|
|
130
|
if ( @optionlist && $optionlist[0] =~ /^\W+$/ |
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
343
|
|
|
|
|
|
|
&& !($optionlist[0] eq '<>' |
344
|
|
|
|
|
|
|
&& @optionlist > 0 |
345
|
|
|
|
|
|
|
&& ref($optionlist[1])) ) { |
346
|
0
|
|
|
|
|
0
|
$prefix = shift (@optionlist); |
347
|
|
|
|
|
|
|
# Turn into regexp. Needs to be parenthesized! |
348
|
0
|
|
|
|
|
0
|
$prefix =~ s/(\W)/\\$1/g; |
349
|
0
|
|
|
|
|
0
|
$prefix = "([" . $prefix . "])"; |
350
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> prefix=\"$prefix\"\n") if $debug; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
# Verify correctness of optionlist. |
354
|
20
|
|
|
|
|
33
|
%opctl = (); |
355
|
20
|
|
|
|
|
44
|
while ( @optionlist ) { |
356
|
38
|
|
|
|
|
59
|
my $opt = shift (@optionlist); |
357
|
|
|
|
|
|
|
|
358
|
38
|
50
|
|
|
|
65
|
unless ( defined($opt) ) { |
359
|
0
|
|
|
|
|
0
|
$error .= "Undefined argument in option spec\n"; |
360
|
0
|
|
|
|
|
0
|
next; |
361
|
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
# Strip leading prefix so people can specify "--foo=i" if they like. |
364
|
38
|
50
|
|
|
|
339
|
$opt = $+ if $opt =~ /^$prefix+(.*)$/s; |
365
|
|
|
|
|
|
|
|
366
|
38
|
100
|
|
|
|
122
|
if ( $opt eq '<>' ) { |
367
|
2
|
0
|
0
|
|
|
5
|
if ( (defined $userlinkage) |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
0
|
|
|
|
|
368
|
|
|
|
|
|
|
&& !(@optionlist > 0 && ref($optionlist[0])) |
369
|
|
|
|
|
|
|
&& (exists $userlinkage->{$opt}) |
370
|
|
|
|
|
|
|
&& ref($userlinkage->{$opt}) ) { |
371
|
0
|
|
|
|
|
0
|
unshift (@optionlist, $userlinkage->{$opt}); |
372
|
|
|
|
|
|
|
} |
373
|
2
|
50
|
33
|
|
|
12
|
unless ( @optionlist > 0 |
|
|
|
33
|
|
|
|
|
374
|
|
|
|
|
|
|
&& ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) { |
375
|
0
|
|
|
|
|
0
|
$error .= "Option spec <> requires a reference to a subroutine\n"; |
376
|
|
|
|
|
|
|
# Kill the linkage (to avoid another error). |
377
|
0
|
0
|
0
|
|
|
0
|
shift (@optionlist) |
378
|
|
|
|
|
|
|
if @optionlist && ref($optionlist[0]); |
379
|
0
|
|
|
|
|
0
|
next; |
380
|
|
|
|
|
|
|
} |
381
|
2
|
|
|
|
|
3
|
$linkage{'<>'} = shift (@optionlist); |
382
|
2
|
|
|
|
|
5
|
next; |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
# Parse option spec. |
386
|
36
|
|
|
|
|
105
|
my ($name, $orig) = ParseOptionSpec ($opt, \%opctl); |
387
|
36
|
50
|
|
|
|
89
|
unless ( defined $name ) { |
388
|
|
|
|
|
|
|
# Failed. $orig contains the error message. Sorry for the abuse. |
389
|
0
|
|
|
|
|
0
|
$error .= $orig; |
390
|
|
|
|
|
|
|
# Kill the linkage (to avoid another error). |
391
|
0
|
0
|
0
|
|
|
0
|
shift (@optionlist) |
392
|
|
|
|
|
|
|
if @optionlist && ref($optionlist[0]); |
393
|
0
|
|
|
|
|
0
|
next; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# If no linkage is supplied in the @optionlist, copy it from |
397
|
|
|
|
|
|
|
# the userlinkage if available. |
398
|
36
|
100
|
|
|
|
64
|
if ( defined $userlinkage ) { |
399
|
11
|
100
|
100
|
|
|
29
|
unless ( @optionlist > 0 && ref($optionlist[0]) ) { |
400
|
6
|
50
|
33
|
|
|
13
|
if ( exists $userlinkage->{$orig} && |
401
|
|
|
|
|
|
|
ref($userlinkage->{$orig}) ) { |
402
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> found userlinkage for \"$orig\": ", |
403
|
|
|
|
|
|
|
"$userlinkage->{$orig}\n") |
404
|
|
|
|
|
|
|
if $debug; |
405
|
0
|
|
|
|
|
0
|
unshift (@optionlist, $userlinkage->{$orig}); |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
else { |
408
|
|
|
|
|
|
|
# Do nothing. Being undefined will be handled later. |
409
|
6
|
|
|
|
|
15
|
next; |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
} |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
# Copy the linkage. If omitted, link to global variable. |
415
|
30
|
100
|
100
|
|
|
100
|
if ( @optionlist > 0 && ref($optionlist[0]) ) { |
416
|
15
|
50
|
|
|
|
27
|
print STDERR ("=> link \"$orig\" to $optionlist[0]\n") |
417
|
|
|
|
|
|
|
if $debug; |
418
|
15
|
|
|
|
|
27
|
my $rl = ref($linkage{$orig} = shift (@optionlist)); |
419
|
|
|
|
|
|
|
|
420
|
15
|
50
|
66
|
|
|
61
|
if ( $rl eq "ARRAY" ) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
421
|
0
|
|
|
|
|
0
|
$opctl{$name}[CTL_DEST] = CTL_DEST_ARRAY; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
elsif ( $rl eq "HASH" ) { |
424
|
0
|
|
|
|
|
0
|
$opctl{$name}[CTL_DEST] = CTL_DEST_HASH; |
425
|
|
|
|
|
|
|
} |
426
|
|
|
|
|
|
|
elsif ( $rl eq "SCALAR" || $rl eq "REF" ) { |
427
|
|
|
|
|
|
|
# if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) { |
428
|
|
|
|
|
|
|
# my $t = $linkage{$orig}; |
429
|
|
|
|
|
|
|
# $$t = $linkage{$orig} = []; |
430
|
|
|
|
|
|
|
# } |
431
|
|
|
|
|
|
|
# elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) { |
432
|
|
|
|
|
|
|
# } |
433
|
|
|
|
|
|
|
# else { |
434
|
|
|
|
|
|
|
# Ok. |
435
|
|
|
|
|
|
|
# } |
436
|
|
|
|
|
|
|
} |
437
|
|
|
|
|
|
|
elsif ( $rl eq "CODE" ) { |
438
|
|
|
|
|
|
|
# Ok. |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
else { |
441
|
0
|
|
|
|
|
0
|
$error .= "Invalid option linkage for \"$opt\"\n"; |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
else { |
445
|
|
|
|
|
|
|
# Link to global $opt_XXX variable. |
446
|
|
|
|
|
|
|
# Make sure a valid perl identifier results. |
447
|
15
|
|
|
|
|
22
|
my $ov = $orig; |
448
|
15
|
|
|
|
|
33
|
$ov =~ s/\W/_/g; |
449
|
15
|
50
|
|
|
|
79
|
if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) { |
|
|
50
|
|
|
|
|
|
450
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> link \"$orig\" to \@$pkg","::opt_$ov\n") |
451
|
|
|
|
|
|
|
if $debug; |
452
|
0
|
|
|
|
|
0
|
eval ("\$linkage{\$orig} = \\\@".$pkg."::opt_$ov;"); |
453
|
|
|
|
|
|
|
} |
454
|
|
|
|
|
|
|
elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) { |
455
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> link \"$orig\" to \%$pkg","::opt_$ov\n") |
456
|
|
|
|
|
|
|
if $debug; |
457
|
0
|
|
|
|
|
0
|
eval ("\$linkage{\$orig} = \\\%".$pkg."::opt_$ov;"); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
else { |
460
|
15
|
50
|
|
|
|
42
|
print STDERR ("=> link \"$orig\" to \$$pkg","::opt_$ov\n") |
461
|
|
|
|
|
|
|
if $debug; |
462
|
15
|
|
|
|
|
911
|
eval ("\$linkage{\$orig} = \\\$".$pkg."::opt_$ov;"); |
463
|
|
|
|
|
|
|
} |
464
|
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
|
|
466
|
30
|
0
|
0
|
|
|
126
|
if ( $opctl{$name}[CTL_TYPE] eq 'I' |
|
|
|
33
|
|
|
|
|
467
|
|
|
|
|
|
|
&& ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY |
468
|
|
|
|
|
|
|
|| $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) |
469
|
|
|
|
|
|
|
) { |
470
|
0
|
|
|
|
|
0
|
$error .= "Invalid option linkage for \"$opt\"\n"; |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
|
|
475
|
20
|
50
|
33
|
|
|
104
|
$error .= "GetOptionsFromArray: 1st parameter is not an array reference\n" |
476
|
|
|
|
|
|
|
unless $argv && UNIVERSAL::isa( $argv, 'ARRAY' ); |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
# Bail out if errors found. |
479
|
20
|
50
|
|
|
|
38
|
die ($error) if $error; |
480
|
20
|
|
|
|
|
27
|
$error = 0; |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
# Supply --version and --help support, if needed and allowed. |
483
|
20
|
50
|
|
|
|
62
|
if ( defined($auto_version) ? $auto_version : ($requested_version >= 2.3203) ) { |
|
|
50
|
|
|
|
|
|
484
|
0
|
0
|
|
|
|
0
|
if ( !defined($opctl{version}) ) { |
485
|
0
|
|
|
|
|
0
|
$opctl{version} = ['','version',0,CTL_DEST_CODE,undef]; |
486
|
0
|
|
|
|
|
0
|
$linkage{version} = \&VersionMessage; |
487
|
|
|
|
|
|
|
} |
488
|
0
|
|
|
|
|
0
|
$auto_version = 1; |
489
|
|
|
|
|
|
|
} |
490
|
20
|
50
|
|
|
|
52
|
if ( defined($auto_help) ? $auto_help : ($requested_version >= 2.3203) ) { |
|
|
50
|
|
|
|
|
|
491
|
0
|
0
|
0
|
|
|
0
|
if ( !defined($opctl{help}) && !defined($opctl{'?'}) ) { |
492
|
0
|
|
|
|
|
0
|
$opctl{help} = $opctl{'?'} = ['','help',0,CTL_DEST_CODE,undef]; |
493
|
0
|
|
|
|
|
0
|
$linkage{help} = \&HelpMessage; |
494
|
|
|
|
|
|
|
} |
495
|
0
|
|
|
|
|
0
|
$auto_help = 1; |
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
# Show the options tables if debugging. |
499
|
20
|
50
|
|
|
|
44
|
if ( $debug ) { |
500
|
0
|
|
|
|
|
0
|
my ($arrow, $k, $v); |
501
|
0
|
|
|
|
|
0
|
$arrow = "=> "; |
502
|
0
|
|
|
|
|
0
|
while ( ($k,$v) = each(%opctl) ) { |
503
|
0
|
|
|
|
|
0
|
print STDERR ($arrow, "\$opctl{$k} = $v ", OptCtl($v), "\n"); |
504
|
0
|
|
|
|
|
0
|
$arrow = " "; |
505
|
|
|
|
|
|
|
} |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
# Process argument list |
509
|
20
|
|
|
|
|
26
|
my $goon = 1; |
510
|
20
|
|
66
|
|
|
85
|
while ( $goon && @$argv > 0 ) { |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
# Get next argument. |
513
|
52
|
|
|
|
|
84
|
$opt = shift (@$argv); |
514
|
52
|
50
|
|
|
|
82
|
print STDERR ("=> arg \"", $opt, "\"\n") if $debug; |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
# Double dash is option list terminator. |
517
|
52
|
100
|
66
|
|
|
142
|
if ( defined($opt) && $opt eq $argend ) { |
518
|
1
|
50
|
|
|
|
3
|
push (@ret, $argend) if $passthrough; |
519
|
1
|
|
|
|
|
1
|
last; |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
# Look it up. |
523
|
51
|
|
|
|
|
64
|
my $tryopt = $opt; |
524
|
51
|
|
|
|
|
142
|
my $found; # success status |
525
|
|
|
|
|
|
|
my $key; # key (if hash type) |
526
|
51
|
|
|
|
|
0
|
my $arg; # option argument |
527
|
51
|
|
|
|
|
0
|
my $ctl; # the opctl entry |
528
|
51
|
|
|
|
|
0
|
my $starter; # the actual starter character(s) |
529
|
|
|
|
|
|
|
|
530
|
51
|
|
|
|
|
95
|
($found, $opt, $ctl, $starter, $arg, $key) = |
531
|
|
|
|
|
|
|
FindOption ($argv, $prefix, $argend, $opt, \%opctl); |
532
|
|
|
|
|
|
|
|
533
|
51
|
100
|
|
|
|
133
|
if ( $found ) { |
|
|
100
|
|
|
|
|
|
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
# FindOption undefines $opt in case of errors. |
536
|
36
|
100
|
|
|
|
71
|
next unless defined $opt; |
537
|
|
|
|
|
|
|
|
538
|
35
|
|
|
|
|
42
|
my $argcnt = 0; |
539
|
35
|
|
|
|
|
61
|
while ( defined $arg ) { |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
# Get the canonical name. |
542
|
35
|
|
|
|
|
51
|
my $given = $opt; |
543
|
35
|
50
|
|
|
|
56
|
print STDERR ("=> cname for \"$opt\" is ") if $debug; |
544
|
35
|
|
|
|
|
44
|
$opt = $ctl->[CTL_CNAME]; |
545
|
35
|
50
|
|
|
|
54
|
print STDERR ("\"$ctl->[CTL_CNAME]\"\n") if $debug; |
546
|
|
|
|
|
|
|
|
547
|
35
|
100
|
|
|
|
83
|
if ( defined $linkage{$opt} ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
548
|
|
|
|
|
|
|
print STDERR ("=> ref(\$L{$opt}) -> ", |
549
|
30
|
50
|
|
|
|
51
|
ref($linkage{$opt}), "\n") if $debug; |
550
|
|
|
|
|
|
|
|
551
|
30
|
100
|
66
|
|
|
111
|
if ( ref($linkage{$opt}) eq 'SCALAR' |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
552
|
|
|
|
|
|
|
|| ref($linkage{$opt}) eq 'REF' ) { |
553
|
29
|
50
|
|
|
|
85
|
if ( $ctl->[CTL_TYPE] eq '+' ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
554
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> \$\$L{$opt} += \"$arg\"\n") |
555
|
|
|
|
|
|
|
if $debug; |
556
|
0
|
0
|
|
|
|
0
|
if ( defined ${$linkage{$opt}} ) { |
|
0
|
|
|
|
|
0
|
|
557
|
0
|
|
|
|
|
0
|
${$linkage{$opt}} += $arg; |
|
0
|
|
|
|
|
0
|
|
558
|
|
|
|
|
|
|
} |
559
|
|
|
|
|
|
|
else { |
560
|
0
|
|
|
|
|
0
|
${$linkage{$opt}} = $arg; |
|
0
|
|
|
|
|
0
|
|
561
|
|
|
|
|
|
|
} |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) { |
564
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> ref(\$L{$opt}) auto-vivified", |
565
|
|
|
|
|
|
|
" to ARRAY\n") |
566
|
|
|
|
|
|
|
if $debug; |
567
|
0
|
|
|
|
|
0
|
my $t = $linkage{$opt}; |
568
|
0
|
|
|
|
|
0
|
$$t = $linkage{$opt} = []; |
569
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n") |
570
|
|
|
|
|
|
|
if $debug; |
571
|
0
|
|
|
|
|
0
|
push (@{$linkage{$opt}}, $arg); |
|
0
|
|
|
|
|
0
|
|
572
|
|
|
|
|
|
|
} |
573
|
|
|
|
|
|
|
elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { |
574
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> ref(\$L{$opt}) auto-vivified", |
575
|
|
|
|
|
|
|
" to HASH\n") |
576
|
|
|
|
|
|
|
if $debug; |
577
|
0
|
|
|
|
|
0
|
my $t = $linkage{$opt}; |
578
|
0
|
|
|
|
|
0
|
$$t = $linkage{$opt} = {}; |
579
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n") |
580
|
|
|
|
|
|
|
if $debug; |
581
|
0
|
|
|
|
|
0
|
$linkage{$opt}->{$key} = $arg; |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
else { |
584
|
29
|
50
|
|
|
|
45
|
print STDERR ("=> \$\$L{$opt} = \"$arg\"\n") |
585
|
|
|
|
|
|
|
if $debug; |
586
|
29
|
|
|
|
|
40
|
${$linkage{$opt}} = $arg; |
|
29
|
|
|
|
|
52
|
|
587
|
|
|
|
|
|
|
} |
588
|
|
|
|
|
|
|
} |
589
|
|
|
|
|
|
|
elsif ( ref($linkage{$opt}) eq 'ARRAY' ) { |
590
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n") |
591
|
|
|
|
|
|
|
if $debug; |
592
|
0
|
|
|
|
|
0
|
push (@{$linkage{$opt}}, $arg); |
|
0
|
|
|
|
|
0
|
|
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
elsif ( ref($linkage{$opt}) eq 'HASH' ) { |
595
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n") |
596
|
|
|
|
|
|
|
if $debug; |
597
|
0
|
|
|
|
|
0
|
$linkage{$opt}->{$key} = $arg; |
598
|
|
|
|
|
|
|
} |
599
|
|
|
|
|
|
|
elsif ( ref($linkage{$opt}) eq 'CODE' ) { |
600
|
1
|
0
|
|
|
|
3
|
print STDERR ("=> &L{$opt}(\"$opt\"", |
|
|
50
|
|
|
|
|
|
601
|
|
|
|
|
|
|
$ctl->[CTL_DEST] == CTL_DEST_HASH ? ", \"$key\"" : "", |
602
|
|
|
|
|
|
|
", \"$arg\")\n") |
603
|
|
|
|
|
|
|
if $debug; |
604
|
1
|
|
|
|
|
2
|
my $eval_error = do { |
605
|
1
|
|
|
|
|
1
|
local $@; |
606
|
1
|
|
|
|
|
5
|
local $SIG{__DIE__} = 'DEFAULT'; |
607
|
1
|
|
|
|
|
2
|
eval { |
608
|
1
|
50
|
|
|
|
8
|
&{$linkage{$opt}} |
|
1
|
|
|
|
|
4
|
|
609
|
|
|
|
|
|
|
(Getopt::Long::CallBack->new |
610
|
|
|
|
|
|
|
(name => $opt, |
611
|
|
|
|
|
|
|
given => $given, |
612
|
|
|
|
|
|
|
ctl => $ctl, |
613
|
|
|
|
|
|
|
opctl => \%opctl, |
614
|
|
|
|
|
|
|
linkage => \%linkage, |
615
|
|
|
|
|
|
|
prefix => $prefix, |
616
|
|
|
|
|
|
|
starter => $starter, |
617
|
|
|
|
|
|
|
), |
618
|
|
|
|
|
|
|
$ctl->[CTL_DEST] == CTL_DEST_HASH ? ($key) : (), |
619
|
|
|
|
|
|
|
$arg); |
620
|
|
|
|
|
|
|
}; |
621
|
1
|
|
|
|
|
12
|
$@; |
622
|
|
|
|
|
|
|
}; |
623
|
1
|
50
|
33
|
|
|
3
|
print STDERR ("=> die($eval_error)\n") |
624
|
|
|
|
|
|
|
if $debug && $eval_error ne ''; |
625
|
1
|
50
|
|
|
|
5
|
if ( $eval_error =~ /^!/ ) { |
|
|
50
|
|
|
|
|
|
626
|
0
|
0
|
|
|
|
0
|
if ( $eval_error =~ /^!FINISH\b/ ) { |
627
|
0
|
|
|
|
|
0
|
$goon = 0; |
628
|
|
|
|
|
|
|
} |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
elsif ( $eval_error ne '' ) { |
631
|
0
|
|
|
|
|
0
|
warn ($eval_error); |
632
|
0
|
|
|
|
|
0
|
$error++; |
633
|
|
|
|
|
|
|
} |
634
|
|
|
|
|
|
|
} |
635
|
|
|
|
|
|
|
else { |
636
|
0
|
|
|
|
|
0
|
print STDERR ("Invalid REF type \"", ref($linkage{$opt}), |
637
|
|
|
|
|
|
|
"\" in linkage\n"); |
638
|
0
|
|
|
|
|
0
|
die("Getopt::Long -- internal error!\n"); |
639
|
|
|
|
|
|
|
} |
640
|
|
|
|
|
|
|
} |
641
|
|
|
|
|
|
|
# No entry in linkage means entry in userlinkage. |
642
|
|
|
|
|
|
|
elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) { |
643
|
0
|
0
|
|
|
|
0
|
if ( defined $userlinkage->{$opt} ) { |
644
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n") |
645
|
|
|
|
|
|
|
if $debug; |
646
|
0
|
|
|
|
|
0
|
push (@{$userlinkage->{$opt}}, $arg); |
|
0
|
|
|
|
|
0
|
|
647
|
|
|
|
|
|
|
} |
648
|
|
|
|
|
|
|
else { |
649
|
0
|
0
|
|
|
|
0
|
print STDERR ("=>\$L{$opt} = [\"$arg\"]\n") |
650
|
|
|
|
|
|
|
if $debug; |
651
|
0
|
|
|
|
|
0
|
$userlinkage->{$opt} = [$arg]; |
652
|
|
|
|
|
|
|
} |
653
|
|
|
|
|
|
|
} |
654
|
|
|
|
|
|
|
elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { |
655
|
0
|
0
|
|
|
|
0
|
if ( defined $userlinkage->{$opt} ) { |
656
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n") |
657
|
|
|
|
|
|
|
if $debug; |
658
|
0
|
|
|
|
|
0
|
$userlinkage->{$opt}->{$key} = $arg; |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
else { |
661
|
0
|
0
|
|
|
|
0
|
print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n") |
662
|
|
|
|
|
|
|
if $debug; |
663
|
0
|
|
|
|
|
0
|
$userlinkage->{$opt} = {$key => $arg}; |
664
|
|
|
|
|
|
|
} |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
else { |
667
|
5
|
50
|
|
|
|
10
|
if ( $ctl->[CTL_TYPE] eq '+' ) { |
668
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> \$L{$opt} += \"$arg\"\n") |
669
|
|
|
|
|
|
|
if $debug; |
670
|
0
|
0
|
|
|
|
0
|
if ( defined $userlinkage->{$opt} ) { |
671
|
0
|
|
|
|
|
0
|
$userlinkage->{$opt} += $arg; |
672
|
|
|
|
|
|
|
} |
673
|
|
|
|
|
|
|
else { |
674
|
0
|
|
|
|
|
0
|
$userlinkage->{$opt} = $arg; |
675
|
|
|
|
|
|
|
} |
676
|
|
|
|
|
|
|
} |
677
|
|
|
|
|
|
|
else { |
678
|
5
|
50
|
|
|
|
8
|
print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug; |
679
|
5
|
|
|
|
|
8
|
$userlinkage->{$opt} = $arg; |
680
|
|
|
|
|
|
|
} |
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
|
683
|
35
|
|
|
|
|
41
|
$argcnt++; |
684
|
35
|
50
|
33
|
|
|
175
|
last if $argcnt >= $ctl->[CTL_AMAX] && $ctl->[CTL_AMAX] != -1; |
685
|
0
|
|
|
|
|
0
|
undef($arg); |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
# Need more args? |
688
|
0
|
0
|
|
|
|
0
|
if ( $argcnt < $ctl->[CTL_AMIN] ) { |
689
|
0
|
0
|
|
|
|
0
|
if ( @$argv ) { |
690
|
0
|
0
|
|
|
|
0
|
if ( ValidValue($ctl, $argv->[0], 1, $argend, $prefix) ) { |
691
|
0
|
|
|
|
|
0
|
$arg = shift(@$argv); |
692
|
0
|
0
|
|
|
|
0
|
if ( $ctl->[CTL_TYPE] =~ /^[iIo]$/ ) { |
693
|
0
|
|
|
|
|
0
|
$arg =~ tr/_//d; |
694
|
0
|
0
|
0
|
|
|
0
|
$arg = $ctl->[CTL_TYPE] eq 'o' && $arg =~ /^0/ |
695
|
|
|
|
|
|
|
? oct($arg) |
696
|
|
|
|
|
|
|
: 0+$arg |
697
|
|
|
|
|
|
|
} |
698
|
0
|
0
|
|
|
|
0
|
($key,$arg) = $arg =~ /^([^=]+)=(.*)/ |
699
|
|
|
|
|
|
|
if $ctl->[CTL_DEST] == CTL_DEST_HASH; |
700
|
0
|
|
|
|
|
0
|
next; |
701
|
|
|
|
|
|
|
} |
702
|
0
|
|
|
|
|
0
|
warn("Value \"$$argv[0]\" invalid for option $opt\n"); |
703
|
0
|
|
|
|
|
0
|
$error++; |
704
|
|
|
|
|
|
|
} |
705
|
|
|
|
|
|
|
else { |
706
|
0
|
|
|
|
|
0
|
warn("Insufficient arguments for option $opt\n"); |
707
|
0
|
|
|
|
|
0
|
$error++; |
708
|
|
|
|
|
|
|
} |
709
|
|
|
|
|
|
|
} |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
# Any more args? |
712
|
0
|
0
|
0
|
|
|
0
|
if ( @$argv && ValidValue($ctl, $argv->[0], 0, $argend, $prefix) ) { |
713
|
0
|
|
|
|
|
0
|
$arg = shift(@$argv); |
714
|
0
|
0
|
|
|
|
0
|
if ( $ctl->[CTL_TYPE] =~ /^[iIo]$/ ) { |
715
|
0
|
|
|
|
|
0
|
$arg =~ tr/_//d; |
716
|
0
|
0
|
0
|
|
|
0
|
$arg = $ctl->[CTL_TYPE] eq 'o' && $arg =~ /^0/ |
717
|
|
|
|
|
|
|
? oct($arg) |
718
|
|
|
|
|
|
|
: 0+$arg |
719
|
|
|
|
|
|
|
} |
720
|
0
|
0
|
|
|
|
0
|
($key,$arg) = $arg =~ /^([^=]+)=(.*)/ |
721
|
|
|
|
|
|
|
if $ctl->[CTL_DEST] == CTL_DEST_HASH; |
722
|
0
|
|
|
|
|
0
|
next; |
723
|
|
|
|
|
|
|
} |
724
|
|
|
|
|
|
|
} |
725
|
|
|
|
|
|
|
} |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
# Not an option. Save it if we $PERMUTE and don't have a <>. |
728
|
|
|
|
|
|
|
elsif ( $order == $PERMUTE ) { |
729
|
|
|
|
|
|
|
# Try non-options call-back. |
730
|
14
|
|
|
|
|
20
|
my $cb; |
731
|
14
|
100
|
|
|
|
48
|
if ( defined ($cb = $linkage{'<>'}) ) { |
732
|
3
|
50
|
|
|
|
6
|
print STDERR ("=> &L{$tryopt}(\"$tryopt\")\n") |
733
|
|
|
|
|
|
|
if $debug; |
734
|
3
|
|
|
|
|
4
|
my $eval_error = do { |
735
|
3
|
|
|
|
|
3
|
local $@; |
736
|
3
|
|
|
|
|
9
|
local $SIG{__DIE__} = 'DEFAULT'; |
737
|
3
|
|
|
|
|
7
|
eval { |
738
|
|
|
|
|
|
|
# The arg to <> cannot be the CallBack object |
739
|
|
|
|
|
|
|
# since it may be passed to other modules that |
740
|
|
|
|
|
|
|
# get confused (e.g., Archive::Tar). Well, |
741
|
|
|
|
|
|
|
# it's not relevant for this callback anyway. |
742
|
3
|
|
|
|
|
5
|
&$cb($tryopt); |
743
|
|
|
|
|
|
|
}; |
744
|
3
|
|
|
|
|
15
|
$@; |
745
|
|
|
|
|
|
|
}; |
746
|
3
|
50
|
33
|
|
|
8
|
print STDERR ("=> die($eval_error)\n") |
747
|
|
|
|
|
|
|
if $debug && $eval_error ne ''; |
748
|
3
|
50
|
|
|
|
8
|
if ( $eval_error =~ /^!/ ) { |
|
|
50
|
|
|
|
|
|
749
|
0
|
0
|
|
|
|
0
|
if ( $eval_error =~ /^!FINISH\b/ ) { |
750
|
0
|
|
|
|
|
0
|
$goon = 0; |
751
|
|
|
|
|
|
|
} |
752
|
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
elsif ( $eval_error ne '' ) { |
754
|
0
|
|
|
|
|
0
|
warn ($eval_error); |
755
|
0
|
|
|
|
|
0
|
$error++; |
756
|
|
|
|
|
|
|
} |
757
|
|
|
|
|
|
|
} |
758
|
|
|
|
|
|
|
else { |
759
|
11
|
50
|
|
|
|
30
|
print STDERR ("=> saving \"$tryopt\" ", |
760
|
|
|
|
|
|
|
"(not an option, may permute)\n") if $debug; |
761
|
11
|
|
|
|
|
22
|
push (@ret, $tryopt); |
762
|
|
|
|
|
|
|
} |
763
|
14
|
|
|
|
|
49
|
next; |
764
|
|
|
|
|
|
|
} |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
# ...otherwise, terminate. |
767
|
|
|
|
|
|
|
else { |
768
|
|
|
|
|
|
|
# Push this one back and exit. |
769
|
1
|
|
|
|
|
2
|
unshift (@$argv, $tryopt); |
770
|
1
|
|
|
|
|
6
|
return ($error == 0); |
771
|
|
|
|
|
|
|
} |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
} |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
# Finish. |
776
|
19
|
50
|
33
|
|
|
61
|
if ( @ret && ( $order == $PERMUTE || $passthrough ) ) { |
|
|
|
66
|
|
|
|
|
777
|
|
|
|
|
|
|
# Push back accumulated arguments |
778
|
9
|
50
|
|
|
|
20
|
print STDERR ("=> restoring \"", join('" "', @ret), "\"\n") |
779
|
|
|
|
|
|
|
if $debug; |
780
|
9
|
|
|
|
|
17
|
unshift (@$argv, @ret); |
781
|
|
|
|
|
|
|
} |
782
|
|
|
|
|
|
|
|
783
|
19
|
|
|
|
|
96
|
return ($error == 0); |
784
|
|
|
|
|
|
|
} |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
# A readable representation of what's in an optbl. |
787
|
|
|
|
|
|
|
sub OptCtl ($) { |
788
|
0
|
|
|
0
|
0
|
0
|
my ($v) = @_; |
789
|
0
|
0
|
|
|
|
0
|
my @v = map { defined($_) ? ($_) : ("") } @$v; |
|
0
|
|
|
|
|
0
|
|
790
|
0
|
|
0
|
|
|
0
|
"[". |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
791
|
|
|
|
|
|
|
join(",", |
792
|
|
|
|
|
|
|
"\"$v[CTL_TYPE]\"", |
793
|
|
|
|
|
|
|
"\"$v[CTL_CNAME]\"", |
794
|
|
|
|
|
|
|
"\"$v[CTL_DEFAULT]\"", |
795
|
|
|
|
|
|
|
("\$","\@","\%","\&")[$v[CTL_DEST] || 0], |
796
|
|
|
|
|
|
|
$v[CTL_AMIN] || '', |
797
|
|
|
|
|
|
|
$v[CTL_AMAX] || '', |
798
|
|
|
|
|
|
|
# $v[CTL_RANGE] || '', |
799
|
|
|
|
|
|
|
# $v[CTL_REPEAT] || '', |
800
|
|
|
|
|
|
|
). "]"; |
801
|
|
|
|
|
|
|
} |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
# Parse an option specification and fill the tables. |
804
|
|
|
|
|
|
|
sub ParseOptionSpec ($$) { |
805
|
36
|
|
|
36
|
0
|
66
|
my ($opt, $opctl) = @_; |
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
# Match option spec. |
808
|
36
|
50
|
|
|
|
160
|
if ( $opt !~ m;^ |
809
|
|
|
|
|
|
|
( |
810
|
|
|
|
|
|
|
# Option name |
811
|
|
|
|
|
|
|
(?: \w+[-\w]* ) |
812
|
|
|
|
|
|
|
# Aliases |
813
|
|
|
|
|
|
|
(?: \| (?: . [^|!+=:]* )? )* |
814
|
|
|
|
|
|
|
)? |
815
|
|
|
|
|
|
|
( |
816
|
|
|
|
|
|
|
# Either modifiers ... |
817
|
|
|
|
|
|
|
[!+] |
818
|
|
|
|
|
|
|
| |
819
|
|
|
|
|
|
|
# ... or a value/dest/repeat specification |
820
|
|
|
|
|
|
|
[=:] [ionfs] [@%]? (?: \{\d*,?\d*\} )? |
821
|
|
|
|
|
|
|
| |
822
|
|
|
|
|
|
|
# ... or an optional-with-default spec |
823
|
|
|
|
|
|
|
: (?: -?\d+ | \+ ) [@%]? |
824
|
|
|
|
|
|
|
)? |
825
|
|
|
|
|
|
|
$;x ) { |
826
|
0
|
|
|
|
|
0
|
return (undef, "Error in option spec: \"$opt\"\n"); |
827
|
|
|
|
|
|
|
} |
828
|
|
|
|
|
|
|
|
829
|
36
|
|
|
|
|
100
|
my ($names, $spec) = ($1, $2); |
830
|
36
|
100
|
|
|
|
67
|
$spec = '' unless defined $spec; |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
# $orig keeps track of the primary name the user specified. |
833
|
|
|
|
|
|
|
# This name will be used for the internal or external linkage. |
834
|
|
|
|
|
|
|
# In other words, if the user specifies "FoO|BaR", it will |
835
|
|
|
|
|
|
|
# match any case combinations of 'foo' and 'bar', but if a global |
836
|
|
|
|
|
|
|
# variable needs to be set, it will be $opt_FoO in the exact case |
837
|
|
|
|
|
|
|
# as specified. |
838
|
36
|
|
|
|
|
44
|
my $orig; |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
my @names; |
841
|
36
|
50
|
|
|
|
57
|
if ( defined $names ) { |
842
|
36
|
|
|
|
|
94
|
@names = split (/\|/, $names); |
843
|
36
|
|
|
|
|
47
|
$orig = $names[0]; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
else { |
846
|
0
|
|
|
|
|
0
|
@names = (''); |
847
|
0
|
|
|
|
|
0
|
$orig = ''; |
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
# Construct the opctl entries. |
851
|
36
|
|
|
|
|
41
|
my $entry; |
852
|
36
|
100
|
66
|
|
|
192
|
if ( $spec eq '' || $spec eq '+' || $spec eq '!' ) { |
|
|
50
|
100
|
|
|
|
|
853
|
|
|
|
|
|
|
# Fields are hard-wired here. |
854
|
19
|
|
|
|
|
51
|
$entry = [$spec,$orig,undef,CTL_DEST_SCALAR,0,0]; |
855
|
|
|
|
|
|
|
} |
856
|
|
|
|
|
|
|
elsif ( $spec =~ /^:(-?\d+|\+)([@%])?$/ ) { |
857
|
0
|
|
|
|
|
0
|
my $def = $1; |
858
|
0
|
|
|
|
|
0
|
my $dest = $2; |
859
|
0
|
0
|
|
|
|
0
|
my $type = $def eq '+' ? 'I' : 'i'; |
860
|
0
|
|
0
|
|
|
0
|
$dest ||= '$'; |
861
|
0
|
0
|
|
|
|
0
|
$dest = $dest eq '@' ? CTL_DEST_ARRAY |
|
|
0
|
|
|
|
|
|
862
|
|
|
|
|
|
|
: $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR; |
863
|
|
|
|
|
|
|
# Fields are hard-wired here. |
864
|
0
|
0
|
|
|
|
0
|
$entry = [$type,$orig,$def eq '+' ? undef : $def, |
865
|
|
|
|
|
|
|
$dest,0,1]; |
866
|
|
|
|
|
|
|
} |
867
|
|
|
|
|
|
|
else { |
868
|
17
|
|
|
|
|
80
|
my ($mand, $type, $dest) = |
869
|
|
|
|
|
|
|
$spec =~ /^([=:])([ionfs])([@%])?(\{(\d+)?(,)?(\d+)?\})?$/; |
870
|
17
|
50
|
66
|
|
|
52
|
return (undef, "Cannot repeat while bundling: \"$opt\"\n") |
871
|
|
|
|
|
|
|
if $bundling && defined($4); |
872
|
17
|
|
|
|
|
40
|
my ($mi, $cm, $ma) = ($5, $6, $7); |
873
|
17
|
0
|
33
|
|
|
38
|
return (undef, "{0} is useless in option spec: \"$opt\"\n") |
|
|
|
33
|
|
|
|
|
|
|
|
0
|
|
|
|
|
874
|
|
|
|
|
|
|
if defined($mi) && !$mi && !defined($ma) && !defined($cm); |
875
|
|
|
|
|
|
|
|
876
|
17
|
50
|
|
|
|
33
|
$type = 'i' if $type eq 'n'; |
877
|
17
|
|
50
|
|
|
67
|
$dest ||= '$'; |
878
|
17
|
50
|
|
|
|
52
|
$dest = $dest eq '@' ? CTL_DEST_ARRAY |
|
|
50
|
|
|
|
|
|
879
|
|
|
|
|
|
|
: $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR; |
880
|
|
|
|
|
|
|
# Default minargs to 1/0 depending on mand status. |
881
|
17
|
100
|
|
|
|
53
|
$mi = $mand eq '=' ? 1 : 0 unless defined $mi; |
|
|
50
|
|
|
|
|
|
882
|
|
|
|
|
|
|
# Adjust mand status according to minargs. |
883
|
17
|
100
|
|
|
|
34
|
$mand = $mi ? '=' : ':'; |
884
|
|
|
|
|
|
|
# Adjust maxargs. |
885
|
17
|
100
|
33
|
|
|
75
|
$ma = $mi ? $mi : 1 unless defined $ma || defined $cm; |
|
|
50
|
|
|
|
|
|
886
|
17
|
50
|
33
|
|
|
50
|
return (undef, "Max must be greater than zero in option spec: \"$opt\"\n") |
887
|
|
|
|
|
|
|
if defined($ma) && !$ma; |
888
|
17
|
50
|
33
|
|
|
58
|
return (undef, "Max less than min in option spec: \"$opt\"\n") |
889
|
|
|
|
|
|
|
if defined($ma) && $ma < $mi; |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
# Fields are hard-wired here. |
892
|
17
|
|
50
|
|
|
80
|
$entry = [$type,$orig,undef,$dest,$mi,$ma||-1]; |
893
|
|
|
|
|
|
|
} |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
# Process all names. First is canonical, the rest are aliases. |
896
|
36
|
|
|
|
|
50
|
my $dups = ''; |
897
|
36
|
|
|
|
|
85
|
foreach ( @names ) { |
898
|
|
|
|
|
|
|
|
899
|
36
|
50
|
66
|
|
|
116
|
$_ = lc ($_) |
|
|
100
|
|
|
|
|
|
900
|
|
|
|
|
|
|
if $ignorecase > (($bundling && length($_) == 1) ? 1 : 0); |
901
|
|
|
|
|
|
|
|
902
|
36
|
50
|
|
|
|
72
|
if ( exists $opctl->{$_} ) { |
903
|
0
|
|
|
|
|
0
|
$dups .= "Duplicate specification \"$opt\" for option \"$_\"\n"; |
904
|
|
|
|
|
|
|
} |
905
|
|
|
|
|
|
|
|
906
|
36
|
100
|
|
|
|
61
|
if ( $spec eq '!' ) { |
907
|
4
|
|
|
|
|
10
|
$opctl->{"no$_"} = $entry; |
908
|
4
|
|
|
|
|
7
|
$opctl->{"no-$_"} = $entry; |
909
|
4
|
|
|
|
|
26
|
$opctl->{$_} = [@$entry]; |
910
|
4
|
|
|
|
|
11
|
$opctl->{$_}->[CTL_TYPE] = ''; |
911
|
|
|
|
|
|
|
} |
912
|
|
|
|
|
|
|
else { |
913
|
32
|
|
|
|
|
64
|
$opctl->{$_} = $entry; |
914
|
|
|
|
|
|
|
} |
915
|
|
|
|
|
|
|
} |
916
|
|
|
|
|
|
|
|
917
|
36
|
0
|
33
|
|
|
105
|
if ( $dups && $^W ) { |
918
|
0
|
|
|
|
|
0
|
foreach ( split(/\n+/, $dups) ) { |
919
|
0
|
|
|
|
|
0
|
warn($_."\n"); |
920
|
|
|
|
|
|
|
} |
921
|
|
|
|
|
|
|
} |
922
|
36
|
|
|
|
|
137
|
($names[0], $orig); |
923
|
|
|
|
|
|
|
} |
924
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
# Option lookup. |
926
|
|
|
|
|
|
|
sub FindOption ($$$$$) { |
927
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
# returns (1, $opt, $ctl, $starter, $arg, $key) if okay, |
929
|
|
|
|
|
|
|
# returns (1, undef) if option in error, |
930
|
|
|
|
|
|
|
# returns (0) otherwise. |
931
|
|
|
|
|
|
|
|
932
|
51
|
|
|
51
|
0
|
105
|
my ($argv, $prefix, $argend, $opt, $opctl) = @_; |
933
|
|
|
|
|
|
|
|
934
|
51
|
50
|
|
|
|
76
|
print STDERR ("=> find \"$opt\"\n") if $debug; |
935
|
|
|
|
|
|
|
|
936
|
51
|
50
|
|
|
|
83
|
return (0) unless defined($opt); |
937
|
51
|
100
|
|
|
|
415
|
return (0) unless $opt =~ /^($prefix)(.*)$/s; |
938
|
39
|
50
|
33
|
|
|
101
|
return (0) if $opt eq "-" && !defined $opctl->{''}; |
939
|
|
|
|
|
|
|
|
940
|
39
|
|
|
|
|
102
|
$opt = substr( $opt, length($1) ); # retain taintedness |
941
|
39
|
|
|
|
|
55
|
my $starter = $1; |
942
|
|
|
|
|
|
|
|
943
|
39
|
50
|
|
|
|
69
|
print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug; |
944
|
|
|
|
|
|
|
|
945
|
39
|
|
|
|
|
50
|
my $optarg; # value supplied with --opt=value |
946
|
|
|
|
|
|
|
my $rest; # remainder from unbundling |
947
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
# If it is a long option, it may include the value. |
949
|
|
|
|
|
|
|
# With getopt_compat, only if not bundling. |
950
|
39
|
100
|
100
|
|
|
372
|
if ( ($starter=~/^$longprefix$/ |
|
|
|
100
|
|
|
|
|
951
|
|
|
|
|
|
|
|| ($getopt_compat && ($bundling == 0 || $bundling == 2))) |
952
|
|
|
|
|
|
|
&& (my $oppos = index($opt, '=', 1)) > 0) { |
953
|
3
|
|
|
|
|
6
|
my $optorg = $opt; |
954
|
3
|
|
|
|
|
8
|
$opt = substr($optorg, 0, $oppos); |
955
|
3
|
|
|
|
|
5
|
$optarg = substr($optorg, $oppos + 1); # retain tainedness |
956
|
3
|
50
|
|
|
|
8
|
print STDERR ("=> option \"", $opt, |
957
|
|
|
|
|
|
|
"\", optarg = \"$optarg\"\n") if $debug; |
958
|
|
|
|
|
|
|
} |
959
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
#### Look it up ### |
961
|
|
|
|
|
|
|
|
962
|
39
|
|
|
|
|
57
|
my $tryopt = $opt; # option to try |
963
|
|
|
|
|
|
|
|
964
|
39
|
50
|
66
|
|
|
226
|
if ( ( $bundling || $bundling_values ) && $starter eq '-' ) { |
|
|
50
|
66
|
|
|
|
|
|
|
0
|
33
|
|
|
|
|
965
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
# To try overrides, obey case ignore. |
967
|
0
|
0
|
|
|
|
0
|
$tryopt = $ignorecase ? lc($opt) : $opt; |
968
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
# If bundling == 2, long options can override bundles. |
970
|
0
|
0
|
0
|
|
|
0
|
if ( $bundling == 2 && length($tryopt) > 1 |
|
|
0
|
0
|
|
|
|
|
971
|
|
|
|
|
|
|
&& defined ($opctl->{$tryopt}) ) { |
972
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> $starter$tryopt overrides unbundling\n") |
973
|
|
|
|
|
|
|
if $debug; |
974
|
|
|
|
|
|
|
} |
975
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
# If bundling_values, option may be followed by the value. |
977
|
|
|
|
|
|
|
elsif ( $bundling_values ) { |
978
|
0
|
|
|
|
|
0
|
$tryopt = $opt; |
979
|
|
|
|
|
|
|
# Unbundle single letter option. |
980
|
0
|
0
|
|
|
|
0
|
$rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : ''; |
981
|
0
|
|
|
|
|
0
|
$tryopt = substr ($tryopt, 0, 1); |
982
|
0
|
0
|
|
|
|
0
|
$tryopt = lc ($tryopt) if $ignorecase > 1; |
983
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> $starter$tryopt unbundled from ", |
984
|
|
|
|
|
|
|
"$starter$tryopt$rest\n") if $debug; |
985
|
|
|
|
|
|
|
# Whatever remains may not be considered an option. |
986
|
0
|
0
|
|
|
|
0
|
$optarg = $rest eq '' ? undef : $rest; |
987
|
0
|
|
|
|
|
0
|
$rest = undef; |
988
|
|
|
|
|
|
|
} |
989
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
# Split off a single letter and leave the rest for |
991
|
|
|
|
|
|
|
# further processing. |
992
|
|
|
|
|
|
|
else { |
993
|
0
|
|
|
|
|
0
|
$tryopt = $opt; |
994
|
|
|
|
|
|
|
# Unbundle single letter option. |
995
|
0
|
0
|
|
|
|
0
|
$rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : ''; |
996
|
0
|
|
|
|
|
0
|
$tryopt = substr ($tryopt, 0, 1); |
997
|
0
|
0
|
|
|
|
0
|
$tryopt = lc ($tryopt) if $ignorecase > 1; |
998
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> $starter$tryopt unbundled from ", |
999
|
|
|
|
|
|
|
"$starter$tryopt$rest\n") if $debug; |
1000
|
0
|
0
|
|
|
|
0
|
$rest = undef unless $rest ne ''; |
1001
|
|
|
|
|
|
|
} |
1002
|
|
|
|
|
|
|
} |
1003
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
# Try auto-abbreviation. |
1005
|
|
|
|
|
|
|
elsif ( $autoabbrev && $opt ne "" ) { |
1006
|
|
|
|
|
|
|
# Sort the possible long option names. |
1007
|
39
|
|
|
|
|
161
|
my @names = sort(keys (%$opctl)); |
1008
|
|
|
|
|
|
|
# Downcase if allowed. |
1009
|
39
|
100
|
|
|
|
104
|
$opt = lc ($opt) if $ignorecase; |
1010
|
39
|
|
|
|
|
60
|
$tryopt = $opt; |
1011
|
|
|
|
|
|
|
# Turn option name into pattern. |
1012
|
39
|
|
|
|
|
58
|
my $pat = quotemeta ($opt); |
1013
|
|
|
|
|
|
|
# Look up in option names. |
1014
|
39
|
|
|
|
|
518
|
my @hits = grep (/^$pat/, @names); |
1015
|
39
|
50
|
|
|
|
98
|
print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ", |
1016
|
|
|
|
|
|
|
"out of ", scalar(@names), "\n") if $debug; |
1017
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
# Check for ambiguous results. |
1019
|
39
|
50
|
33
|
|
|
86
|
unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) { |
1020
|
|
|
|
|
|
|
# See if all matches are for the same option. |
1021
|
0
|
|
|
|
|
0
|
my %hit; |
1022
|
0
|
|
|
|
|
0
|
foreach ( @hits ) { |
1023
|
|
|
|
|
|
|
my $hit = $opctl->{$_}->[CTL_CNAME] |
1024
|
0
|
0
|
|
|
|
0
|
if defined $opctl->{$_}->[CTL_CNAME]; |
1025
|
0
|
0
|
|
|
|
0
|
$hit = "no" . $hit if $opctl->{$_}->[CTL_TYPE] eq '!'; |
1026
|
0
|
|
|
|
|
0
|
$hit{$hit} = 1; |
1027
|
|
|
|
|
|
|
} |
1028
|
|
|
|
|
|
|
# Remove auto-supplied options (version, help). |
1029
|
0
|
0
|
|
|
|
0
|
if ( keys(%hit) == 2 ) { |
1030
|
0
|
0
|
0
|
|
|
0
|
if ( $auto_version && exists($hit{version}) ) { |
|
|
0
|
0
|
|
|
|
|
1031
|
0
|
|
|
|
|
0
|
delete $hit{version}; |
1032
|
|
|
|
|
|
|
} |
1033
|
|
|
|
|
|
|
elsif ( $auto_help && exists($hit{help}) ) { |
1034
|
0
|
|
|
|
|
0
|
delete $hit{help}; |
1035
|
|
|
|
|
|
|
} |
1036
|
|
|
|
|
|
|
} |
1037
|
|
|
|
|
|
|
# Now see if it really is ambiguous. |
1038
|
0
|
0
|
|
|
|
0
|
unless ( keys(%hit) == 1 ) { |
1039
|
0
|
0
|
|
|
|
0
|
return (0) if $passthrough; |
1040
|
0
|
|
|
|
|
0
|
warn ("Option ", $opt, " is ambiguous (", |
1041
|
|
|
|
|
|
|
join(", ", @hits), ")\n"); |
1042
|
0
|
|
|
|
|
0
|
$error++; |
1043
|
0
|
|
|
|
|
0
|
return (1, undef); |
1044
|
|
|
|
|
|
|
} |
1045
|
0
|
|
|
|
|
0
|
@hits = keys(%hit); |
1046
|
|
|
|
|
|
|
} |
1047
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
# Complete the option name, if appropriate. |
1049
|
39
|
50
|
66
|
|
|
142
|
if ( @hits == 1 && $hits[0] ne $opt ) { |
1050
|
0
|
|
|
|
|
0
|
$tryopt = $hits[0]; |
1051
|
0
|
0
|
0
|
|
|
0
|
$tryopt = lc ($tryopt) |
|
|
0
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
if $ignorecase > (($bundling && length($tryopt) == 1) ? 1 : 0); |
1053
|
0
|
0
|
|
|
|
0
|
print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n") |
1054
|
|
|
|
|
|
|
if $debug; |
1055
|
|
|
|
|
|
|
} |
1056
|
|
|
|
|
|
|
} |
1057
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
# Map to all lowercase if ignoring case. |
1059
|
|
|
|
|
|
|
elsif ( $ignorecase ) { |
1060
|
0
|
|
|
|
|
0
|
$tryopt = lc ($opt); |
1061
|
|
|
|
|
|
|
} |
1062
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
# Check validity by fetching the info. |
1064
|
39
|
|
|
|
|
66
|
my $ctl = $opctl->{$tryopt}; |
1065
|
39
|
100
|
|
|
|
74
|
unless ( defined $ctl ) { |
1066
|
4
|
100
|
|
|
|
12
|
return (0) if $passthrough; |
1067
|
|
|
|
|
|
|
# Pretend one char when bundling. |
1068
|
1
|
50
|
33
|
|
|
5
|
if ( $bundling == 1 && length($starter) == 1 ) { |
1069
|
1
|
|
|
|
|
3
|
$opt = substr($opt,0,1); |
1070
|
1
|
50
|
|
|
|
2
|
unshift (@$argv, $starter.$rest) if defined $rest; |
1071
|
|
|
|
|
|
|
} |
1072
|
1
|
50
|
|
|
|
3
|
if ( $opt eq "" ) { |
1073
|
0
|
|
|
|
|
0
|
warn ("Missing option after ", $starter, "\n"); |
1074
|
|
|
|
|
|
|
} |
1075
|
|
|
|
|
|
|
else { |
1076
|
1
|
|
|
|
|
11
|
warn ("Unknown option: ", $opt, "\n"); |
1077
|
|
|
|
|
|
|
} |
1078
|
1
|
|
|
|
|
7
|
$error++; |
1079
|
1
|
|
|
|
|
3
|
return (1, undef); |
1080
|
|
|
|
|
|
|
} |
1081
|
|
|
|
|
|
|
# Apparently valid. |
1082
|
35
|
|
|
|
|
49
|
$opt = $tryopt; |
1083
|
35
|
50
|
|
|
|
52
|
print STDERR ("=> found ", OptCtl($ctl), |
1084
|
|
|
|
|
|
|
" for \"", $opt, "\"\n") if $debug; |
1085
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
#### Determine argument status #### |
1087
|
|
|
|
|
|
|
|
1088
|
|
|
|
|
|
|
# If it is an option w/o argument, we're almost finished with it. |
1089
|
35
|
|
|
|
|
50
|
my $type = $ctl->[CTL_TYPE]; |
1090
|
35
|
|
|
|
|
46
|
my $arg; |
1091
|
|
|
|
|
|
|
|
1092
|
35
|
100
|
100
|
|
|
125
|
if ( $type eq '' || $type eq '!' || $type eq '+' ) { |
|
|
|
66
|
|
|
|
|
1093
|
18
|
50
|
66
|
|
|
61
|
if ( defined $optarg ) { |
|
|
100
|
|
|
|
|
|
1094
|
0
|
0
|
|
|
|
0
|
return (0) if $passthrough; |
1095
|
0
|
|
|
|
|
0
|
warn ("Option ", $opt, " does not take an argument\n"); |
1096
|
0
|
|
|
|
|
0
|
$error++; |
1097
|
0
|
|
|
|
|
0
|
undef $opt; |
1098
|
0
|
0
|
|
|
|
0
|
undef $optarg if $bundling_values; |
1099
|
|
|
|
|
|
|
} |
1100
|
|
|
|
|
|
|
elsif ( $type eq '' || $type eq '+' ) { |
1101
|
|
|
|
|
|
|
# Supply explicit value. |
1102
|
15
|
|
|
|
|
21
|
$arg = 1; |
1103
|
|
|
|
|
|
|
} |
1104
|
|
|
|
|
|
|
else { |
1105
|
3
|
|
|
|
|
10
|
$opt =~ s/^no-?//i; # strip NO prefix |
1106
|
3
|
|
|
|
|
6
|
$arg = 0; # supply explicit value |
1107
|
|
|
|
|
|
|
} |
1108
|
18
|
50
|
|
|
|
55
|
unshift (@$argv, $starter.$rest) if defined $rest; |
1109
|
18
|
|
|
|
|
101
|
return (1, $opt, $ctl, $starter, $arg); |
1110
|
|
|
|
|
|
|
} |
1111
|
|
|
|
|
|
|
|
1112
|
|
|
|
|
|
|
# Get mandatory status and type info. |
1113
|
17
|
|
|
|
|
27
|
my $mand = $ctl->[CTL_AMIN]; |
1114
|
|
|
|
|
|
|
|
1115
|
|
|
|
|
|
|
# Check if there is an option argument available. |
1116
|
17
|
50
|
|
|
|
30
|
if ( $gnu_compat ) { |
1117
|
0
|
|
|
|
|
0
|
my $optargtype = 0; # none, 1 = empty, 2 = nonempty, 3 = aux |
1118
|
0
|
0
|
0
|
|
|
0
|
if ( defined($optarg) ) { |
|
|
0
|
|
|
|
|
|
1119
|
0
|
0
|
|
|
|
0
|
$optargtype = (length($optarg) == 0) ? 1 : 2; |
1120
|
|
|
|
|
|
|
} |
1121
|
|
|
|
|
|
|
elsif ( defined $rest || @$argv > 0 ) { |
1122
|
|
|
|
|
|
|
# GNU getopt_long() does not accept the (optional) |
1123
|
|
|
|
|
|
|
# argument to be passed to the option without = sign. |
1124
|
|
|
|
|
|
|
# We do, since not doing so breaks existing scripts. |
1125
|
0
|
|
|
|
|
0
|
$optargtype = 3; |
1126
|
|
|
|
|
|
|
} |
1127
|
0
|
0
|
0
|
|
|
0
|
if(($optargtype == 0) && !$mand) { |
1128
|
0
|
0
|
|
|
|
0
|
if ( $type eq 'I' ) { |
1129
|
|
|
|
|
|
|
# Fake incremental type. |
1130
|
0
|
|
|
|
|
0
|
my @c = @$ctl; |
1131
|
0
|
|
|
|
|
0
|
$c[CTL_TYPE] = '+'; |
1132
|
0
|
|
|
|
|
0
|
return (1, $opt, \@c, $starter, 1); |
1133
|
|
|
|
|
|
|
} |
1134
|
0
|
0
|
|
|
|
0
|
my $val |
|
|
0
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
= defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] |
1136
|
|
|
|
|
|
|
: $type eq 's' ? '' |
1137
|
|
|
|
|
|
|
: 0; |
1138
|
0
|
|
|
|
|
0
|
return (1, $opt, $ctl, $starter, $val); |
1139
|
|
|
|
|
|
|
} |
1140
|
0
|
0
|
|
|
|
0
|
return (1, $opt, $ctl, $starter, $type eq 's' ? '' : 0) |
|
|
0
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
if $optargtype == 1; # --foo= -> return nothing |
1142
|
|
|
|
|
|
|
} |
1143
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
# Check if there is an option argument available. |
1145
|
17
|
100
|
33
|
|
|
84
|
if ( defined $optarg |
|
|
50
|
|
|
|
|
|
1146
|
|
|
|
|
|
|
? ($optarg eq '') |
1147
|
|
|
|
|
|
|
: !(defined $rest || @$argv > 0) ) { |
1148
|
|
|
|
|
|
|
# Complain if this option needs an argument. |
1149
|
|
|
|
|
|
|
# if ( $mand && !($type eq 's' ? defined($optarg) : 0) ) { |
1150
|
0
|
0
|
0
|
|
|
0
|
if ( $mand || $ctl->[CTL_DEST] == CTL_DEST_HASH ) { |
1151
|
0
|
0
|
|
|
|
0
|
return (0) if $passthrough; |
1152
|
0
|
|
|
|
|
0
|
warn ("Option ", $opt, " requires an argument\n"); |
1153
|
0
|
|
|
|
|
0
|
$error++; |
1154
|
0
|
|
|
|
|
0
|
return (1, undef); |
1155
|
|
|
|
|
|
|
} |
1156
|
0
|
0
|
|
|
|
0
|
if ( $type eq 'I' ) { |
1157
|
|
|
|
|
|
|
# Fake incremental type. |
1158
|
0
|
|
|
|
|
0
|
my @c = @$ctl; |
1159
|
0
|
|
|
|
|
0
|
$c[CTL_TYPE] = '+'; |
1160
|
0
|
|
|
|
|
0
|
return (1, $opt, \@c, $starter, 1); |
1161
|
|
|
|
|
|
|
} |
1162
|
0
|
0
|
|
|
|
0
|
return (1, $opt, $ctl, $starter, |
|
|
0
|
|
|
|
|
|
1163
|
|
|
|
|
|
|
defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : |
1164
|
|
|
|
|
|
|
$type eq 's' ? '' : 0); |
1165
|
|
|
|
|
|
|
} |
1166
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
# Get (possibly optional) argument. |
1168
|
17
|
100
|
|
|
|
64
|
$arg = (defined $rest ? $rest |
|
|
50
|
|
|
|
|
|
1169
|
|
|
|
|
|
|
: (defined $optarg ? $optarg : shift (@$argv))); |
1170
|
|
|
|
|
|
|
|
1171
|
|
|
|
|
|
|
# Get key if this is a "name=value" pair for a hash option. |
1172
|
17
|
|
|
|
|
31
|
my $key; |
1173
|
17
|
50
|
33
|
|
|
46
|
if ($ctl->[CTL_DEST] == CTL_DEST_HASH && defined $arg) { |
1174
|
0
|
0
|
|
|
|
0
|
($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1175
|
|
|
|
|
|
|
: ($arg, defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : |
1176
|
|
|
|
|
|
|
($mand ? undef : ($type eq 's' ? "" : 1))); |
1177
|
0
|
0
|
|
|
|
0
|
if (! defined $arg) { |
1178
|
0
|
|
|
|
|
0
|
warn ("Option $opt, key \"$key\", requires a value\n"); |
1179
|
0
|
|
|
|
|
0
|
$error++; |
1180
|
|
|
|
|
|
|
# Push back. |
1181
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, $starter.$rest) if defined $rest; |
1182
|
0
|
|
|
|
|
0
|
return (1, undef); |
1183
|
|
|
|
|
|
|
} |
1184
|
|
|
|
|
|
|
} |
1185
|
|
|
|
|
|
|
|
1186
|
|
|
|
|
|
|
#### Check if the argument is valid for this option #### |
1187
|
|
|
|
|
|
|
|
1188
|
17
|
50
|
|
|
|
35
|
my $key_valid = $ctl->[CTL_DEST] == CTL_DEST_HASH ? "[^=]+=" : ""; |
1189
|
|
|
|
|
|
|
|
1190
|
17
|
100
|
33
|
|
|
51
|
if ( $type eq 's' ) { # string |
|
|
50
|
33
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1191
|
|
|
|
|
|
|
# A mandatory string takes anything. |
1192
|
15
|
100
|
|
|
|
74
|
return (1, $opt, $ctl, $starter, $arg, $key) if $mand; |
1193
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
# Same for optional string as a hash value |
1195
|
1
|
50
|
|
|
|
5
|
return (1, $opt, $ctl, $starter, $arg, $key) |
1196
|
|
|
|
|
|
|
if $ctl->[CTL_DEST] == CTL_DEST_HASH; |
1197
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
# An optional string takes almost anything. |
1199
|
1
|
50
|
33
|
|
|
6
|
return (1, $opt, $ctl, $starter, $arg, $key) |
1200
|
|
|
|
|
|
|
if defined $optarg || defined $rest; |
1201
|
1
|
50
|
|
|
|
3
|
return (1, $opt, $ctl, $starter, $arg, $key) if $arg eq "-"; # ?? |
1202
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
# Check for option or option list terminator. |
1204
|
1
|
50
|
33
|
|
|
37
|
if ($arg eq $argend || |
1205
|
|
|
|
|
|
|
$arg =~ /^$prefix.+/) { |
1206
|
|
|
|
|
|
|
# Push back. |
1207
|
0
|
|
|
|
|
0
|
unshift (@$argv, $arg); |
1208
|
|
|
|
|
|
|
# Supply empty value. |
1209
|
0
|
|
|
|
|
0
|
$arg = ''; |
1210
|
|
|
|
|
|
|
} |
1211
|
|
|
|
|
|
|
} |
1212
|
|
|
|
|
|
|
|
1213
|
|
|
|
|
|
|
elsif ( $type eq 'i' # numeric/integer |
1214
|
|
|
|
|
|
|
|| $type eq 'I' # numeric/integer w/ incr default |
1215
|
|
|
|
|
|
|
|| $type eq 'o' ) { # dec/oct/hex/bin value |
1216
|
|
|
|
|
|
|
|
1217
|
2
|
50
|
|
|
|
6
|
my $o_valid = $type eq 'o' ? PAT_XINT : PAT_INT; |
1218
|
|
|
|
|
|
|
|
1219
|
2
|
50
|
33
|
|
|
80
|
if ( $bundling && defined $rest |
|
|
50
|
33
|
|
|
|
|
1220
|
|
|
|
|
|
|
&& $rest =~ /^($key_valid)($o_valid)(.*)$/si ) { |
1221
|
0
|
|
|
|
|
0
|
($key, $arg, $rest) = ($1, $2, $+); |
1222
|
0
|
0
|
|
|
|
0
|
chop($key) if $key; |
1223
|
0
|
0
|
0
|
|
|
0
|
$arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg; |
1224
|
0
|
0
|
0
|
|
|
0
|
unshift (@$argv, $starter.$rest) if defined $rest && $rest ne ''; |
1225
|
|
|
|
|
|
|
} |
1226
|
|
|
|
|
|
|
elsif ( $arg =~ /^$o_valid$/si ) { |
1227
|
2
|
|
|
|
|
8
|
$arg =~ tr/_//d; |
1228
|
2
|
50
|
33
|
|
|
16
|
$arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg; |
1229
|
|
|
|
|
|
|
} |
1230
|
|
|
|
|
|
|
else { |
1231
|
0
|
0
|
0
|
|
|
0
|
if ( defined $optarg || $mand ) { |
1232
|
0
|
0
|
|
|
|
0
|
if ( $passthrough ) { |
1233
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, defined $rest ? $starter.$rest : $arg) |
|
|
0
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
unless defined $optarg; |
1235
|
0
|
|
|
|
|
0
|
return (0); |
1236
|
|
|
|
|
|
|
} |
1237
|
0
|
0
|
|
|
|
0
|
warn ("Value \"", $arg, "\" invalid for option ", |
1238
|
|
|
|
|
|
|
$opt, " (", |
1239
|
|
|
|
|
|
|
$type eq 'o' ? "extended " : '', |
1240
|
|
|
|
|
|
|
"number expected)\n"); |
1241
|
0
|
|
|
|
|
0
|
$error++; |
1242
|
|
|
|
|
|
|
# Push back. |
1243
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, $starter.$rest) if defined $rest; |
1244
|
0
|
|
|
|
|
0
|
return (1, undef); |
1245
|
|
|
|
|
|
|
} |
1246
|
|
|
|
|
|
|
else { |
1247
|
|
|
|
|
|
|
# Push back. |
1248
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, defined $rest ? $starter.$rest : $arg); |
1249
|
0
|
0
|
|
|
|
0
|
if ( $type eq 'I' ) { |
1250
|
|
|
|
|
|
|
# Fake incremental type. |
1251
|
0
|
|
|
|
|
0
|
my @c = @$ctl; |
1252
|
0
|
|
|
|
|
0
|
$c[CTL_TYPE] = '+'; |
1253
|
0
|
|
|
|
|
0
|
return (1, $opt, \@c, $starter, 1); |
1254
|
|
|
|
|
|
|
} |
1255
|
|
|
|
|
|
|
# Supply default value. |
1256
|
0
|
0
|
|
|
|
0
|
$arg = defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : 0; |
1257
|
|
|
|
|
|
|
} |
1258
|
|
|
|
|
|
|
} |
1259
|
|
|
|
|
|
|
} |
1260
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
elsif ( $type eq 'f' ) { # real number, int is also ok |
1262
|
0
|
|
|
|
|
0
|
my $o_valid = PAT_FLOAT; |
1263
|
0
|
0
|
0
|
|
|
0
|
if ( $bundling && defined $rest && |
|
|
0
|
0
|
|
|
|
|
1264
|
|
|
|
|
|
|
$rest =~ /^($key_valid)($o_valid)(.*)$/s ) { |
1265
|
0
|
|
|
|
|
0
|
$arg =~ tr/_//d; |
1266
|
0
|
|
|
|
|
0
|
($key, $arg, $rest) = ($1, $2, $+); |
1267
|
0
|
0
|
|
|
|
0
|
chop($key) if $key; |
1268
|
0
|
0
|
0
|
|
|
0
|
unshift (@$argv, $starter.$rest) if defined $rest && $rest ne ''; |
1269
|
|
|
|
|
|
|
} |
1270
|
|
|
|
|
|
|
elsif ( $arg =~ /^$o_valid$/ ) { |
1271
|
0
|
|
|
|
|
0
|
$arg =~ tr/_//d; |
1272
|
|
|
|
|
|
|
} |
1273
|
|
|
|
|
|
|
else { |
1274
|
0
|
0
|
0
|
|
|
0
|
if ( defined $optarg || $mand ) { |
1275
|
0
|
0
|
|
|
|
0
|
if ( $passthrough ) { |
1276
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, defined $rest ? $starter.$rest : $arg) |
|
|
0
|
|
|
|
|
|
1277
|
|
|
|
|
|
|
unless defined $optarg; |
1278
|
0
|
|
|
|
|
0
|
return (0); |
1279
|
|
|
|
|
|
|
} |
1280
|
0
|
|
|
|
|
0
|
warn ("Value \"", $arg, "\" invalid for option ", |
1281
|
|
|
|
|
|
|
$opt, " (real number expected)\n"); |
1282
|
0
|
|
|
|
|
0
|
$error++; |
1283
|
|
|
|
|
|
|
# Push back. |
1284
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, $starter.$rest) if defined $rest; |
1285
|
0
|
|
|
|
|
0
|
return (1, undef); |
1286
|
|
|
|
|
|
|
} |
1287
|
|
|
|
|
|
|
else { |
1288
|
|
|
|
|
|
|
# Push back. |
1289
|
0
|
0
|
|
|
|
0
|
unshift (@$argv, defined $rest ? $starter.$rest : $arg); |
1290
|
|
|
|
|
|
|
# Supply default value. |
1291
|
0
|
|
|
|
|
0
|
$arg = 0.0; |
1292
|
|
|
|
|
|
|
} |
1293
|
|
|
|
|
|
|
} |
1294
|
|
|
|
|
|
|
} |
1295
|
|
|
|
|
|
|
else { |
1296
|
0
|
|
|
|
|
0
|
die("Getopt::Long internal error (Can't happen)\n"); |
1297
|
|
|
|
|
|
|
} |
1298
|
3
|
|
|
|
|
19
|
return (1, $opt, $ctl, $starter, $arg, $key); |
1299
|
|
|
|
|
|
|
} |
1300
|
|
|
|
|
|
|
|
1301
|
|
|
|
|
|
|
sub ValidValue ($$$$$) { |
1302
|
0
|
|
|
0
|
0
|
0
|
my ($ctl, $arg, $mand, $argend, $prefix) = @_; |
1303
|
|
|
|
|
|
|
|
1304
|
0
|
0
|
|
|
|
0
|
if ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { |
1305
|
0
|
0
|
|
|
|
0
|
return 0 unless $arg =~ /[^=]+=(.*)/; |
1306
|
0
|
|
|
|
|
0
|
$arg = $1; |
1307
|
|
|
|
|
|
|
} |
1308
|
|
|
|
|
|
|
|
1309
|
0
|
|
|
|
|
0
|
my $type = $ctl->[CTL_TYPE]; |
1310
|
|
|
|
|
|
|
|
1311
|
0
|
0
|
0
|
|
|
0
|
if ( $type eq 's' ) { # string |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1312
|
|
|
|
|
|
|
# A mandatory string takes anything. |
1313
|
0
|
0
|
|
|
|
0
|
return (1) if $mand; |
1314
|
|
|
|
|
|
|
|
1315
|
0
|
0
|
|
|
|
0
|
return (1) if $arg eq "-"; |
1316
|
|
|
|
|
|
|
|
1317
|
|
|
|
|
|
|
# Check for option or option list terminator. |
1318
|
0
|
0
|
0
|
|
|
0
|
return 0 if $arg eq $argend || $arg =~ /^$prefix.+/; |
1319
|
0
|
|
|
|
|
0
|
return 1; |
1320
|
|
|
|
|
|
|
} |
1321
|
|
|
|
|
|
|
|
1322
|
|
|
|
|
|
|
elsif ( $type eq 'i' # numeric/integer |
1323
|
|
|
|
|
|
|
|| $type eq 'I' # numeric/integer w/ incr default |
1324
|
|
|
|
|
|
|
|| $type eq 'o' ) { # dec/oct/hex/bin value |
1325
|
|
|
|
|
|
|
|
1326
|
0
|
0
|
|
|
|
0
|
my $o_valid = $type eq 'o' ? PAT_XINT : PAT_INT; |
1327
|
0
|
|
|
|
|
0
|
return $arg =~ /^$o_valid$/si; |
1328
|
|
|
|
|
|
|
} |
1329
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
elsif ( $type eq 'f' ) { # real number, int is also ok |
1331
|
0
|
|
|
|
|
0
|
my $o_valid = PAT_FLOAT; |
1332
|
0
|
|
|
|
|
0
|
return $arg =~ /^$o_valid$/; |
1333
|
|
|
|
|
|
|
} |
1334
|
0
|
|
|
|
|
0
|
die("ValidValue: Cannot happen\n"); |
1335
|
|
|
|
|
|
|
} |
1336
|
|
|
|
|
|
|
|
1337
|
|
|
|
|
|
|
# Getopt::Long Configuration. |
1338
|
|
|
|
|
|
|
sub Configure (@) { |
1339
|
25
|
|
|
25
|
|
187
|
my (@options) = @_; |
1340
|
|
|
|
|
|
|
|
1341
|
25
|
|
|
|
|
85
|
my $prevconfig = |
1342
|
|
|
|
|
|
|
[ $error, $debug, $major_version, $minor_version, $caller, |
1343
|
|
|
|
|
|
|
$autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, |
1344
|
|
|
|
|
|
|
$gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help, |
1345
|
|
|
|
|
|
|
$longprefix, $bundling_values ]; |
1346
|
|
|
|
|
|
|
|
1347
|
25
|
100
|
|
|
|
61
|
if ( ref($options[0]) eq 'ARRAY' ) { |
1348
|
|
|
|
|
|
|
( $error, $debug, $major_version, $minor_version, $caller, |
1349
|
|
|
|
|
|
|
$autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, |
1350
|
|
|
|
|
|
|
$gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help, |
1351
|
6
|
|
|
|
|
7
|
$longprefix, $bundling_values ) = @{shift(@options)}; |
|
6
|
|
|
|
|
18
|
|
1352
|
|
|
|
|
|
|
} |
1353
|
|
|
|
|
|
|
|
1354
|
25
|
|
|
|
|
30
|
my $opt; |
1355
|
25
|
|
|
|
|
40
|
foreach $opt ( @options ) { |
1356
|
26
|
|
|
|
|
46
|
my $try = lc ($opt); |
1357
|
26
|
|
|
|
|
35
|
my $action = 1; |
1358
|
26
|
100
|
|
|
|
58
|
if ( $try =~ /^no_?(.*)$/s ) { |
1359
|
6
|
|
|
|
|
9
|
$action = 0; |
1360
|
6
|
|
|
|
|
16
|
$try = $+; |
1361
|
|
|
|
|
|
|
} |
1362
|
26
|
100
|
66
|
|
|
334
|
if ( ($try eq 'default' or $try eq 'defaults') && $action ) { |
|
|
50
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
|
|
50
|
66
|
|
|
|
|
|
|
50
|
66
|
|
|
|
|
|
|
50
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
|
|
100
|
33
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1363
|
7
|
|
|
|
|
13
|
ConfigDefaults (); |
1364
|
|
|
|
|
|
|
} |
1365
|
|
|
|
|
|
|
elsif ( ($try eq 'posix_default' or $try eq 'posix_defaults') ) { |
1366
|
0
|
|
|
|
|
0
|
local $ENV{POSIXLY_CORRECT}; |
1367
|
0
|
0
|
|
|
|
0
|
$ENV{POSIXLY_CORRECT} = 1 if $action; |
1368
|
0
|
|
|
|
|
0
|
ConfigDefaults (); |
1369
|
|
|
|
|
|
|
} |
1370
|
|
|
|
|
|
|
elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) { |
1371
|
0
|
|
|
|
|
0
|
$autoabbrev = $action; |
1372
|
|
|
|
|
|
|
} |
1373
|
|
|
|
|
|
|
elsif ( $try eq 'getopt_compat' ) { |
1374
|
0
|
|
|
|
|
0
|
$getopt_compat = $action; |
1375
|
0
|
0
|
|
|
|
0
|
$genprefix = $action ? "(--|-|\\+)" : "(--|-)"; |
1376
|
|
|
|
|
|
|
} |
1377
|
|
|
|
|
|
|
elsif ( $try eq 'gnu_getopt' ) { |
1378
|
0
|
0
|
|
|
|
0
|
if ( $action ) { |
1379
|
0
|
|
|
|
|
0
|
$gnu_compat = 1; |
1380
|
0
|
|
|
|
|
0
|
$bundling = 1; |
1381
|
0
|
|
|
|
|
0
|
$getopt_compat = 0; |
1382
|
0
|
|
|
|
|
0
|
$genprefix = "(--|-)"; |
1383
|
0
|
|
|
|
|
0
|
$order = $PERMUTE; |
1384
|
0
|
|
|
|
|
0
|
$bundling_values = 0; |
1385
|
|
|
|
|
|
|
} |
1386
|
|
|
|
|
|
|
} |
1387
|
|
|
|
|
|
|
elsif ( $try eq 'gnu_compat' ) { |
1388
|
0
|
|
|
|
|
0
|
$gnu_compat = $action; |
1389
|
0
|
|
|
|
|
0
|
$bundling = 0; |
1390
|
0
|
|
|
|
|
0
|
$bundling_values = 1; |
1391
|
|
|
|
|
|
|
} |
1392
|
|
|
|
|
|
|
elsif ( $try =~ /^(auto_?)?version$/ ) { |
1393
|
0
|
|
|
|
|
0
|
$auto_version = $action; |
1394
|
|
|
|
|
|
|
} |
1395
|
|
|
|
|
|
|
elsif ( $try =~ /^(auto_?)?help$/ ) { |
1396
|
0
|
|
|
|
|
0
|
$auto_help = $action; |
1397
|
|
|
|
|
|
|
} |
1398
|
|
|
|
|
|
|
elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) { |
1399
|
6
|
|
|
|
|
13
|
$ignorecase = $action; |
1400
|
|
|
|
|
|
|
} |
1401
|
|
|
|
|
|
|
elsif ( $try eq 'ignorecase_always' or $try eq 'ignore_case_always' ) { |
1402
|
2
|
50
|
|
|
|
3
|
$ignorecase = $action ? 2 : 0; |
1403
|
|
|
|
|
|
|
} |
1404
|
|
|
|
|
|
|
elsif ( $try eq 'bundling' ) { |
1405
|
2
|
|
|
|
|
3
|
$bundling = $action; |
1406
|
2
|
50
|
|
|
|
5
|
$bundling_values = 0 if $action; |
1407
|
|
|
|
|
|
|
} |
1408
|
|
|
|
|
|
|
elsif ( $try eq 'bundling_override' ) { |
1409
|
0
|
0
|
|
|
|
0
|
$bundling = $action ? 2 : 0; |
1410
|
0
|
0
|
|
|
|
0
|
$bundling_values = 0 if $action; |
1411
|
|
|
|
|
|
|
} |
1412
|
|
|
|
|
|
|
elsif ( $try eq 'bundling_values' ) { |
1413
|
0
|
|
|
|
|
0
|
$bundling_values = $action; |
1414
|
0
|
0
|
|
|
|
0
|
$bundling = 0 if $action; |
1415
|
|
|
|
|
|
|
} |
1416
|
|
|
|
|
|
|
elsif ( $try eq 'require_order' ) { |
1417
|
1
|
50
|
|
|
|
3
|
$order = $action ? $REQUIRE_ORDER : $PERMUTE; |
1418
|
|
|
|
|
|
|
} |
1419
|
|
|
|
|
|
|
elsif ( $try eq 'permute' ) { |
1420
|
0
|
0
|
|
|
|
0
|
$order = $action ? $PERMUTE : $REQUIRE_ORDER; |
1421
|
|
|
|
|
|
|
} |
1422
|
|
|
|
|
|
|
elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) { |
1423
|
2
|
|
|
|
|
4
|
$passthrough = $action; |
1424
|
|
|
|
|
|
|
} |
1425
|
|
|
|
|
|
|
elsif ( $try =~ /^prefix=(.+)$/ && $action ) { |
1426
|
0
|
|
|
|
|
0
|
$genprefix = $1; |
1427
|
|
|
|
|
|
|
# Turn into regexp. Needs to be parenthesized! |
1428
|
0
|
|
|
|
|
0
|
$genprefix = "(" . quotemeta($genprefix) . ")"; |
1429
|
0
|
|
|
|
|
0
|
eval { '' =~ /$genprefix/; }; |
|
0
|
|
|
|
|
0
|
|
1430
|
0
|
0
|
|
|
|
0
|
die("Getopt::Long: invalid pattern \"$genprefix\"\n") if $@; |
1431
|
|
|
|
|
|
|
} |
1432
|
|
|
|
|
|
|
elsif ( $try =~ /^prefix_pattern=(.+)$/ && $action ) { |
1433
|
3
|
|
|
|
|
9
|
$genprefix = $1; |
1434
|
|
|
|
|
|
|
# Parenthesize if needed. |
1435
|
3
|
50
|
|
|
|
10
|
$genprefix = "(" . $genprefix . ")" |
1436
|
|
|
|
|
|
|
unless $genprefix =~ /^\(.*\)$/; |
1437
|
3
|
|
|
|
|
5
|
eval { '' =~ m"$genprefix"; }; |
|
3
|
|
|
|
|
23
|
|
1438
|
3
|
50
|
|
|
|
7
|
die("Getopt::Long: invalid pattern \"$genprefix\"\n") if $@; |
1439
|
|
|
|
|
|
|
} |
1440
|
|
|
|
|
|
|
elsif ( $try =~ /^long_prefix_pattern=(.+)$/ && $action ) { |
1441
|
3
|
|
|
|
|
8
|
$longprefix = $1; |
1442
|
|
|
|
|
|
|
# Parenthesize if needed. |
1443
|
3
|
50
|
|
|
|
9
|
$longprefix = "(" . $longprefix . ")" |
1444
|
|
|
|
|
|
|
unless $longprefix =~ /^\(.*\)$/; |
1445
|
3
|
|
|
|
|
5
|
eval { '' =~ m"$longprefix"; }; |
|
3
|
|
|
|
|
37
|
|
1446
|
3
|
50
|
|
|
|
10
|
die("Getopt::Long: invalid long prefix pattern \"$longprefix\"\n") if $@; |
1447
|
|
|
|
|
|
|
} |
1448
|
|
|
|
|
|
|
elsif ( $try eq 'debug' ) { |
1449
|
0
|
|
|
|
|
0
|
$debug = $action; |
1450
|
|
|
|
|
|
|
} |
1451
|
|
|
|
|
|
|
else { |
1452
|
0
|
|
|
|
|
0
|
die("Getopt::Long: unknown or erroneous config parameter \"$opt\"\n") |
1453
|
|
|
|
|
|
|
} |
1454
|
|
|
|
|
|
|
} |
1455
|
25
|
|
|
|
|
4423
|
$prevconfig; |
1456
|
|
|
|
|
|
|
} |
1457
|
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
# Deprecated name. |
1459
|
|
|
|
|
|
|
sub config (@) { |
1460
|
0
|
|
|
0
|
0
|
0
|
Configure (@_); |
1461
|
|
|
|
|
|
|
} |
1462
|
|
|
|
|
|
|
|
1463
|
|
|
|
|
|
|
# Issue a standard message for --version. |
1464
|
|
|
|
|
|
|
# |
1465
|
|
|
|
|
|
|
# The arguments are mostly the same as for Pod::Usage::pod2usage: |
1466
|
|
|
|
|
|
|
# |
1467
|
|
|
|
|
|
|
# - a number (exit value) |
1468
|
|
|
|
|
|
|
# - a string (lead in message) |
1469
|
|
|
|
|
|
|
# - a hash with options. See Pod::Usage for details. |
1470
|
|
|
|
|
|
|
# |
1471
|
|
|
|
|
|
|
sub VersionMessage(@) { |
1472
|
|
|
|
|
|
|
# Massage args. |
1473
|
0
|
|
|
0
|
|
0
|
my $pa = setup_pa_args("version", @_); |
1474
|
|
|
|
|
|
|
|
1475
|
0
|
|
|
|
|
0
|
my $v = $main::VERSION; |
1476
|
|
|
|
|
|
|
my $fh = $pa->{-output} || |
1477
|
0
|
|
0
|
|
|
0
|
( ($pa->{-exitval} eq "NOEXIT" || $pa->{-exitval} < 2) ? \*STDOUT : \*STDERR ); |
1478
|
|
|
|
|
|
|
|
1479
|
0
|
0
|
|
|
|
0
|
print $fh (defined($pa->{-message}) ? $pa->{-message} : (), |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1480
|
|
|
|
|
|
|
$0, defined $v ? " version $v" : (), |
1481
|
|
|
|
|
|
|
"\n", |
1482
|
|
|
|
|
|
|
"(", __PACKAGE__, "::", "GetOptions", |
1483
|
|
|
|
|
|
|
" version ", |
1484
|
|
|
|
|
|
|
defined($Getopt::Long::VERSION_STRING) |
1485
|
|
|
|
|
|
|
? $Getopt::Long::VERSION_STRING : $VERSION, ";", |
1486
|
|
|
|
|
|
|
" Perl version ", |
1487
|
|
|
|
|
|
|
$] >= 5.006 ? sprintf("%vd", $^V) : $], |
1488
|
|
|
|
|
|
|
")\n"); |
1489
|
0
|
0
|
|
|
|
0
|
exit($pa->{-exitval}) unless $pa->{-exitval} eq "NOEXIT"; |
1490
|
|
|
|
|
|
|
} |
1491
|
|
|
|
|
|
|
|
1492
|
|
|
|
|
|
|
# Issue a standard message for --help. |
1493
|
|
|
|
|
|
|
# |
1494
|
|
|
|
|
|
|
# The arguments are the same as for Pod::Usage::pod2usage: |
1495
|
|
|
|
|
|
|
# |
1496
|
|
|
|
|
|
|
# - a number (exit value) |
1497
|
|
|
|
|
|
|
# - a string (lead in message) |
1498
|
|
|
|
|
|
|
# - a hash with options. See Pod::Usage for details. |
1499
|
|
|
|
|
|
|
# |
1500
|
|
|
|
|
|
|
sub HelpMessage(@) { |
1501
|
0
|
0
|
|
0
|
|
0
|
eval { |
1502
|
0
|
|
|
|
|
0
|
require Pod::Usage; |
1503
|
0
|
|
|
|
|
0
|
import Pod::Usage; |
1504
|
0
|
|
|
|
|
0
|
1; |
1505
|
|
|
|
|
|
|
} || die("Cannot provide help: cannot load Pod::Usage\n"); |
1506
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
# Note that pod2usage will issue a warning if -exitval => NOEXIT. |
1508
|
0
|
|
|
|
|
0
|
pod2usage(setup_pa_args("help", @_)); |
1509
|
|
|
|
|
|
|
|
1510
|
|
|
|
|
|
|
} |
1511
|
|
|
|
|
|
|
|
1512
|
|
|
|
|
|
|
# Helper routine to set up a normalized hash ref to be used as |
1513
|
|
|
|
|
|
|
# argument to pod2usage. |
1514
|
|
|
|
|
|
|
sub setup_pa_args($@) { |
1515
|
0
|
|
|
0
|
0
|
0
|
my $tag = shift; # who's calling |
1516
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
# If called by direct binding to an option, it will get the option |
1518
|
|
|
|
|
|
|
# name and value as arguments. Remove these, if so. |
1519
|
0
|
0
|
0
|
|
|
0
|
@_ = () if @_ == 2 && $_[0] eq $tag; |
1520
|
|
|
|
|
|
|
|
1521
|
0
|
|
|
|
|
0
|
my $pa; |
1522
|
0
|
0
|
|
|
|
0
|
if ( @_ > 1 ) { |
1523
|
0
|
|
|
|
|
0
|
$pa = { @_ }; |
1524
|
|
|
|
|
|
|
} |
1525
|
|
|
|
|
|
|
else { |
1526
|
0
|
|
0
|
|
|
0
|
$pa = shift || {}; |
1527
|
|
|
|
|
|
|
} |
1528
|
|
|
|
|
|
|
|
1529
|
|
|
|
|
|
|
# At this point, $pa can be a number (exit value), string |
1530
|
|
|
|
|
|
|
# (message) or hash with options. |
1531
|
|
|
|
|
|
|
|
1532
|
0
|
0
|
|
|
|
0
|
if ( UNIVERSAL::isa($pa, 'HASH') ) { |
|
|
0
|
|
|
|
|
|
1533
|
|
|
|
|
|
|
# Get rid of -msg vs. -message ambiguity. |
1534
|
0
|
|
0
|
|
|
0
|
$pa->{-message} //= delete($pa->{-msg}); |
1535
|
|
|
|
|
|
|
} |
1536
|
|
|
|
|
|
|
elsif ( $pa =~ /^-?\d+$/ ) { |
1537
|
0
|
|
|
|
|
0
|
$pa = { -exitval => $pa }; |
1538
|
|
|
|
|
|
|
} |
1539
|
|
|
|
|
|
|
else { |
1540
|
0
|
|
|
|
|
0
|
$pa = { -message => $pa }; |
1541
|
|
|
|
|
|
|
} |
1542
|
|
|
|
|
|
|
|
1543
|
|
|
|
|
|
|
# These are _our_ defaults. |
1544
|
0
|
0
|
|
|
|
0
|
$pa->{-verbose} = 0 unless exists($pa->{-verbose}); |
1545
|
0
|
0
|
|
|
|
0
|
$pa->{-exitval} = 0 unless exists($pa->{-exitval}); |
1546
|
0
|
|
|
|
|
0
|
$pa; |
1547
|
|
|
|
|
|
|
} |
1548
|
|
|
|
|
|
|
|
1549
|
|
|
|
|
|
|
# Sneak way to know what version the user requested. |
1550
|
|
|
|
|
|
|
sub VERSION { |
1551
|
0
|
0
|
|
0
|
0
|
0
|
$requested_version = $_[1] if @_ > 1; |
1552
|
0
|
|
|
|
|
0
|
shift->SUPER::VERSION(@_); |
1553
|
|
|
|
|
|
|
} |
1554
|
|
|
|
|
|
|
|
1555
|
|
|
|
|
|
|
package Getopt::Long::CallBack; |
1556
|
|
|
|
|
|
|
|
1557
|
|
|
|
|
|
|
sub new { |
1558
|
1
|
|
|
1
|
|
8
|
my ($pkg, %atts) = @_; |
1559
|
1
|
|
|
|
|
23
|
bless { %atts }, $pkg; |
1560
|
|
|
|
|
|
|
} |
1561
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
sub name { |
1563
|
0
|
|
|
0
|
|
|
my $self = shift; |
1564
|
0
|
|
|
|
|
|
''.$self->{name}; |
1565
|
|
|
|
|
|
|
} |
1566
|
|
|
|
|
|
|
|
1567
|
|
|
|
|
|
|
sub given { |
1568
|
0
|
|
|
0
|
|
|
my $self = shift; |
1569
|
0
|
|
|
|
|
|
$self->{given}; |
1570
|
|
|
|
|
|
|
} |
1571
|
|
|
|
|
|
|
|
1572
|
|
|
|
|
|
|
use overload |
1573
|
|
|
|
|
|
|
# Treat this object as an ordinary string for legacy API. |
1574
|
6
|
|
|
|
|
37
|
'""' => \&name, |
1575
|
6
|
|
|
6
|
|
5177
|
fallback => 1; |
|
6
|
|
|
|
|
4530
|
|
1576
|
|
|
|
|
|
|
|
1577
|
|
|
|
|
|
|
1; |
1578
|
|
|
|
|
|
|
|
1579
|
|
|
|
|
|
|
################ Documentation ################ |
1580
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
=head1 NAME |
1582
|
|
|
|
|
|
|
|
1583
|
|
|
|
|
|
|
Getopt::Long - Extended processing of command line options |
1584
|
|
|
|
|
|
|
|
1585
|
|
|
|
|
|
|
=head1 SYNOPSIS |
1586
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
use Getopt::Long; |
1588
|
|
|
|
|
|
|
my $data = "file.dat"; |
1589
|
|
|
|
|
|
|
my $length = 24; |
1590
|
|
|
|
|
|
|
my $verbose; |
1591
|
|
|
|
|
|
|
GetOptions ("length=i" => \$length, # numeric |
1592
|
|
|
|
|
|
|
"file=s" => \$data, # string |
1593
|
|
|
|
|
|
|
"verbose" => \$verbose) # flag |
1594
|
|
|
|
|
|
|
or die("Error in command line arguments\n"); |
1595
|
|
|
|
|
|
|
|
1596
|
|
|
|
|
|
|
=head1 DESCRIPTION |
1597
|
|
|
|
|
|
|
|
1598
|
|
|
|
|
|
|
The Getopt::Long module implements an extended getopt function called |
1599
|
|
|
|
|
|
|
GetOptions(). It parses the command line from C<@ARGV>, recognizing |
1600
|
|
|
|
|
|
|
and removing specified options and their possible values. |
1601
|
|
|
|
|
|
|
|
1602
|
|
|
|
|
|
|
This function adheres to the POSIX syntax for command |
1603
|
|
|
|
|
|
|
line options, with GNU extensions. In general, this means that options |
1604
|
|
|
|
|
|
|
have long names instead of single letters, and are introduced with a |
1605
|
|
|
|
|
|
|
double dash "--". Support for bundling of command line options, as was |
1606
|
|
|
|
|
|
|
the case with the more traditional single-letter approach, is provided |
1607
|
|
|
|
|
|
|
but not enabled by default. |
1608
|
|
|
|
|
|
|
|
1609
|
|
|
|
|
|
|
=head1 Command Line Options, an Introduction |
1610
|
|
|
|
|
|
|
|
1611
|
|
|
|
|
|
|
Command line operated programs traditionally take their arguments from |
1612
|
|
|
|
|
|
|
the command line, for example filenames or other information that the |
1613
|
|
|
|
|
|
|
program needs to know. Besides arguments, these programs often take |
1614
|
|
|
|
|
|
|
command line I as well. Options are not necessary for the |
1615
|
|
|
|
|
|
|
program to work, hence the name 'option', but are used to modify its |
1616
|
|
|
|
|
|
|
default behaviour. For example, a program could do its job quietly, |
1617
|
|
|
|
|
|
|
but with a suitable option it could provide verbose information about |
1618
|
|
|
|
|
|
|
what it did. |
1619
|
|
|
|
|
|
|
|
1620
|
|
|
|
|
|
|
Command line options come in several flavours. Historically, they are |
1621
|
|
|
|
|
|
|
preceded by a single dash C<->, and consist of a single letter. |
1622
|
|
|
|
|
|
|
|
1623
|
|
|
|
|
|
|
-l -a -c |
1624
|
|
|
|
|
|
|
|
1625
|
|
|
|
|
|
|
Usually, these single-character options can be bundled: |
1626
|
|
|
|
|
|
|
|
1627
|
|
|
|
|
|
|
-lac |
1628
|
|
|
|
|
|
|
|
1629
|
|
|
|
|
|
|
Options can have values, the value is placed after the option |
1630
|
|
|
|
|
|
|
character. Sometimes with whitespace in between, sometimes not: |
1631
|
|
|
|
|
|
|
|
1632
|
|
|
|
|
|
|
-s 24 -s24 |
1633
|
|
|
|
|
|
|
|
1634
|
|
|
|
|
|
|
Due to the very cryptic nature of these options, another style was |
1635
|
|
|
|
|
|
|
developed that used long names. So instead of a cryptic C<-l> one |
1636
|
|
|
|
|
|
|
could use the more descriptive C<--long>. To distinguish between a |
1637
|
|
|
|
|
|
|
bundle of single-character options and a long one, two dashes are used |
1638
|
|
|
|
|
|
|
to precede the option name. Early implementations of long options used |
1639
|
|
|
|
|
|
|
a plus C<+> instead. Also, option values could be specified either |
1640
|
|
|
|
|
|
|
like |
1641
|
|
|
|
|
|
|
|
1642
|
|
|
|
|
|
|
--size=24 |
1643
|
|
|
|
|
|
|
|
1644
|
|
|
|
|
|
|
or |
1645
|
|
|
|
|
|
|
|
1646
|
|
|
|
|
|
|
--size 24 |
1647
|
|
|
|
|
|
|
|
1648
|
|
|
|
|
|
|
The C<+> form is now obsolete and strongly deprecated. |
1649
|
|
|
|
|
|
|
|
1650
|
|
|
|
|
|
|
=head1 Getting Started with Getopt::Long |
1651
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
Getopt::Long is the Perl5 successor of C. This was the |
1653
|
|
|
|
|
|
|
first Perl module that provided support for handling the new style of |
1654
|
|
|
|
|
|
|
command line options, in particular long option names, hence the Perl5 |
1655
|
|
|
|
|
|
|
name Getopt::Long. This module also supports single-character options |
1656
|
|
|
|
|
|
|
and bundling. |
1657
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
To use Getopt::Long from a Perl program, you must include the |
1659
|
|
|
|
|
|
|
following line in your Perl program: |
1660
|
|
|
|
|
|
|
|
1661
|
|
|
|
|
|
|
use Getopt::Long; |
1662
|
|
|
|
|
|
|
|
1663
|
|
|
|
|
|
|
This will load the core of the Getopt::Long module and prepare your |
1664
|
|
|
|
|
|
|
program for using it. Most of the actual Getopt::Long code is not |
1665
|
|
|
|
|
|
|
loaded until you really call one of its functions. |
1666
|
|
|
|
|
|
|
|
1667
|
|
|
|
|
|
|
In the default configuration, options names may be abbreviated to |
1668
|
|
|
|
|
|
|
uniqueness, case does not matter, and a single dash is sufficient, |
1669
|
|
|
|
|
|
|
even for long option names. Also, options may be placed between |
1670
|
|
|
|
|
|
|
non-option arguments. See L for more |
1671
|
|
|
|
|
|
|
details on how to configure Getopt::Long. |
1672
|
|
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
=head2 Simple options |
1674
|
|
|
|
|
|
|
|
1675
|
|
|
|
|
|
|
The most simple options are the ones that take no values. Their mere |
1676
|
|
|
|
|
|
|
presence on the command line enables the option. Popular examples are: |
1677
|
|
|
|
|
|
|
|
1678
|
|
|
|
|
|
|
--all --verbose --quiet --debug |
1679
|
|
|
|
|
|
|
|
1680
|
|
|
|
|
|
|
Handling simple options is straightforward: |
1681
|
|
|
|
|
|
|
|
1682
|
|
|
|
|
|
|
my $verbose = ''; # option variable with default value (false) |
1683
|
|
|
|
|
|
|
my $all = ''; # option variable with default value (false) |
1684
|
|
|
|
|
|
|
GetOptions ('verbose' => \$verbose, 'all' => \$all); |
1685
|
|
|
|
|
|
|
|
1686
|
|
|
|
|
|
|
The call to GetOptions() parses the command line arguments that are |
1687
|
|
|
|
|
|
|
present in C<@ARGV> and sets the option variable to the value C<1> if |
1688
|
|
|
|
|
|
|
the option did occur on the command line. Otherwise, the option |
1689
|
|
|
|
|
|
|
variable is not touched. Setting the option value to true is often |
1690
|
|
|
|
|
|
|
called I the option. |
1691
|
|
|
|
|
|
|
|
1692
|
|
|
|
|
|
|
The option name as specified to the GetOptions() function is called |
1693
|
|
|
|
|
|
|
the option I. Later we'll see that this specification |
1694
|
|
|
|
|
|
|
can contain more than just the option name. The reference to the |
1695
|
|
|
|
|
|
|
variable is called the option I. |
1696
|
|
|
|
|
|
|
|
1697
|
|
|
|
|
|
|
GetOptions() will return a true value if the command line could be |
1698
|
|
|
|
|
|
|
processed successfully. Otherwise, it will write error messages using |
1699
|
|
|
|
|
|
|
die() and warn(), and return a false result. |
1700
|
|
|
|
|
|
|
|
1701
|
|
|
|
|
|
|
=head2 A little bit less simple options |
1702
|
|
|
|
|
|
|
|
1703
|
|
|
|
|
|
|
Getopt::Long supports two useful variants of simple options: |
1704
|
|
|
|
|
|
|
I options and I options. |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
A negatable option is specified with an exclamation mark C after the |
1707
|
|
|
|
|
|
|
option name: |
1708
|
|
|
|
|
|
|
|
1709
|
|
|
|
|
|
|
my $verbose = ''; # option variable with default value (false) |
1710
|
|
|
|
|
|
|
GetOptions ('verbose!' => \$verbose); |
1711
|
|
|
|
|
|
|
|
1712
|
|
|
|
|
|
|
Now, using C<--verbose> on the command line will enable C<$verbose>, |
1713
|
|
|
|
|
|
|
as expected. But it is also allowed to use C<--noverbose>, which will |
1714
|
|
|
|
|
|
|
disable C<$verbose> by setting its value to C<0>. Using a suitable |
1715
|
|
|
|
|
|
|
default value, the program can find out whether C<$verbose> is false |
1716
|
|
|
|
|
|
|
by default, or disabled by using C<--noverbose>. |
1717
|
|
|
|
|
|
|
|
1718
|
|
|
|
|
|
|
(If both C<--verbose> and C<--noverbose> are given, whichever is given |
1719
|
|
|
|
|
|
|
last takes precedence.) |
1720
|
|
|
|
|
|
|
|
1721
|
|
|
|
|
|
|
An incremental option is specified with a plus C<+> after the |
1722
|
|
|
|
|
|
|
option name: |
1723
|
|
|
|
|
|
|
|
1724
|
|
|
|
|
|
|
my $verbose = ''; # option variable with default value (false) |
1725
|
|
|
|
|
|
|
GetOptions ('verbose+' => \$verbose); |
1726
|
|
|
|
|
|
|
|
1727
|
|
|
|
|
|
|
Using C<--verbose> on the command line will increment the value of |
1728
|
|
|
|
|
|
|
C<$verbose>. This way the program can keep track of how many times the |
1729
|
|
|
|
|
|
|
option occurred on the command line. For example, each occurrence of |
1730
|
|
|
|
|
|
|
C<--verbose> could increase the verbosity level of the program. |
1731
|
|
|
|
|
|
|
|
1732
|
|
|
|
|
|
|
=head2 Mixing command line option with other arguments |
1733
|
|
|
|
|
|
|
|
1734
|
|
|
|
|
|
|
Usually programs take command line options as well as other arguments, |
1735
|
|
|
|
|
|
|
for example, file names. It is good practice to always specify the |
1736
|
|
|
|
|
|
|
options first, and the other arguments last. Getopt::Long will, |
1737
|
|
|
|
|
|
|
however, allow the options and arguments to be mixed and 'filter out' |
1738
|
|
|
|
|
|
|
all the options before passing the rest of the arguments to the |
1739
|
|
|
|
|
|
|
program. To stop Getopt::Long from processing further arguments, |
1740
|
|
|
|
|
|
|
insert a double dash C<--> on the command line: |
1741
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
--size 24 -- --all |
1743
|
|
|
|
|
|
|
|
1744
|
|
|
|
|
|
|
In this example, C<--all> will I be treated as an option, but |
1745
|
|
|
|
|
|
|
passed to the program unharmed, in C<@ARGV>. |
1746
|
|
|
|
|
|
|
|
1747
|
|
|
|
|
|
|
=head2 Options with values |
1748
|
|
|
|
|
|
|
|
1749
|
|
|
|
|
|
|
For options that take values it must be specified whether the option |
1750
|
|
|
|
|
|
|
value is required or not, and what kind of value the option expects. |
1751
|
|
|
|
|
|
|
|
1752
|
|
|
|
|
|
|
Three kinds of values are supported: integer numbers, floating point |
1753
|
|
|
|
|
|
|
numbers, and strings. |
1754
|
|
|
|
|
|
|
|
1755
|
|
|
|
|
|
|
If the option value is required, Getopt::Long will take the |
1756
|
|
|
|
|
|
|
command line argument that follows the option and assign this to the |
1757
|
|
|
|
|
|
|
option variable. If, however, the option value is specified as |
1758
|
|
|
|
|
|
|
optional, this will only be done if that value does not look like a |
1759
|
|
|
|
|
|
|
valid command line option itself. |
1760
|
|
|
|
|
|
|
|
1761
|
|
|
|
|
|
|
my $tag = ''; # option variable with default value |
1762
|
|
|
|
|
|
|
GetOptions ('tag=s' => \$tag); |
1763
|
|
|
|
|
|
|
|
1764
|
|
|
|
|
|
|
In the option specification, the option name is followed by an equals |
1765
|
|
|
|
|
|
|
sign C<=> and the letter C. The equals sign indicates that this |
1766
|
|
|
|
|
|
|
option requires a value. The letter C indicates that this value is |
1767
|
|
|
|
|
|
|
an arbitrary string. Other possible value types are C for integer |
1768
|
|
|
|
|
|
|
values, and C for floating point values. Using a colon C<:> instead |
1769
|
|
|
|
|
|
|
of the equals sign indicates that the option value is optional. In |
1770
|
|
|
|
|
|
|
this case, if no suitable value is supplied, string valued options get |
1771
|
|
|
|
|
|
|
an empty string C<''> assigned, while numeric options are set to C<0>. |
1772
|
|
|
|
|
|
|
|
1773
|
|
|
|
|
|
|
(If the same option appears more than once on the command line, the |
1774
|
|
|
|
|
|
|
last given value is used. If you want to take all the values, see |
1775
|
|
|
|
|
|
|
below.) |
1776
|
|
|
|
|
|
|
|
1777
|
|
|
|
|
|
|
=head2 Options with multiple values |
1778
|
|
|
|
|
|
|
|
1779
|
|
|
|
|
|
|
Options sometimes take several values. For example, a program could |
1780
|
|
|
|
|
|
|
use multiple directories to search for library files: |
1781
|
|
|
|
|
|
|
|
1782
|
|
|
|
|
|
|
--library lib/stdlib --library lib/extlib |
1783
|
|
|
|
|
|
|
|
1784
|
|
|
|
|
|
|
To accomplish this behaviour, simply specify an array reference as the |
1785
|
|
|
|
|
|
|
destination for the option: |
1786
|
|
|
|
|
|
|
|
1787
|
|
|
|
|
|
|
GetOptions ("library=s" => \@libfiles); |
1788
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
Alternatively, you can specify that the option can have multiple |
1790
|
|
|
|
|
|
|
values by adding a "@", and pass a reference to a scalar as the |
1791
|
|
|
|
|
|
|
destination: |
1792
|
|
|
|
|
|
|
|
1793
|
|
|
|
|
|
|
GetOptions ("library=s@" => \$libfiles); |
1794
|
|
|
|
|
|
|
|
1795
|
|
|
|
|
|
|
Used with the example above, C<@libfiles> c.q. C<@$libfiles> would |
1796
|
|
|
|
|
|
|
contain two strings upon completion: C<"lib/stdlib"> and |
1797
|
|
|
|
|
|
|
C<"lib/extlib">, in that order. It is also possible to specify that |
1798
|
|
|
|
|
|
|
only integer or floating point numbers are acceptable values. |
1799
|
|
|
|
|
|
|
|
1800
|
|
|
|
|
|
|
Often it is useful to allow comma-separated lists of values as well as |
1801
|
|
|
|
|
|
|
multiple occurrences of the options. This is easy using Perl's split() |
1802
|
|
|
|
|
|
|
and join() operators: |
1803
|
|
|
|
|
|
|
|
1804
|
|
|
|
|
|
|
GetOptions ("library=s" => \@libfiles); |
1805
|
|
|
|
|
|
|
@libfiles = split(/,/,join(',',@libfiles)); |
1806
|
|
|
|
|
|
|
|
1807
|
|
|
|
|
|
|
Of course, it is important to choose the right separator string for |
1808
|
|
|
|
|
|
|
each purpose. |
1809
|
|
|
|
|
|
|
|
1810
|
|
|
|
|
|
|
Warning: What follows is an experimental feature. |
1811
|
|
|
|
|
|
|
|
1812
|
|
|
|
|
|
|
Options can take multiple values at once, for example |
1813
|
|
|
|
|
|
|
|
1814
|
|
|
|
|
|
|
--coordinates 52.2 16.4 --rgbcolor 255 255 149 |
1815
|
|
|
|
|
|
|
|
1816
|
|
|
|
|
|
|
This can be accomplished by adding a repeat specifier to the option |
1817
|
|
|
|
|
|
|
specification. Repeat specifiers are very similar to the C<{...}> |
1818
|
|
|
|
|
|
|
repeat specifiers that can be used with regular expression patterns. |
1819
|
|
|
|
|
|
|
For example, the above command line would be handled as follows: |
1820
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
GetOptions('coordinates=f{2}' => \@coor, 'rgbcolor=i{3}' => \@color); |
1822
|
|
|
|
|
|
|
|
1823
|
|
|
|
|
|
|
The destination for the option must be an array or array reference. |
1824
|
|
|
|
|
|
|
|
1825
|
|
|
|
|
|
|
It is also possible to specify the minimal and maximal number of |
1826
|
|
|
|
|
|
|
arguments an option takes. C indicates an option that |
1827
|
|
|
|
|
|
|
takes at least two and at most 4 arguments. C indicates one |
1828
|
|
|
|
|
|
|
or more values; C indicates zero or more option values. |
1829
|
|
|
|
|
|
|
|
1830
|
|
|
|
|
|
|
=head2 Options with hash values |
1831
|
|
|
|
|
|
|
|
1832
|
|
|
|
|
|
|
If the option destination is a reference to a hash, the option will |
1833
|
|
|
|
|
|
|
take, as value, strings of the form IC<=>I. The value will |
1834
|
|
|
|
|
|
|
be stored with the specified key in the hash. |
1835
|
|
|
|
|
|
|
|
1836
|
|
|
|
|
|
|
GetOptions ("define=s" => \%defines); |
1837
|
|
|
|
|
|
|
|
1838
|
|
|
|
|
|
|
Alternatively you can use: |
1839
|
|
|
|
|
|
|
|
1840
|
|
|
|
|
|
|
GetOptions ("define=s%" => \$defines); |
1841
|
|
|
|
|
|
|
|
1842
|
|
|
|
|
|
|
When used with command line options: |
1843
|
|
|
|
|
|
|
|
1844
|
|
|
|
|
|
|
--define os=linux --define vendor=redhat |
1845
|
|
|
|
|
|
|
|
1846
|
|
|
|
|
|
|
the hash C<%defines> (or C<%$defines>) will contain two keys, C<"os"> |
1847
|
|
|
|
|
|
|
with value C<"linux"> and C<"vendor"> with value C<"redhat">. It is |
1848
|
|
|
|
|
|
|
also possible to specify that only integer or floating point numbers |
1849
|
|
|
|
|
|
|
are acceptable values. The keys are always taken to be strings. |
1850
|
|
|
|
|
|
|
|
1851
|
|
|
|
|
|
|
=head2 User-defined subroutines to handle options |
1852
|
|
|
|
|
|
|
|
1853
|
|
|
|
|
|
|
Ultimate control over what should be done when (actually: each time) |
1854
|
|
|
|
|
|
|
an option is encountered on the command line can be achieved by |
1855
|
|
|
|
|
|
|
designating a reference to a subroutine (or an anonymous subroutine) |
1856
|
|
|
|
|
|
|
as the option destination. When GetOptions() encounters the option, it |
1857
|
|
|
|
|
|
|
will call the subroutine with two or three arguments. The first |
1858
|
|
|
|
|
|
|
argument is the name of the option. (Actually, it is an object that |
1859
|
|
|
|
|
|
|
stringifies to the name of the option.) For a scalar or array destination, |
1860
|
|
|
|
|
|
|
the second argument is the value to be stored. For a hash destination, |
1861
|
|
|
|
|
|
|
the second argument is the key to the hash, and the third argument |
1862
|
|
|
|
|
|
|
the value to be stored. It is up to the subroutine to store the value, |
1863
|
|
|
|
|
|
|
or do whatever it thinks is appropriate. |
1864
|
|
|
|
|
|
|
|
1865
|
|
|
|
|
|
|
A trivial application of this mechanism is to implement options that |
1866
|
|
|
|
|
|
|
are related to each other. For example: |
1867
|
|
|
|
|
|
|
|
1868
|
|
|
|
|
|
|
my $verbose = ''; # option variable with default value (false) |
1869
|
|
|
|
|
|
|
GetOptions ('verbose' => \$verbose, |
1870
|
|
|
|
|
|
|
'quiet' => sub { $verbose = 0 }); |
1871
|
|
|
|
|
|
|
|
1872
|
|
|
|
|
|
|
Here C<--verbose> and C<--quiet> control the same variable |
1873
|
|
|
|
|
|
|
C<$verbose>, but with opposite values. |
1874
|
|
|
|
|
|
|
|
1875
|
|
|
|
|
|
|
If the subroutine needs to signal an error, it should call die() with |
1876
|
|
|
|
|
|
|
the desired error message as its argument. GetOptions() will catch the |
1877
|
|
|
|
|
|
|
die(), issue the error message, and record that an error result must |
1878
|
|
|
|
|
|
|
be returned upon completion. |
1879
|
|
|
|
|
|
|
|
1880
|
|
|
|
|
|
|
If the text of the error message starts with an exclamation mark C |
1881
|
|
|
|
|
|
|
it is interpreted specially by GetOptions(). There is currently one |
1882
|
|
|
|
|
|
|
special command implemented: C will cause GetOptions() |
1883
|
|
|
|
|
|
|
to stop processing options, as if it encountered a double dash C<-->. |
1884
|
|
|
|
|
|
|
|
1885
|
|
|
|
|
|
|
Here is an example of how to access the option name and value from within |
1886
|
|
|
|
|
|
|
a subroutine: |
1887
|
|
|
|
|
|
|
|
1888
|
|
|
|
|
|
|
GetOptions ('opt=i' => \&handler); |
1889
|
|
|
|
|
|
|
sub handler { |
1890
|
|
|
|
|
|
|
my ($opt_name, $opt_value) = @_; |
1891
|
|
|
|
|
|
|
print("Option name is $opt_name and value is $opt_value\n"); |
1892
|
|
|
|
|
|
|
} |
1893
|
|
|
|
|
|
|
|
1894
|
|
|
|
|
|
|
=head2 Options with multiple names |
1895
|
|
|
|
|
|
|
|
1896
|
|
|
|
|
|
|
Often it is user friendly to supply alternate mnemonic names for |
1897
|
|
|
|
|
|
|
options. For example C<--height> could be an alternate name for |
1898
|
|
|
|
|
|
|
C<--length>. Alternate names can be included in the option |
1899
|
|
|
|
|
|
|
specification, separated by vertical bar C<|> characters. To implement |
1900
|
|
|
|
|
|
|
the above example: |
1901
|
|
|
|
|
|
|
|
1902
|
|
|
|
|
|
|
GetOptions ('length|height=f' => \$length); |
1903
|
|
|
|
|
|
|
|
1904
|
|
|
|
|
|
|
The first name is called the I name, the other names are |
1905
|
|
|
|
|
|
|
called I. When using a hash to store options, the key will |
1906
|
|
|
|
|
|
|
always be the primary name. |
1907
|
|
|
|
|
|
|
|
1908
|
|
|
|
|
|
|
Multiple alternate names are possible. |
1909
|
|
|
|
|
|
|
|
1910
|
|
|
|
|
|
|
=head2 Case and abbreviations |
1911
|
|
|
|
|
|
|
|
1912
|
|
|
|
|
|
|
Without additional configuration, GetOptions() will ignore the case of |
1913
|
|
|
|
|
|
|
option names, and allow the options to be abbreviated to uniqueness. |
1914
|
|
|
|
|
|
|
|
1915
|
|
|
|
|
|
|
GetOptions ('length|height=f' => \$length, "head" => \$head); |
1916
|
|
|
|
|
|
|
|
1917
|
|
|
|
|
|
|
This call will allow C<--l> and C<--L> for the length option, but |
1918
|
|
|
|
|
|
|
requires a least C<--hea> and C<--hei> for the head and height options. |
1919
|
|
|
|
|
|
|
|
1920
|
|
|
|
|
|
|
=head2 Summary of Option Specifications |
1921
|
|
|
|
|
|
|
|
1922
|
|
|
|
|
|
|
Each option specifier consists of two parts: the name specification |
1923
|
|
|
|
|
|
|
and the argument specification. |
1924
|
|
|
|
|
|
|
|
1925
|
|
|
|
|
|
|
The name specification contains the name of the option, optionally |
1926
|
|
|
|
|
|
|
followed by a list of alternative names separated by vertical bar |
1927
|
|
|
|
|
|
|
characters. |
1928
|
|
|
|
|
|
|
|
1929
|
|
|
|
|
|
|
length option name is "length" |
1930
|
|
|
|
|
|
|
length|size|l name is "length", aliases are "size" and "l" |
1931
|
|
|
|
|
|
|
|
1932
|
|
|
|
|
|
|
The argument specification is optional. If omitted, the option is |
1933
|
|
|
|
|
|
|
considered boolean, a value of 1 will be assigned when the option is |
1934
|
|
|
|
|
|
|
used on the command line. |
1935
|
|
|
|
|
|
|
|
1936
|
|
|
|
|
|
|
The argument specification can be |
1937
|
|
|
|
|
|
|
|
1938
|
|
|
|
|
|
|
=over 4 |
1939
|
|
|
|
|
|
|
|
1940
|
|
|
|
|
|
|
=item ! |
1941
|
|
|
|
|
|
|
|
1942
|
|
|
|
|
|
|
The option does not take an argument and may be negated by prefixing |
1943
|
|
|
|
|
|
|
it with "no" or "no-". E.g. C<"foo!"> will allow C<--foo> (a value of |
1944
|
|
|
|
|
|
|
1 will be assigned) as well as C<--nofoo> and C<--no-foo> (a value of |
1945
|
|
|
|
|
|
|
0 will be assigned). If the option has aliases, this applies to the |
1946
|
|
|
|
|
|
|
aliases as well. |
1947
|
|
|
|
|
|
|
|
1948
|
|
|
|
|
|
|
Using negation on a single letter option when bundling is in effect is |
1949
|
|
|
|
|
|
|
pointless and will result in a warning. |
1950
|
|
|
|
|
|
|
|
1951
|
|
|
|
|
|
|
=item + |
1952
|
|
|
|
|
|
|
|
1953
|
|
|
|
|
|
|
The option does not take an argument and will be incremented by 1 |
1954
|
|
|
|
|
|
|
every time it appears on the command line. E.g. C<"more+">, when used |
1955
|
|
|
|
|
|
|
with C<--more --more --more>, will increment the value three times, |
1956
|
|
|
|
|
|
|
resulting in a value of 3 (provided it was 0 or undefined at first). |
1957
|
|
|
|
|
|
|
|
1958
|
|
|
|
|
|
|
The C<+> specifier is ignored if the option destination is not a scalar. |
1959
|
|
|
|
|
|
|
|
1960
|
|
|
|
|
|
|
=item = I [ I ] [ I ] |
1961
|
|
|
|
|
|
|
|
1962
|
|
|
|
|
|
|
The option requires an argument of the given type. Supported types |
1963
|
|
|
|
|
|
|
are: |
1964
|
|
|
|
|
|
|
|
1965
|
|
|
|
|
|
|
=over 4 |
1966
|
|
|
|
|
|
|
|
1967
|
|
|
|
|
|
|
=item s |
1968
|
|
|
|
|
|
|
|
1969
|
|
|
|
|
|
|
String. An arbitrary sequence of characters. It is valid for the |
1970
|
|
|
|
|
|
|
argument to start with C<-> or C<-->. |
1971
|
|
|
|
|
|
|
|
1972
|
|
|
|
|
|
|
=item i |
1973
|
|
|
|
|
|
|
|
1974
|
|
|
|
|
|
|
Integer. An optional leading plus or minus sign, followed by a |
1975
|
|
|
|
|
|
|
sequence of digits. |
1976
|
|
|
|
|
|
|
|
1977
|
|
|
|
|
|
|
=item o |
1978
|
|
|
|
|
|
|
|
1979
|
|
|
|
|
|
|
Extended integer, Perl style. This can be either an optional leading |
1980
|
|
|
|
|
|
|
plus or minus sign, followed by a sequence of digits, or an octal |
1981
|
|
|
|
|
|
|
string (a zero, optionally followed by '0', '1', .. '7'), or a |
1982
|
|
|
|
|
|
|
hexadecimal string (C<0x> followed by '0' .. '9', 'a' .. 'f', case |
1983
|
|
|
|
|
|
|
insensitive), or a binary string (C<0b> followed by a series of '0' |
1984
|
|
|
|
|
|
|
and '1'). |
1985
|
|
|
|
|
|
|
|
1986
|
|
|
|
|
|
|
=item f |
1987
|
|
|
|
|
|
|
|
1988
|
|
|
|
|
|
|
Real number. For example C<3.14>, C<-6.23E24> and so on. |
1989
|
|
|
|
|
|
|
|
1990
|
|
|
|
|
|
|
=back |
1991
|
|
|
|
|
|
|
|
1992
|
|
|
|
|
|
|
The I can be C<@> or C<%> to specify that the option is |
1993
|
|
|
|
|
|
|
list or a hash valued. This is only needed when the destination for |
1994
|
|
|
|
|
|
|
the option value is not otherwise specified. It should be omitted when |
1995
|
|
|
|
|
|
|
not needed. |
1996
|
|
|
|
|
|
|
|
1997
|
|
|
|
|
|
|
The I specifies the number of values this option takes per |
1998
|
|
|
|
|
|
|
occurrence on the command line. It has the format C<{> [ I ] [ C<,> [ I ] ] C<}>. |
1999
|
|
|
|
|
|
|
|
2000
|
|
|
|
|
|
|
I denotes the minimal number of arguments. It defaults to 1 for |
2001
|
|
|
|
|
|
|
options with C<=> and to 0 for options with C<:>, see below. Note that |
2002
|
|
|
|
|
|
|
I overrules the C<=> / C<:> semantics. |
2003
|
|
|
|
|
|
|
|
2004
|
|
|
|
|
|
|
I denotes the maximum number of arguments. It must be at least |
2005
|
|
|
|
|
|
|
I. If I is omitted, I, there is no |
2006
|
|
|
|
|
|
|
upper bound to the number of argument values taken. |
2007
|
|
|
|
|
|
|
|
2008
|
|
|
|
|
|
|
=item : I [ I ] |
2009
|
|
|
|
|
|
|
|
2010
|
|
|
|
|
|
|
Like C<=>, but designates the argument as optional. |
2011
|
|
|
|
|
|
|
If omitted, an empty string will be assigned to string values options, |
2012
|
|
|
|
|
|
|
and the value zero to numeric options. |
2013
|
|
|
|
|
|
|
|
2014
|
|
|
|
|
|
|
Note that if a string argument starts with C<-> or C<-->, it will be |
2015
|
|
|
|
|
|
|
considered an option on itself. |
2016
|
|
|
|
|
|
|
|
2017
|
|
|
|
|
|
|
=item : I [ I ] |
2018
|
|
|
|
|
|
|
|
2019
|
|
|
|
|
|
|
Like C<:i>, but if the value is omitted, the I will be assigned. |
2020
|
|
|
|
|
|
|
|
2021
|
|
|
|
|
|
|
=item : + [ I ] |
2022
|
|
|
|
|
|
|
|
2023
|
|
|
|
|
|
|
Like C<:i>, but if the value is omitted, the current value for the |
2024
|
|
|
|
|
|
|
option will be incremented. |
2025
|
|
|
|
|
|
|
|
2026
|
|
|
|
|
|
|
=back |
2027
|
|
|
|
|
|
|
|
2028
|
|
|
|
|
|
|
=head1 Advanced Possibilities |
2029
|
|
|
|
|
|
|
|
2030
|
|
|
|
|
|
|
=head2 Object oriented interface |
2031
|
|
|
|
|
|
|
|
2032
|
|
|
|
|
|
|
Getopt::Long can be used in an object oriented way as well: |
2033
|
|
|
|
|
|
|
|
2034
|
|
|
|
|
|
|
use Getopt::Long; |
2035
|
|
|
|
|
|
|
$p = Getopt::Long::Parser->new; |
2036
|
|
|
|
|
|
|
$p->configure(...configuration options...); |
2037
|
|
|
|
|
|
|
if ($p->getoptions(...options descriptions...)) ... |
2038
|
|
|
|
|
|
|
if ($p->getoptionsfromarray( \@array, ...options descriptions...)) ... |
2039
|
|
|
|
|
|
|
|
2040
|
|
|
|
|
|
|
Configuration options can be passed to the constructor: |
2041
|
|
|
|
|
|
|
|
2042
|
|
|
|
|
|
|
$p = new Getopt::Long::Parser |
2043
|
|
|
|
|
|
|
config => [...configuration options...]; |
2044
|
|
|
|
|
|
|
|
2045
|
|
|
|
|
|
|
=head2 Callback object |
2046
|
|
|
|
|
|
|
|
2047
|
|
|
|
|
|
|
In version 2.37 the first argument to the callback function was |
2048
|
|
|
|
|
|
|
changed from string to object. This was done to make room for |
2049
|
|
|
|
|
|
|
extensions and more detailed control. The object stringifies to the |
2050
|
|
|
|
|
|
|
option name so this change should not introduce compatibility |
2051
|
|
|
|
|
|
|
problems. |
2052
|
|
|
|
|
|
|
|
2053
|
|
|
|
|
|
|
The callback object has the following methods: |
2054
|
|
|
|
|
|
|
|
2055
|
|
|
|
|
|
|
=over |
2056
|
|
|
|
|
|
|
|
2057
|
|
|
|
|
|
|
=item name |
2058
|
|
|
|
|
|
|
|
2059
|
|
|
|
|
|
|
The name of the option, unabbreviated. For an option with multiple |
2060
|
|
|
|
|
|
|
names it return the first (canonical) name. |
2061
|
|
|
|
|
|
|
|
2062
|
|
|
|
|
|
|
=item given |
2063
|
|
|
|
|
|
|
|
2064
|
|
|
|
|
|
|
The name of the option as actually used, unabbreveated. |
2065
|
|
|
|
|
|
|
|
2066
|
|
|
|
|
|
|
=back |
2067
|
|
|
|
|
|
|
|
2068
|
|
|
|
|
|
|
=head2 Thread Safety |
2069
|
|
|
|
|
|
|
|
2070
|
|
|
|
|
|
|
Getopt::Long is thread safe when using ithreads as of Perl 5.8. It is |
2071
|
|
|
|
|
|
|
I thread safe when using the older (experimental and now |
2072
|
|
|
|
|
|
|
obsolete) threads implementation that was added to Perl 5.005. |
2073
|
|
|
|
|
|
|
|
2074
|
|
|
|
|
|
|
=head2 Documentation and help texts |
2075
|
|
|
|
|
|
|
|
2076
|
|
|
|
|
|
|
Getopt::Long encourages the use of Pod::Usage to produce help |
2077
|
|
|
|
|
|
|
messages. For example: |
2078
|
|
|
|
|
|
|
|
2079
|
|
|
|
|
|
|
use Getopt::Long; |
2080
|
|
|
|
|
|
|
use Pod::Usage; |
2081
|
|
|
|
|
|
|
|
2082
|
|
|
|
|
|
|
my $man = 0; |
2083
|
|
|
|
|
|
|
my $help = 0; |
2084
|
|
|
|
|
|
|
|
2085
|
|
|
|
|
|
|
GetOptions('help|?' => \$help, man => \$man) or pod2usage(2); |
2086
|
|
|
|
|
|
|
pod2usage(1) if $help; |
2087
|
|
|
|
|
|
|
pod2usage(-exitval => 0, -verbose => 2) if $man; |
2088
|
|
|
|
|
|
|
|
2089
|
|
|
|
|
|
|
__END__ |
2090
|
|
|
|
|
|
|
|
2091
|
|
|
|
|
|
|
=head1 NAME |
2092
|
|
|
|
|
|
|
|
2093
|
|
|
|
|
|
|
sample - Using Getopt::Long and Pod::Usage |
2094
|
|
|
|
|
|
|
|
2095
|
|
|
|
|
|
|
=head1 SYNOPSIS |
2096
|
|
|
|
|
|
|
|
2097
|
|
|
|
|
|
|
sample [options] [file ...] |
2098
|
|
|
|
|
|
|
|
2099
|
|
|
|
|
|
|
Options: |
2100
|
|
|
|
|
|
|
-help brief help message |
2101
|
|
|
|
|
|
|
-man full documentation |
2102
|
|
|
|
|
|
|
|
2103
|
|
|
|
|
|
|
=head1 OPTIONS |
2104
|
|
|
|
|
|
|
|
2105
|
|
|
|
|
|
|
=over 8 |
2106
|
|
|
|
|
|
|
|
2107
|
|
|
|
|
|
|
=item B<-help> |
2108
|
|
|
|
|
|
|
|
2109
|
|
|
|
|
|
|
Print a brief help message and exits. |
2110
|
|
|
|
|
|
|
|
2111
|
|
|
|
|
|
|
=item B<-man> |
2112
|
|
|
|
|
|
|
|
2113
|
|
|
|
|
|
|
Prints the manual page and exits. |
2114
|
|
|
|
|
|
|
|
2115
|
|
|
|
|
|
|
=back |
2116
|
|
|
|
|
|
|
|
2117
|
|
|
|
|
|
|
=head1 DESCRIPTION |
2118
|
|
|
|
|
|
|
|
2119
|
|
|
|
|
|
|
B will read the given input file(s) and do something |
2120
|
|
|
|
|
|
|
useful with the contents thereof. |
2121
|
|
|
|
|
|
|
|
2122
|
|
|
|
|
|
|
=cut |
2123
|
|
|
|
|
|
|
|
2124
|
|
|
|
|
|
|
See L for details. |
2125
|
|
|
|
|
|
|
|
2126
|
|
|
|
|
|
|
=head2 Parsing options from an arbitrary array |
2127
|
|
|
|
|
|
|
|
2128
|
|
|
|
|
|
|
By default, GetOptions parses the options that are present in the |
2129
|
|
|
|
|
|
|
global array C<@ARGV>. A special entry C can be |
2130
|
|
|
|
|
|
|
used to parse options from an arbitrary array. |
2131
|
|
|
|
|
|
|
|
2132
|
|
|
|
|
|
|
use Getopt::Long qw(GetOptionsFromArray); |
2133
|
|
|
|
|
|
|
$ret = GetOptionsFromArray(\@myopts, ...); |
2134
|
|
|
|
|
|
|
|
2135
|
|
|
|
|
|
|
When used like this, options and their possible values are removed |
2136
|
|
|
|
|
|
|
from C<@myopts>, the global C<@ARGV> is not touched at all. |
2137
|
|
|
|
|
|
|
|
2138
|
|
|
|
|
|
|
The following two calls behave identically: |
2139
|
|
|
|
|
|
|
|
2140
|
|
|
|
|
|
|
$ret = GetOptions( ... ); |
2141
|
|
|
|
|
|
|
$ret = GetOptionsFromArray(\@ARGV, ... ); |
2142
|
|
|
|
|
|
|
|
2143
|
|
|
|
|
|
|
This also means that a first argument hash reference now becomes the |
2144
|
|
|
|
|
|
|
second argument: |
2145
|
|
|
|
|
|
|
|
2146
|
|
|
|
|
|
|
$ret = GetOptions(\%opts, ... ); |
2147
|
|
|
|
|
|
|
$ret = GetOptionsFromArray(\@ARGV, \%opts, ... ); |
2148
|
|
|
|
|
|
|
|
2149
|
|
|
|
|
|
|
=head2 Parsing options from an arbitrary string |
2150
|
|
|
|
|
|
|
|
2151
|
|
|
|
|
|
|
A special entry C can be used to parse options |
2152
|
|
|
|
|
|
|
from an arbitrary string. |
2153
|
|
|
|
|
|
|
|
2154
|
|
|
|
|
|
|
use Getopt::Long qw(GetOptionsFromString); |
2155
|
|
|
|
|
|
|
$ret = GetOptionsFromString($string, ...); |
2156
|
|
|
|
|
|
|
|
2157
|
|
|
|
|
|
|
The contents of the string are split into arguments using a call to |
2158
|
|
|
|
|
|
|
C. As with C, the |
2159
|
|
|
|
|
|
|
global C<@ARGV> is not touched. |
2160
|
|
|
|
|
|
|
|
2161
|
|
|
|
|
|
|
It is possible that, upon completion, not all arguments in the string |
2162
|
|
|
|
|
|
|
have been processed. C will, when called in list |
2163
|
|
|
|
|
|
|
context, return both the return status and an array reference to any |
2164
|
|
|
|
|
|
|
remaining arguments: |
2165
|
|
|
|
|
|
|
|
2166
|
|
|
|
|
|
|
($ret, $args) = GetOptionsFromString($string, ... ); |
2167
|
|
|
|
|
|
|
|
2168
|
|
|
|
|
|
|
If any arguments remain, and C was not called in |
2169
|
|
|
|
|
|
|
list context, a message will be given and C will |
2170
|
|
|
|
|
|
|
return failure. |
2171
|
|
|
|
|
|
|
|
2172
|
|
|
|
|
|
|
As with GetOptionsFromArray, a first argument hash reference now |
2173
|
|
|
|
|
|
|
becomes the second argument. See the next section. |
2174
|
|
|
|
|
|
|
|
2175
|
|
|
|
|
|
|
=head2 Storing options values in a hash |
2176
|
|
|
|
|
|
|
|
2177
|
|
|
|
|
|
|
Sometimes, for example when there are a lot of options, having a |
2178
|
|
|
|
|
|
|
separate variable for each of them can be cumbersome. GetOptions() |
2179
|
|
|
|
|
|
|
supports, as an alternative mechanism, storing options values in a |
2180
|
|
|
|
|
|
|
hash. |
2181
|
|
|
|
|
|
|
|
2182
|
|
|
|
|
|
|
To obtain this, a reference to a hash must be passed I
|
2183
|
|
|
|
|
|
|
argument> to GetOptions(). For each option that is specified on the |
2184
|
|
|
|
|
|
|
command line, the option value will be stored in the hash with the |
2185
|
|
|
|
|
|
|
option name as key. Options that are not actually used on the command |
2186
|
|
|
|
|
|
|
line will not be put in the hash, on other words, |
2187
|
|
|
|
|
|
|
C (or defined()) can be used to test if an option |
2188
|
|
|
|
|
|
|
was used. The drawback is that warnings will be issued if the program |
2189
|
|
|
|
|
|
|
runs under C |
2190
|
|
|
|
|
|
|
exists() or defined() first. |
2191
|
|
|
|
|
|
|
|
2192
|
|
|
|
|
|
|
my %h = (); |
2193
|
|
|
|
|
|
|
GetOptions (\%h, 'length=i'); # will store in $h{length} |
2194
|
|
|
|
|
|
|
|
2195
|
|
|
|
|
|
|
For options that take list or hash values, it is necessary to indicate |
2196
|
|
|
|
|
|
|
this by appending an C<@> or C<%> sign after the type: |
2197
|
|
|
|
|
|
|
|
2198
|
|
|
|
|
|
|
GetOptions (\%h, 'colours=s@'); # will push to @{$h{colours}} |
2199
|
|
|
|
|
|
|
|
2200
|
|
|
|
|
|
|
To make things more complicated, the hash may contain references to |
2201
|
|
|
|
|
|
|
the actual destinations, for example: |
2202
|
|
|
|
|
|
|
|
2203
|
|
|
|
|
|
|
my $len = 0; |
2204
|
|
|
|
|
|
|
my %h = ('length' => \$len); |
2205
|
|
|
|
|
|
|
GetOptions (\%h, 'length=i'); # will store in $len |
2206
|
|
|
|
|
|
|
|
2207
|
|
|
|
|
|
|
This example is fully equivalent with: |
2208
|
|
|
|
|
|
|
|
2209
|
|
|
|
|
|
|
my $len = 0; |
2210
|
|
|
|
|
|
|
GetOptions ('length=i' => \$len); # will store in $len |
2211
|
|
|
|
|
|
|
|
2212
|
|
|
|
|
|
|
Any mixture is possible. For example, the most frequently used options |
2213
|
|
|
|
|
|
|
could be stored in variables while all other options get stored in the |
2214
|
|
|
|
|
|
|
hash: |
2215
|
|
|
|
|
|
|
|
2216
|
|
|
|
|
|
|
my $verbose = 0; # frequently referred |
2217
|
|
|
|
|
|
|
my $debug = 0; # frequently referred |
2218
|
|
|
|
|
|
|
my %h = ('verbose' => \$verbose, 'debug' => \$debug); |
2219
|
|
|
|
|
|
|
GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i'); |
2220
|
|
|
|
|
|
|
if ( $verbose ) { ... } |
2221
|
|
|
|
|
|
|
if ( exists $h{filter} ) { ... option 'filter' was specified ... } |
2222
|
|
|
|
|
|
|
|
2223
|
|
|
|
|
|
|
=head2 Bundling |
2224
|
|
|
|
|
|
|
|
2225
|
|
|
|
|
|
|
With bundling it is possible to set several single-character options |
2226
|
|
|
|
|
|
|
at once. For example if C, C and C are all valid options, |
2227
|
|
|
|
|
|
|
|
2228
|
|
|
|
|
|
|
-vax |
2229
|
|
|
|
|
|
|
|
2230
|
|
|
|
|
|
|
will set all three. |
2231
|
|
|
|
|
|
|
|
2232
|
|
|
|
|
|
|
Getopt::Long supports three styles of bundling. To enable bundling, a |
2233
|
|
|
|
|
|
|
call to Getopt::Long::Configure is required. |
2234
|
|
|
|
|
|
|
|
2235
|
|
|
|
|
|
|
The simplest style of bundling can be enabled with: |
2236
|
|
|
|
|
|
|
|
2237
|
|
|
|
|
|
|
Getopt::Long::Configure ("bundling"); |
2238
|
|
|
|
|
|
|
|
2239
|
|
|
|
|
|
|
Configured this way, single-character options can be bundled but long |
2240
|
|
|
|
|
|
|
options (and any of their auto-abbreviated shortened forms) B |
2241
|
|
|
|
|
|
|
always start with a double dash C<--> to avoid ambiguity. For example, |
2242
|
|
|
|
|
|
|
when C, C, C and C are all valid options, |
2243
|
|
|
|
|
|
|
|
2244
|
|
|
|
|
|
|
-vax |
2245
|
|
|
|
|
|
|
|
2246
|
|
|
|
|
|
|
will set C, C and C, but |
2247
|
|
|
|
|
|
|
|
2248
|
|
|
|
|
|
|
--vax |
2249
|
|
|
|
|
|
|
|
2250
|
|
|
|
|
|
|
will set C. |
2251
|
|
|
|
|
|
|
|
2252
|
|
|
|
|
|
|
The second style of bundling lifts this restriction. It can be enabled |
2253
|
|
|
|
|
|
|
with: |
2254
|
|
|
|
|
|
|
|
2255
|
|
|
|
|
|
|
Getopt::Long::Configure ("bundling_override"); |
2256
|
|
|
|
|
|
|
|
2257
|
|
|
|
|
|
|
Now, C<-vax> will set the option C. |
2258
|
|
|
|
|
|
|
|
2259
|
|
|
|
|
|
|
In all of the above cases, option values may be inserted in the |
2260
|
|
|
|
|
|
|
bundle. For example: |
2261
|
|
|
|
|
|
|
|
2262
|
|
|
|
|
|
|
-h24w80 |
2263
|
|
|
|
|
|
|
|
2264
|
|
|
|
|
|
|
is equivalent to |
2265
|
|
|
|
|
|
|
|
2266
|
|
|
|
|
|
|
-h 24 -w 80 |
2267
|
|
|
|
|
|
|
|
2268
|
|
|
|
|
|
|
A third style of bundling allows only values to be bundled with |
2269
|
|
|
|
|
|
|
options. It can be enabled with: |
2270
|
|
|
|
|
|
|
|
2271
|
|
|
|
|
|
|
Getopt::Long::Configure ("bundling_values"); |
2272
|
|
|
|
|
|
|
|
2273
|
|
|
|
|
|
|
Now, C<-h24> will set the option C to C<24>, but option bundles |
2274
|
|
|
|
|
|
|
like C<-vxa> and C<-h24w80> are flagged as errors. |
2275
|
|
|
|
|
|
|
|
2276
|
|
|
|
|
|
|
Enabling C will disable the other two styles of |
2277
|
|
|
|
|
|
|
bundling. |
2278
|
|
|
|
|
|
|
|
2279
|
|
|
|
|
|
|
When configured for bundling, single-character options are matched |
2280
|
|
|
|
|
|
|
case sensitive while long options are matched case insensitive. To |
2281
|
|
|
|
|
|
|
have the single-character options matched case insensitive as well, |
2282
|
|
|
|
|
|
|
use: |
2283
|
|
|
|
|
|
|
|
2284
|
|
|
|
|
|
|
Getopt::Long::Configure ("bundling", "ignorecase_always"); |
2285
|
|
|
|
|
|
|
|
2286
|
|
|
|
|
|
|
It goes without saying that bundling can be quite confusing. |
2287
|
|
|
|
|
|
|
|
2288
|
|
|
|
|
|
|
=head2 The lonesome dash |
2289
|
|
|
|
|
|
|
|
2290
|
|
|
|
|
|
|
Normally, a lone dash C<-> on the command line will not be considered |
2291
|
|
|
|
|
|
|
an option. Option processing will terminate (unless "permute" is |
2292
|
|
|
|
|
|
|
configured) and the dash will be left in C<@ARGV>. |
2293
|
|
|
|
|
|
|
|
2294
|
|
|
|
|
|
|
It is possible to get special treatment for a lone dash. This can be |
2295
|
|
|
|
|
|
|
achieved by adding an option specification with an empty name, for |
2296
|
|
|
|
|
|
|
example: |
2297
|
|
|
|
|
|
|
|
2298
|
|
|
|
|
|
|
GetOptions ('' => \$stdio); |
2299
|
|
|
|
|
|
|
|
2300
|
|
|
|
|
|
|
A lone dash on the command line will now be a legal option, and using |
2301
|
|
|
|
|
|
|
it will set variable C<$stdio>. |
2302
|
|
|
|
|
|
|
|
2303
|
|
|
|
|
|
|
=head2 Argument callback |
2304
|
|
|
|
|
|
|
|
2305
|
|
|
|
|
|
|
A special option 'name' C<< <> >> can be used to designate a subroutine |
2306
|
|
|
|
|
|
|
to handle non-option arguments. When GetOptions() encounters an |
2307
|
|
|
|
|
|
|
argument that does not look like an option, it will immediately call this |
2308
|
|
|
|
|
|
|
subroutine and passes it one parameter: the argument name. |
2309
|
|
|
|
|
|
|
|
2310
|
|
|
|
|
|
|
For example: |
2311
|
|
|
|
|
|
|
|
2312
|
|
|
|
|
|
|
my $width = 80; |
2313
|
|
|
|
|
|
|
sub process { ... } |
2314
|
|
|
|
|
|
|
GetOptions ('width=i' => \$width, '<>' => \&process); |
2315
|
|
|
|
|
|
|
|
2316
|
|
|
|
|
|
|
When applied to the following command line: |
2317
|
|
|
|
|
|
|
|
2318
|
|
|
|
|
|
|
arg1 --width=72 arg2 --width=60 arg3 |
2319
|
|
|
|
|
|
|
|
2320
|
|
|
|
|
|
|
This will call |
2321
|
|
|
|
|
|
|
C while C<$width> is C<80>, |
2322
|
|
|
|
|
|
|
C while C<$width> is C<72>, and |
2323
|
|
|
|
|
|
|
C while C<$width> is C<60>. |
2324
|
|
|
|
|
|
|
|
2325
|
|
|
|
|
|
|
This feature requires configuration option B, see section |
2326
|
|
|
|
|
|
|
L. |
2327
|
|
|
|
|
|
|
|
2328
|
|
|
|
|
|
|
=head1 Configuring Getopt::Long |
2329
|
|
|
|
|
|
|
|
2330
|
|
|
|
|
|
|
Getopt::Long can be configured by calling subroutine |
2331
|
|
|
|
|
|
|
Getopt::Long::Configure(). This subroutine takes a list of quoted |
2332
|
|
|
|
|
|
|
strings, each specifying a configuration option to be enabled, e.g. |
2333
|
|
|
|
|
|
|
C. To disable, prefix with C or C, e.g. |
2334
|
|
|
|
|
|
|
C. Case does not matter. Multiple calls to Configure() |
2335
|
|
|
|
|
|
|
are possible. |
2336
|
|
|
|
|
|
|
|
2337
|
|
|
|
|
|
|
Alternatively, as of version 2.24, the configuration options may be |
2338
|
|
|
|
|
|
|
passed together with the C |
2339
|
|
|
|
|
|
|
|
2340
|
|
|
|
|
|
|
use Getopt::Long qw(:config no_ignore_case bundling); |
2341
|
|
|
|
|
|
|
|
2342
|
|
|
|
|
|
|
The following options are available: |
2343
|
|
|
|
|
|
|
|
2344
|
|
|
|
|
|
|
=over 12 |
2345
|
|
|
|
|
|
|
|
2346
|
|
|
|
|
|
|
=item default |
2347
|
|
|
|
|
|
|
|
2348
|
|
|
|
|
|
|
This option causes all configuration options to be reset to their |
2349
|
|
|
|
|
|
|
default values. |
2350
|
|
|
|
|
|
|
|
2351
|
|
|
|
|
|
|
=item posix_default |
2352
|
|
|
|
|
|
|
|
2353
|
|
|
|
|
|
|
This option causes all configuration options to be reset to their |
2354
|
|
|
|
|
|
|
default values as if the environment variable POSIXLY_CORRECT had |
2355
|
|
|
|
|
|
|
been set. |
2356
|
|
|
|
|
|
|
|
2357
|
|
|
|
|
|
|
=item auto_abbrev |
2358
|
|
|
|
|
|
|
|
2359
|
|
|
|
|
|
|
Allow option names to be abbreviated to uniqueness. |
2360
|
|
|
|
|
|
|
Default is enabled unless environment variable |
2361
|
|
|
|
|
|
|
POSIXLY_CORRECT has been set, in which case C is disabled. |
2362
|
|
|
|
|
|
|
|
2363
|
|
|
|
|
|
|
=item getopt_compat |
2364
|
|
|
|
|
|
|
|
2365
|
|
|
|
|
|
|
Allow C<+> to start options. |
2366
|
|
|
|
|
|
|
Default is enabled unless environment variable |
2367
|
|
|
|
|
|
|
POSIXLY_CORRECT has been set, in which case C is disabled. |
2368
|
|
|
|
|
|
|
|
2369
|
|
|
|
|
|
|
=item gnu_compat |
2370
|
|
|
|
|
|
|
|
2371
|
|
|
|
|
|
|
C controls whether C<--opt=> is allowed, and what it should |
2372
|
|
|
|
|
|
|
do. Without C, C<--opt=> gives an error. With C, |
2373
|
|
|
|
|
|
|
C<--opt=> will give option C and empty value. |
2374
|
|
|
|
|
|
|
This is the way GNU getopt_long() does it. |
2375
|
|
|
|
|
|
|
|
2376
|
|
|
|
|
|
|
Note that C<--opt value> is still accepted, even though GNU |
2377
|
|
|
|
|
|
|
getopt_long() doesn't. |
2378
|
|
|
|
|
|
|
|
2379
|
|
|
|
|
|
|
=item gnu_getopt |
2380
|
|
|
|
|
|
|
|
2381
|
|
|
|
|
|
|
This is a short way of setting C C C |
2382
|
|
|
|
|
|
|
C. With C, command line handling should be |
2383
|
|
|
|
|
|
|
reasonably compatible with GNU getopt_long(). |
2384
|
|
|
|
|
|
|
|
2385
|
|
|
|
|
|
|
=item require_order |
2386
|
|
|
|
|
|
|
|
2387
|
|
|
|
|
|
|
Whether command line arguments are allowed to be mixed with options. |
2388
|
|
|
|
|
|
|
Default is disabled unless environment variable |
2389
|
|
|
|
|
|
|
POSIXLY_CORRECT has been set, in which case C is enabled. |
2390
|
|
|
|
|
|
|
|
2391
|
|
|
|
|
|
|
See also C, which is the opposite of C. |
2392
|
|
|
|
|
|
|
|
2393
|
|
|
|
|
|
|
=item permute |
2394
|
|
|
|
|
|
|
|
2395
|
|
|
|
|
|
|
Whether command line arguments are allowed to be mixed with options. |
2396
|
|
|
|
|
|
|
Default is enabled unless environment variable |
2397
|
|
|
|
|
|
|
POSIXLY_CORRECT has been set, in which case C is disabled. |
2398
|
|
|
|
|
|
|
Note that C is the opposite of C. |
2399
|
|
|
|
|
|
|
|
2400
|
|
|
|
|
|
|
If C is enabled, this means that |
2401
|
|
|
|
|
|
|
|
2402
|
|
|
|
|
|
|
--foo arg1 --bar arg2 arg3 |
2403
|
|
|
|
|
|
|
|
2404
|
|
|
|
|
|
|
is equivalent to |
2405
|
|
|
|
|
|
|
|
2406
|
|
|
|
|
|
|
--foo --bar arg1 arg2 arg3 |
2407
|
|
|
|
|
|
|
|
2408
|
|
|
|
|
|
|
If an argument callback routine is specified, C<@ARGV> will always be |
2409
|
|
|
|
|
|
|
empty upon successful return of GetOptions() since all options have been |
2410
|
|
|
|
|
|
|
processed. The only exception is when C<--> is used: |
2411
|
|
|
|
|
|
|
|
2412
|
|
|
|
|
|
|
--foo arg1 --bar arg2 -- arg3 |
2413
|
|
|
|
|
|
|
|
2414
|
|
|
|
|
|
|
This will call the callback routine for arg1 and arg2, and then |
2415
|
|
|
|
|
|
|
terminate GetOptions() leaving C<"arg3"> in C<@ARGV>. |
2416
|
|
|
|
|
|
|
|
2417
|
|
|
|
|
|
|
If C is enabled, options processing |
2418
|
|
|
|
|
|
|
terminates when the first non-option is encountered. |
2419
|
|
|
|
|
|
|
|
2420
|
|
|
|
|
|
|
--foo arg1 --bar arg2 arg3 |
2421
|
|
|
|
|
|
|
|
2422
|
|
|
|
|
|
|
is equivalent to |
2423
|
|
|
|
|
|
|
|
2424
|
|
|
|
|
|
|
--foo -- arg1 --bar arg2 arg3 |
2425
|
|
|
|
|
|
|
|
2426
|
|
|
|
|
|
|
If C is also enabled, options processing will terminate |
2427
|
|
|
|
|
|
|
at the first unrecognized option, or non-option, whichever comes |
2428
|
|
|
|
|
|
|
first. |
2429
|
|
|
|
|
|
|
|
2430
|
|
|
|
|
|
|
=item bundling (default: disabled) |
2431
|
|
|
|
|
|
|
|
2432
|
|
|
|
|
|
|
Enabling this option will allow single-character options to be |
2433
|
|
|
|
|
|
|
bundled. To distinguish bundles from long option names, long options |
2434
|
|
|
|
|
|
|
(and any of their auto-abbreviated shortened forms) I be |
2435
|
|
|
|
|
|
|
introduced with C<--> and bundles with C<->. |
2436
|
|
|
|
|
|
|
|
2437
|
|
|
|
|
|
|
Note that, if you have options C, C and C, and |
2438
|
|
|
|
|
|
|
auto_abbrev enabled, possible arguments and option settings are: |
2439
|
|
|
|
|
|
|
|
2440
|
|
|
|
|
|
|
using argument sets option(s) |
2441
|
|
|
|
|
|
|
------------------------------------------ |
2442
|
|
|
|
|
|
|
-a, --a a |
2443
|
|
|
|
|
|
|
-l, --l l |
2444
|
|
|
|
|
|
|
-al, -la, -ala, -all,... a, l |
2445
|
|
|
|
|
|
|
--al, --all all |
2446
|
|
|
|
|
|
|
|
2447
|
|
|
|
|
|
|
The surprising part is that C<--a> sets option C (due to auto |
2448
|
|
|
|
|
|
|
completion), not C. |
2449
|
|
|
|
|
|
|
|
2450
|
|
|
|
|
|
|
Note: disabling C also disables C. |
2451
|
|
|
|
|
|
|
|
2452
|
|
|
|
|
|
|
=item bundling_override (default: disabled) |
2453
|
|
|
|
|
|
|
|
2454
|
|
|
|
|
|
|
If C is enabled, bundling is enabled as with |
2455
|
|
|
|
|
|
|
C but now long option names override option bundles. |
2456
|
|
|
|
|
|
|
|
2457
|
|
|
|
|
|
|
Note: disabling C also disables C. |
2458
|
|
|
|
|
|
|
|
2459
|
|
|
|
|
|
|
B Using option bundling can easily lead to unexpected results, |
2460
|
|
|
|
|
|
|
especially when mixing long options and bundles. Caveat emptor. |
2461
|
|
|
|
|
|
|
|
2462
|
|
|
|
|
|
|
=item ignore_case (default: enabled) |
2463
|
|
|
|
|
|
|
|
2464
|
|
|
|
|
|
|
If enabled, case is ignored when matching option names. If, however, |
2465
|
|
|
|
|
|
|
bundling is enabled as well, single character options will be treated |
2466
|
|
|
|
|
|
|
case-sensitive. |
2467
|
|
|
|
|
|
|
|
2468
|
|
|
|
|
|
|
With C, option specifications for options that only |
2469
|
|
|
|
|
|
|
differ in case, e.g., C<"foo"> and C<"Foo">, will be flagged as |
2470
|
|
|
|
|
|
|
duplicates. |
2471
|
|
|
|
|
|
|
|
2472
|
|
|
|
|
|
|
Note: disabling C also disables C. |
2473
|
|
|
|
|
|
|
|
2474
|
|
|
|
|
|
|
=item ignore_case_always (default: disabled) |
2475
|
|
|
|
|
|
|
|
2476
|
|
|
|
|
|
|
When bundling is in effect, case is ignored on single-character |
2477
|
|
|
|
|
|
|
options also. |
2478
|
|
|
|
|
|
|
|
2479
|
|
|
|
|
|
|
Note: disabling C also disables C. |
2480
|
|
|
|
|
|
|
|
2481
|
|
|
|
|
|
|
=item auto_version (default:disabled) |
2482
|
|
|
|
|
|
|
|
2483
|
|
|
|
|
|
|
Automatically provide support for the B<--version> option if |
2484
|
|
|
|
|
|
|
the application did not specify a handler for this option itself. |
2485
|
|
|
|
|
|
|
|
2486
|
|
|
|
|
|
|
Getopt::Long will provide a standard version message that includes the |
2487
|
|
|
|
|
|
|
program name, its version (if $main::VERSION is defined), and the |
2488
|
|
|
|
|
|
|
versions of Getopt::Long and Perl. The message will be written to |
2489
|
|
|
|
|
|
|
standard output and processing will terminate. |
2490
|
|
|
|
|
|
|
|
2491
|
|
|
|
|
|
|
C will be enabled if the calling program explicitly |
2492
|
|
|
|
|
|
|
specified a version number higher than 2.32 in the C |
2493
|
|
|
|
|
|
|
C statement. |
2494
|
|
|
|
|
|
|
|
2495
|
|
|
|
|
|
|
=item auto_help (default:disabled) |
2496
|
|
|
|
|
|
|
|
2497
|
|
|
|
|
|
|
Automatically provide support for the B<--help> and B<-?> options if |
2498
|
|
|
|
|
|
|
the application did not specify a handler for this option itself. |
2499
|
|
|
|
|
|
|
|
2500
|
|
|
|
|
|
|
Getopt::Long will provide a help message using module L. The |
2501
|
|
|
|
|
|
|
message, derived from the SYNOPSIS POD section, will be written to |
2502
|
|
|
|
|
|
|
standard output and processing will terminate. |
2503
|
|
|
|
|
|
|
|
2504
|
|
|
|
|
|
|
C will be enabled if the calling program explicitly |
2505
|
|
|
|
|
|
|
specified a version number higher than 2.32 in the C |
2506
|
|
|
|
|
|
|
C statement. |
2507
|
|
|
|
|
|
|
|
2508
|
|
|
|
|
|
|
=item pass_through (default: disabled) |
2509
|
|
|
|
|
|
|
|
2510
|
|
|
|
|
|
|
With C anything that is unknown, ambiguous or supplied with |
2511
|
|
|
|
|
|
|
an invalid option will not be flagged as an error. Instead the unknown |
2512
|
|
|
|
|
|
|
option(s) will be passed to the catchall C<< <> >> if present, otherwise |
2513
|
|
|
|
|
|
|
through to C<@ARGV>. This makes it possible to write wrapper scripts that |
2514
|
|
|
|
|
|
|
process only part of the user supplied command line arguments, and pass the |
2515
|
|
|
|
|
|
|
remaining options to some other program. |
2516
|
|
|
|
|
|
|
|
2517
|
|
|
|
|
|
|
If C is enabled, options processing will terminate at the |
2518
|
|
|
|
|
|
|
first unrecognized option, or non-option, whichever comes first and all |
2519
|
|
|
|
|
|
|
remaining arguments are passed to C<@ARGV> instead of the catchall |
2520
|
|
|
|
|
|
|
C<< <> >> if present. However, if C is enabled instead, results |
2521
|
|
|
|
|
|
|
can become confusing. |
2522
|
|
|
|
|
|
|
|
2523
|
|
|
|
|
|
|
Note that the options terminator (default C<-->), if present, will |
2524
|
|
|
|
|
|
|
also be passed through in C<@ARGV>. |
2525
|
|
|
|
|
|
|
|
2526
|
|
|
|
|
|
|
=item prefix |
2527
|
|
|
|
|
|
|
|
2528
|
|
|
|
|
|
|
The string that starts options. If a constant string is not |
2529
|
|
|
|
|
|
|
sufficient, see C. |
2530
|
|
|
|
|
|
|
|
2531
|
|
|
|
|
|
|
=item prefix_pattern |
2532
|
|
|
|
|
|
|
|
2533
|
|
|
|
|
|
|
A Perl pattern that identifies the strings that introduce options. |
2534
|
|
|
|
|
|
|
Default is C<--|-|\+> unless environment variable |
2535
|
|
|
|
|
|
|
POSIXLY_CORRECT has been set, in which case it is C<--|->. |
2536
|
|
|
|
|
|
|
|
2537
|
|
|
|
|
|
|
=item long_prefix_pattern |
2538
|
|
|
|
|
|
|
|
2539
|
|
|
|
|
|
|
A Perl pattern that allows the disambiguation of long and short |
2540
|
|
|
|
|
|
|
prefixes. Default is C<-->. |
2541
|
|
|
|
|
|
|
|
2542
|
|
|
|
|
|
|
Typically you only need to set this if you are using nonstandard |
2543
|
|
|
|
|
|
|
prefixes and want some or all of them to have the same semantics as |
2544
|
|
|
|
|
|
|
'--' does under normal circumstances. |
2545
|
|
|
|
|
|
|
|
2546
|
|
|
|
|
|
|
For example, setting prefix_pattern to C<--|-|\+|\/> and |
2547
|
|
|
|
|
|
|
long_prefix_pattern to C<--|\/> would add Win32 style argument |
2548
|
|
|
|
|
|
|
handling. |
2549
|
|
|
|
|
|
|
|
2550
|
|
|
|
|
|
|
=item debug (default: disabled) |
2551
|
|
|
|
|
|
|
|
2552
|
|
|
|
|
|
|
Enable debugging output. |
2553
|
|
|
|
|
|
|
|
2554
|
|
|
|
|
|
|
=back |
2555
|
|
|
|
|
|
|
|
2556
|
|
|
|
|
|
|
=head1 Exportable Methods |
2557
|
|
|
|
|
|
|
|
2558
|
|
|
|
|
|
|
=over |
2559
|
|
|
|
|
|
|
|
2560
|
|
|
|
|
|
|
=item VersionMessage |
2561
|
|
|
|
|
|
|
|
2562
|
|
|
|
|
|
|
This subroutine provides a standard version message. Its argument can be: |
2563
|
|
|
|
|
|
|
|
2564
|
|
|
|
|
|
|
=over 4 |
2565
|
|
|
|
|
|
|
|
2566
|
|
|
|
|
|
|
=item * |
2567
|
|
|
|
|
|
|
|
2568
|
|
|
|
|
|
|
A string containing the text of a message to print I printing |
2569
|
|
|
|
|
|
|
the standard message. |
2570
|
|
|
|
|
|
|
|
2571
|
|
|
|
|
|
|
=item * |
2572
|
|
|
|
|
|
|
|
2573
|
|
|
|
|
|
|
A numeric value corresponding to the desired exit status. |
2574
|
|
|
|
|
|
|
|
2575
|
|
|
|
|
|
|
=item * |
2576
|
|
|
|
|
|
|
|
2577
|
|
|
|
|
|
|
A reference to a hash. |
2578
|
|
|
|
|
|
|
|
2579
|
|
|
|
|
|
|
=back |
2580
|
|
|
|
|
|
|
|
2581
|
|
|
|
|
|
|
If more than one argument is given then the entire argument list is |
2582
|
|
|
|
|
|
|
assumed to be a hash. If a hash is supplied (either as a reference or |
2583
|
|
|
|
|
|
|
as a list) it should contain one or more elements with the following |
2584
|
|
|
|
|
|
|
keys: |
2585
|
|
|
|
|
|
|
|
2586
|
|
|
|
|
|
|
=over 4 |
2587
|
|
|
|
|
|
|
|
2588
|
|
|
|
|
|
|
=item C<-message> |
2589
|
|
|
|
|
|
|
|
2590
|
|
|
|
|
|
|
=item C<-msg> |
2591
|
|
|
|
|
|
|
|
2592
|
|
|
|
|
|
|
The text of a message to print immediately prior to printing the |
2593
|
|
|
|
|
|
|
program's usage message. |
2594
|
|
|
|
|
|
|
|
2595
|
|
|
|
|
|
|
=item C<-exitval> |
2596
|
|
|
|
|
|
|
|
2597
|
|
|
|
|
|
|
The desired exit status to pass to the B function. |
2598
|
|
|
|
|
|
|
This should be an integer, or else the string "NOEXIT" to |
2599
|
|
|
|
|
|
|
indicate that control should simply be returned without |
2600
|
|
|
|
|
|
|
terminating the invoking process. |
2601
|
|
|
|
|
|
|
|
2602
|
|
|
|
|
|
|
=item C<-output> |
2603
|
|
|
|
|
|
|
|
2604
|
|
|
|
|
|
|
A reference to a filehandle, or the pathname of a file to which the |
2605
|
|
|
|
|
|
|
usage message should be written. The default is C<\*STDERR> unless the |
2606
|
|
|
|
|
|
|
exit value is less than 2 (in which case the default is C<\*STDOUT>). |
2607
|
|
|
|
|
|
|
|
2608
|
|
|
|
|
|
|
=back |
2609
|
|
|
|
|
|
|
|
2610
|
|
|
|
|
|
|
You cannot tie this routine directly to an option, e.g.: |
2611
|
|
|
|
|
|
|
|
2612
|
|
|
|
|
|
|
GetOptions("version" => \&VersionMessage); |
2613
|
|
|
|
|
|
|
|
2614
|
|
|
|
|
|
|
Use this instead: |
2615
|
|
|
|
|
|
|
|
2616
|
|
|
|
|
|
|
GetOptions("version" => sub { VersionMessage() }); |
2617
|
|
|
|
|
|
|
|
2618
|
|
|
|
|
|
|
=item HelpMessage |
2619
|
|
|
|
|
|
|
|
2620
|
|
|
|
|
|
|
This subroutine produces a standard help message, derived from the |
2621
|
|
|
|
|
|
|
program's POD section SYNOPSIS using L. It takes the same |
2622
|
|
|
|
|
|
|
arguments as VersionMessage(). In particular, you cannot tie it |
2623
|
|
|
|
|
|
|
directly to an option, e.g.: |
2624
|
|
|
|
|
|
|
|
2625
|
|
|
|
|
|
|
GetOptions("help" => \&HelpMessage); |
2626
|
|
|
|
|
|
|
|
2627
|
|
|
|
|
|
|
Use this instead: |
2628
|
|
|
|
|
|
|
|
2629
|
|
|
|
|
|
|
GetOptions("help" => sub { HelpMessage() }); |
2630
|
|
|
|
|
|
|
|
2631
|
|
|
|
|
|
|
=back |
2632
|
|
|
|
|
|
|
|
2633
|
|
|
|
|
|
|
=head1 Return values and Errors |
2634
|
|
|
|
|
|
|
|
2635
|
|
|
|
|
|
|
Configuration errors and errors in the option definitions are |
2636
|
|
|
|
|
|
|
signalled using die() and will terminate the calling program unless |
2637
|
|
|
|
|
|
|
the call to Getopt::Long::GetOptions() was embedded in C
|
2638
|
|
|
|
|
|
|
}>, or die() was trapped using C<$SIG{__DIE__}>. |
2639
|
|
|
|
|
|
|
|
2640
|
|
|
|
|
|
|
GetOptions returns true to indicate success. |
2641
|
|
|
|
|
|
|
It returns false when the function detected one or more errors during |
2642
|
|
|
|
|
|
|
option parsing. These errors are signalled using warn() and can be |
2643
|
|
|
|
|
|
|
trapped with C<$SIG{__WARN__}>. |
2644
|
|
|
|
|
|
|
|
2645
|
|
|
|
|
|
|
=head1 Legacy |
2646
|
|
|
|
|
|
|
|
2647
|
|
|
|
|
|
|
The earliest development of C started in 1990, with Perl |
2648
|
|
|
|
|
|
|
version 4. As a result, its development, and the development of |
2649
|
|
|
|
|
|
|
Getopt::Long, has gone through several stages. Since backward |
2650
|
|
|
|
|
|
|
compatibility has always been extremely important, the current version |
2651
|
|
|
|
|
|
|
of Getopt::Long still supports a lot of constructs that nowadays are |
2652
|
|
|
|
|
|
|
no longer necessary or otherwise unwanted. This section describes |
2653
|
|
|
|
|
|
|
briefly some of these 'features'. |
2654
|
|
|
|
|
|
|
|
2655
|
|
|
|
|
|
|
=head2 Default destinations |
2656
|
|
|
|
|
|
|
|
2657
|
|
|
|
|
|
|
When no destination is specified for an option, GetOptions will store |
2658
|
|
|
|
|
|
|
the resultant value in a global variable named CI, where |
2659
|
|
|
|
|
|
|
I is the primary name of this option. When a program executes |
2660
|
|
|
|
|
|
|
under C |
2661
|
|
|
|
|
|
|
pre-declared with our() or C |
2662
|
|
|
|
|
|
|
|
2663
|
|
|
|
|
|
|
our $opt_length = 0; |
2664
|
|
|
|
|
|
|
GetOptions ('length=i'); # will store in $opt_length |
2665
|
|
|
|
|
|
|
|
2666
|
|
|
|
|
|
|
To yield a usable Perl variable, characters that are not part of the |
2667
|
|
|
|
|
|
|
syntax for variables are translated to underscores. For example, |
2668
|
|
|
|
|
|
|
C<--fpp-struct-return> will set the variable |
2669
|
|
|
|
|
|
|
C<$opt_fpp_struct_return>. Note that this variable resides in the |
2670
|
|
|
|
|
|
|
namespace of the calling program, not necessarily C. For |
2671
|
|
|
|
|
|
|
example: |
2672
|
|
|
|
|
|
|
|
2673
|
|
|
|
|
|
|
GetOptions ("size=i", "sizes=i@"); |
2674
|
|
|
|
|
|
|
|
2675
|
|
|
|
|
|
|
with command line "-size 10 -sizes 24 -sizes 48" will perform the |
2676
|
|
|
|
|
|
|
equivalent of the assignments |
2677
|
|
|
|
|
|
|
|
2678
|
|
|
|
|
|
|
$opt_size = 10; |
2679
|
|
|
|
|
|
|
@opt_sizes = (24, 48); |
2680
|
|
|
|
|
|
|
|
2681
|
|
|
|
|
|
|
=head2 Alternative option starters |
2682
|
|
|
|
|
|
|
|
2683
|
|
|
|
|
|
|
A string of alternative option starter characters may be passed as the |
2684
|
|
|
|
|
|
|
first argument (or the first argument after a leading hash reference |
2685
|
|
|
|
|
|
|
argument). |
2686
|
|
|
|
|
|
|
|
2687
|
|
|
|
|
|
|
my $len = 0; |
2688
|
|
|
|
|
|
|
GetOptions ('/', 'length=i' => $len); |
2689
|
|
|
|
|
|
|
|
2690
|
|
|
|
|
|
|
Now the command line may look like: |
2691
|
|
|
|
|
|
|
|
2692
|
|
|
|
|
|
|
/length 24 -- arg |
2693
|
|
|
|
|
|
|
|
2694
|
|
|
|
|
|
|
Note that to terminate options processing still requires a double dash |
2695
|
|
|
|
|
|
|
C<-->. |
2696
|
|
|
|
|
|
|
|
2697
|
|
|
|
|
|
|
GetOptions() will not interpret a leading C<< "<>" >> as option starters |
2698
|
|
|
|
|
|
|
if the next argument is a reference. To force C<< "<" >> and C<< ">" >> as |
2699
|
|
|
|
|
|
|
option starters, use C<< "><" >>. Confusing? Well, B
|
2700
|
|
|
|
|
|
|
argument is strongly deprecated> anyway. |
2701
|
|
|
|
|
|
|
|
2702
|
|
|
|
|
|
|
=head2 Configuration variables |
2703
|
|
|
|
|
|
|
|
2704
|
|
|
|
|
|
|
Previous versions of Getopt::Long used variables for the purpose of |
2705
|
|
|
|
|
|
|
configuring. Although manipulating these variables still work, it is |
2706
|
|
|
|
|
|
|
strongly encouraged to use the C routine that was introduced |
2707
|
|
|
|
|
|
|
in version 2.17. Besides, it is much easier. |
2708
|
|
|
|
|
|
|
|
2709
|
|
|
|
|
|
|
=head1 Tips and Techniques |
2710
|
|
|
|
|
|
|
|
2711
|
|
|
|
|
|
|
=head2 Pushing multiple values in a hash option |
2712
|
|
|
|
|
|
|
|
2713
|
|
|
|
|
|
|
Sometimes you want to combine the best of hashes and arrays. For |
2714
|
|
|
|
|
|
|
example, the command line: |
2715
|
|
|
|
|
|
|
|
2716
|
|
|
|
|
|
|
--list add=first --list add=second --list add=third |
2717
|
|
|
|
|
|
|
|
2718
|
|
|
|
|
|
|
where each successive 'list add' option will push the value of add |
2719
|
|
|
|
|
|
|
into array ref $list->{'add'}. The result would be like |
2720
|
|
|
|
|
|
|
|
2721
|
|
|
|
|
|
|
$list->{add} = [qw(first second third)]; |
2722
|
|
|
|
|
|
|
|
2723
|
|
|
|
|
|
|
This can be accomplished with a destination routine: |
2724
|
|
|
|
|
|
|
|
2725
|
|
|
|
|
|
|
GetOptions('list=s%' => |
2726
|
|
|
|
|
|
|
sub { push(@{$list{$_[1]}}, $_[2]) }); |
2727
|
|
|
|
|
|
|
|
2728
|
|
|
|
|
|
|
=head1 Troubleshooting |
2729
|
|
|
|
|
|
|
|
2730
|
|
|
|
|
|
|
=head2 GetOptions does not return a false result when an option is not supplied |
2731
|
|
|
|
|
|
|
|
2732
|
|
|
|
|
|
|
That's why they're called 'options'. |
2733
|
|
|
|
|
|
|
|
2734
|
|
|
|
|
|
|
=head2 GetOptions does not split the command line correctly |
2735
|
|
|
|
|
|
|
|
2736
|
|
|
|
|
|
|
The command line is not split by GetOptions, but by the command line |
2737
|
|
|
|
|
|
|
interpreter (CLI). On Unix, this is the shell. On Windows, it is |
2738
|
|
|
|
|
|
|
COMMAND.COM or CMD.EXE. Other operating systems have other CLIs. |
2739
|
|
|
|
|
|
|
|
2740
|
|
|
|
|
|
|
It is important to know that these CLIs may behave different when the |
2741
|
|
|
|
|
|
|
command line contains special characters, in particular quotes or |
2742
|
|
|
|
|
|
|
backslashes. For example, with Unix shells you can use single quotes |
2743
|
|
|
|
|
|
|
(C<'>) and double quotes (C<">) to group words together. The following |
2744
|
|
|
|
|
|
|
alternatives are equivalent on Unix: |
2745
|
|
|
|
|
|
|
|
2746
|
|
|
|
|
|
|
"two words" |
2747
|
|
|
|
|
|
|
'two words' |
2748
|
|
|
|
|
|
|
two\ words |
2749
|
|
|
|
|
|
|
|
2750
|
|
|
|
|
|
|
In case of doubt, insert the following statement in front of your Perl |
2751
|
|
|
|
|
|
|
program: |
2752
|
|
|
|
|
|
|
|
2753
|
|
|
|
|
|
|
print STDERR (join("|",@ARGV),"\n"); |
2754
|
|
|
|
|
|
|
|
2755
|
|
|
|
|
|
|
to verify how your CLI passes the arguments to the program. |
2756
|
|
|
|
|
|
|
|
2757
|
|
|
|
|
|
|
=head2 Undefined subroutine &main::GetOptions called |
2758
|
|
|
|
|
|
|
|
2759
|
|
|
|
|
|
|
Are you running Windows, and did you write |
2760
|
|
|
|
|
|
|
|
2761
|
|
|
|
|
|
|
use GetOpt::Long; |
2762
|
|
|
|
|
|
|
|
2763
|
|
|
|
|
|
|
(note the capital 'O')? |
2764
|
|
|
|
|
|
|
|
2765
|
|
|
|
|
|
|
=head2 How do I put a "-?" option into a Getopt::Long? |
2766
|
|
|
|
|
|
|
|
2767
|
|
|
|
|
|
|
You can only obtain this using an alias, and Getopt::Long of at least |
2768
|
|
|
|
|
|
|
version 2.13. |
2769
|
|
|
|
|
|
|
|
2770
|
|
|
|
|
|
|
use Getopt::Long; |
2771
|
|
|
|
|
|
|
GetOptions ("help|?"); # -help and -? will both set $opt_help |
2772
|
|
|
|
|
|
|
|
2773
|
|
|
|
|
|
|
Other characters that can't appear in Perl identifiers are also |
2774
|
|
|
|
|
|
|
supported in aliases with Getopt::Long of at version 2.39. Note that |
2775
|
|
|
|
|
|
|
the characters C, C<|>, C<+>, C<=>, and C<:> can only appear as the |
2776
|
|
|
|
|
|
|
first (or only) character of an alias. |
2777
|
|
|
|
|
|
|
|
2778
|
|
|
|
|
|
|
As of version 2.32 Getopt::Long provides auto-help, a quick and easy way |
2779
|
|
|
|
|
|
|
to add the options --help and -? to your program, and handle them. |
2780
|
|
|
|
|
|
|
|
2781
|
|
|
|
|
|
|
See C in section L. |
2782
|
|
|
|
|
|
|
|
2783
|
|
|
|
|
|
|
=head1 AUTHOR |
2784
|
|
|
|
|
|
|
|
2785
|
|
|
|
|
|
|
Johan Vromans |
2786
|
|
|
|
|
|
|
|
2787
|
|
|
|
|
|
|
=head1 COPYRIGHT AND DISCLAIMER |
2788
|
|
|
|
|
|
|
|
2789
|
|
|
|
|
|
|
This program is Copyright 1990,2015 by Johan Vromans. |
2790
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
2791
|
|
|
|
|
|
|
modify it under the terms of the Perl Artistic License or the |
2792
|
|
|
|
|
|
|
GNU General Public License as published by the Free Software |
2793
|
|
|
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any |
2794
|
|
|
|
|
|
|
later version. |
2795
|
|
|
|
|
|
|
|
2796
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
2797
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2798
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2799
|
|
|
|
|
|
|
GNU General Public License for more details. |
2800
|
|
|
|
|
|
|
|
2801
|
|
|
|
|
|
|
If you do not have a copy of the GNU General Public License write to |
2802
|
|
|
|
|
|
|
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, |
2803
|
|
|
|
|
|
|
MA 02139, USA. |
2804
|
|
|
|
|
|
|
|
2805
|
|
|
|
|
|
|
=cut |
2806
|
|
|
|
|
|
|
|