line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- Mode: perl -*- |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
22286
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
102
|
|
4
|
4
|
|
|
4
|
|
67
|
use 5.005; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
124
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Getargs::Long; |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
11
|
use vars qw($VERSION @ISA @EXPORT); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
368
|
|
9
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d%02d", q/1.10.7/ =~ /(\d+)/g; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
BEGIN |
12
|
|
|
|
|
|
|
{ |
13
|
4
|
50
|
|
4
|
|
57
|
die "This module is known to exercise a bug in 5.6.0. Please upgrade your perl.\n" |
14
|
|
|
|
|
|
|
if $] eq '5.006'; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
4
|
|
|
4
|
|
1563
|
use Log::Agent; |
|
4
|
|
|
|
|
24999
|
|
|
4
|
|
|
|
|
273
|
|
18
|
4
|
|
|
4
|
|
1836
|
use Data::Dumper; |
|
4
|
|
|
|
|
17606
|
|
|
4
|
|
|
|
|
265
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
require Exporter; |
21
|
4
|
|
|
4
|
|
21
|
use vars qw(@EXPORT); |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
6963
|
|
22
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
23
|
|
|
|
|
|
|
@EXPORT = qw(getargs cgetargs xgetargs cxgetargs); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
# %ignore |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# Cache whether argument names are to be handled case-insensitively or not, |
29
|
|
|
|
|
|
|
# on a package basis. Default is case-sensitive processing. |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
my %ignore = (); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
# ->import |
35
|
|
|
|
|
|
|
# |
36
|
|
|
|
|
|
|
# Trap Exporter's one to handle 'ignorecase' here (or lack thereof). |
37
|
|
|
|
|
|
|
# Then use ->export_to_level() to tell Exporter to continue the export |
38
|
|
|
|
|
|
|
# as if its import method had been called directly via inheritance. |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
sub import { |
41
|
6
|
|
|
6
|
|
36
|
my $module = shift; |
42
|
6
|
|
|
|
|
29
|
my @syms = grep($_ ne 'ignorecase', @_); |
43
|
6
|
|
|
|
|
10
|
my $callpkg = caller; |
44
|
|
|
|
|
|
|
|
45
|
6
|
100
|
|
|
|
15
|
if (@syms == @_) { # There was no "ignorecase" seen |
46
|
3
|
|
|
|
|
4
|
delete $ignore{$callpkg}; |
47
|
3
|
|
|
|
|
28
|
logdbg 'info', "will process arguments case-sensitively in $callpkg"; |
48
|
|
|
|
|
|
|
} else { |
49
|
3
|
|
|
|
|
5
|
$ignore{$callpkg} = 1; |
50
|
3
|
|
|
|
|
31
|
logdbg 'info', "will process arguments case-insensitively in $callpkg"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
6
|
|
|
|
|
5262
|
Getargs::Long->export_to_level(1, $module, @syms); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# %subcache |
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
# Cache validation routine, indexed by "package::routine". |
60
|
|
|
|
|
|
|
# |
61
|
|
|
|
|
|
|
my %subcache = (); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# |
64
|
|
|
|
|
|
|
# getargs |
65
|
|
|
|
|
|
|
# |
66
|
|
|
|
|
|
|
# Parse arguments for subroutine, and validate them if typechecking requested. |
67
|
|
|
|
|
|
|
# Optional arguments with no default return undef. Mandatory arguments cannot |
68
|
|
|
|
|
|
|
# be undefined. |
69
|
|
|
|
|
|
|
# |
70
|
9
|
|
|
9
|
1
|
8863
|
sub getargs (\@@) { _getargs(scalar(caller), 0, "", @_) } |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# cgetargs |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# Same as getargs, but cache data for next call. |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# When called from within an eval, caching is not possible, so this routine |
78
|
|
|
|
|
|
|
# must not be called. |
79
|
|
|
|
|
|
|
# |
80
|
|
|
|
|
|
|
sub cgetargs (\@@) { |
81
|
0
|
|
|
0
|
1
|
0
|
my $sub = (caller(1))[3]; # Anomaly in caller(), will also get pkg name |
82
|
0
|
0
|
|
|
|
0
|
logcroak "can't call cgetargs from within an eval" |
83
|
|
|
|
|
|
|
if $sub =~ /^\(eval/; |
84
|
0
|
|
|
|
|
0
|
_getargs(scalar(caller), 0, $sub, @_) |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
# xgetargs |
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
# Like getargs(), but with extended specifications allowing to specify |
91
|
|
|
|
|
|
|
# defaults for non-mandatory arguments. |
92
|
|
|
|
|
|
|
# |
93
|
8
|
|
|
8
|
1
|
13501
|
sub xgetargs (\@@) { _getargs(scalar(caller), 1, "", @_) } |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# |
96
|
|
|
|
|
|
|
# cxgetargs |
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
# Like cgetargs(), but with extended specifications allowing to specify |
99
|
|
|
|
|
|
|
# defaults for non-mandatory arguments. Be careful: those defaults are |
100
|
|
|
|
|
|
|
# deep-cloned and "frozen", so to speak. |
101
|
|
|
|
|
|
|
# |
102
|
|
|
|
|
|
|
# When called from within an eval, caching is not possible, so this routine |
103
|
|
|
|
|
|
|
# must not be called. |
104
|
|
|
|
|
|
|
# |
105
|
|
|
|
|
|
|
sub cxgetargs (\@@) { |
106
|
8
|
|
|
8
|
0
|
10012
|
my $sub = (caller(1))[3]; # Anomaly in caller(), will also get pkg name |
107
|
8
|
50
|
|
|
|
25
|
logcroak "can't call cxgetargs from within an eval" |
108
|
|
|
|
|
|
|
if $sub =~ /^\(eval/; |
109
|
8
|
|
|
|
|
18
|
_getargs(scalar(caller), 1, $sub, @_) |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# |
113
|
|
|
|
|
|
|
# _getargs |
114
|
|
|
|
|
|
|
# |
115
|
|
|
|
|
|
|
# Factorized work for *getargs() routines |
116
|
|
|
|
|
|
|
# |
117
|
|
|
|
|
|
|
# Our signature is: |
118
|
|
|
|
|
|
|
# |
119
|
|
|
|
|
|
|
# _getargs( |
120
|
|
|
|
|
|
|
# # arguments added by our wrappers |
121
|
|
|
|
|
|
|
# $callpkg, $extended, $subname, |
122
|
|
|
|
|
|
|
# # argument list to parse |
123
|
|
|
|
|
|
|
# \@x, |
124
|
|
|
|
|
|
|
# # optional switches |
125
|
|
|
|
|
|
|
# { |
126
|
|
|
|
|
|
|
# -strict => 1, # unknown switches are fatal |
127
|
|
|
|
|
|
|
# -inplace => 1, # edit \@x inplace: remove parsed args |
128
|
|
|
|
|
|
|
# -ignorecase => 1, # override package's global |
129
|
|
|
|
|
|
|
# -extra => 0, # suppress return of extra arguments |
130
|
|
|
|
|
|
|
# }, |
131
|
|
|
|
|
|
|
# # argument definition list |
132
|
|
|
|
|
|
|
# |
133
|
|
|
|
|
|
|
# ); |
134
|
|
|
|
|
|
|
# |
135
|
|
|
|
|
|
|
# With: |
136
|
|
|
|
|
|
|
# $callpkg Calling package |
137
|
|
|
|
|
|
|
# $extended Are they using x*getargs()? |
138
|
|
|
|
|
|
|
# $subname Cache key, if we use it |
139
|
|
|
|
|
|
|
# |
140
|
|
|
|
|
|
|
# Returns the list of values in the same order given in the definition list |
141
|
|
|
|
|
|
|
# (the part), followed by the extra arguments we did not recognize, |
142
|
|
|
|
|
|
|
# with leading '-' removal and transformation to lowercase if ignorecase is on. |
143
|
|
|
|
|
|
|
# |
144
|
|
|
|
|
|
|
sub _getargs { |
145
|
25
|
|
|
25
|
|
50
|
my ($callpkg, $extended, $subname, $args) = splice(@_, 0, 4); |
146
|
|
|
|
|
|
|
|
147
|
25
|
50
|
|
|
|
64
|
logconfess "first argument must be a reference to the argument list" |
148
|
|
|
|
|
|
|
unless ref $args eq 'ARRAY'; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# |
151
|
|
|
|
|
|
|
# Check cache if told to do so. |
152
|
|
|
|
|
|
|
# |
153
|
|
|
|
|
|
|
|
154
|
25
|
100
|
|
|
|
46
|
if ($subname ne '') { |
155
|
8
|
|
|
|
|
10
|
my $sref = $subcache{$subname}; |
156
|
8
|
100
|
|
|
|
11
|
if (defined $sref) { |
157
|
6
|
|
|
|
|
120
|
logdbg 'info', "calling cached subroutine $sref"; |
158
|
6
|
|
|
|
|
401
|
return &$sref($args); |
159
|
|
|
|
|
|
|
} else { |
160
|
2
|
|
|
|
|
43
|
logdbg 'info', "no cached subroutine yet for $subname"; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# |
165
|
|
|
|
|
|
|
# Nothing in cache, or cache was disabled. |
166
|
|
|
|
|
|
|
# |
167
|
|
|
|
|
|
|
|
168
|
19
|
100
|
|
|
|
145
|
my $case_insensitive = $ignore{$callpkg} ? 1 : 0; |
169
|
19
|
|
|
|
|
387
|
logdbg 'info', "case_insensitive=$case_insensitive for package $callpkg"; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# |
172
|
|
|
|
|
|
|
# If next argument is a HASH, then it's a set of extra switches that |
173
|
|
|
|
|
|
|
# may alter our behaviour. Parse them manually. |
174
|
|
|
|
|
|
|
# |
175
|
|
|
|
|
|
|
# Following are the defaults: |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
|
178
|
19
|
|
|
|
|
1051
|
my $strict = 1; # Fatal error on unknown switches |
179
|
19
|
|
|
|
|
18
|
my $inplace = 0; # No inplace editing of arguments |
180
|
19
|
|
|
|
|
12
|
my $extra; # Don't return extra args by default |
181
|
|
|
|
|
|
|
|
182
|
19
|
100
|
|
|
|
45
|
if (ref $_[0] eq 'HASH') { |
183
|
8
|
|
|
|
|
7
|
my $swref = shift; |
184
|
|
|
|
|
|
|
|
185
|
8
|
|
|
|
|
32
|
my %set = ( |
186
|
|
|
|
|
|
|
-strict => \$strict, |
187
|
|
|
|
|
|
|
-ignorecase => \$case_insensitive, |
188
|
|
|
|
|
|
|
-inplace => \$inplace, |
189
|
|
|
|
|
|
|
-extra => \$extra, |
190
|
|
|
|
|
|
|
); |
191
|
|
|
|
|
|
|
|
192
|
8
|
|
|
|
|
22
|
while (my ($sw, $val) = each %$swref) { |
193
|
19
|
|
|
|
|
18
|
my $vset = $set{lc($sw)}; |
194
|
19
|
50
|
|
|
|
28
|
logcroak "unknown switch $sw" unless ref $vset; |
195
|
19
|
|
|
|
|
37
|
$$vset = $val; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# |
199
|
|
|
|
|
|
|
# If they did not set -extra, compute suitable default: false |
200
|
|
|
|
|
|
|
# when -strict, true otherwise. |
201
|
|
|
|
|
|
|
# |
202
|
|
|
|
|
|
|
|
203
|
8
|
50
|
|
|
|
18
|
$extra = $strict ? 0 : 1 unless defined $extra; |
|
|
100
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# |
206
|
|
|
|
|
|
|
# If strict, we ignore true settings for -inplace and -extra |
207
|
|
|
|
|
|
|
# |
208
|
|
|
|
|
|
|
|
209
|
8
|
50
|
|
|
|
16
|
if ($strict) { |
210
|
0
|
0
|
|
|
|
0
|
if ($inplace) { |
211
|
0
|
|
|
|
|
0
|
logcarp "ignoring -inplace when -strict"; |
212
|
0
|
|
|
|
|
0
|
$inplace = 0; |
213
|
|
|
|
|
|
|
} |
214
|
0
|
0
|
|
|
|
0
|
if ($extra) { |
215
|
0
|
|
|
|
|
0
|
logcarp "ignoring -extra when -strict"; |
216
|
0
|
|
|
|
|
0
|
$extra = 0; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# |
222
|
|
|
|
|
|
|
# If we have one argument, it may be '[list]' or 'x'. |
223
|
|
|
|
|
|
|
# In extended mode, we must have an even amount of arguments. |
224
|
|
|
|
|
|
|
# |
225
|
|
|
|
|
|
|
|
226
|
19
|
|
|
|
|
14
|
my @specs; # User specification list |
227
|
19
|
|
|
|
|
16
|
my $all_optional = 0; # True if all arguments are optional |
228
|
|
|
|
|
|
|
|
229
|
19
|
100
|
66
|
|
|
49
|
if (@_ == 1 && ref $_[0]) { |
230
|
3
|
50
|
|
|
|
6
|
logcroak "must use an array reference for optional args" |
231
|
|
|
|
|
|
|
unless ref $_[0] eq 'ARRAY'; |
232
|
3
|
|
|
|
|
2
|
@specs = @{$_[0]}; |
|
3
|
|
|
|
|
5
|
|
233
|
3
|
|
|
|
|
3
|
$all_optional = 1; |
234
|
|
|
|
|
|
|
} else { |
235
|
16
|
|
|
|
|
33
|
@specs = @_; |
236
|
16
|
50
|
66
|
|
|
61
|
logcroak "must supply an even amount of arguments in extend mode" |
237
|
|
|
|
|
|
|
if $extended && (@specs % 2); |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# |
241
|
|
|
|
|
|
|
# Parse our argument list and compile it into @args |
242
|
|
|
|
|
|
|
# |
243
|
|
|
|
|
|
|
|
244
|
19
|
|
|
|
|
28
|
my %seen; |
245
|
|
|
|
|
|
|
my @args; # List of [name, type, is_optional, default] |
246
|
|
|
|
|
|
|
|
247
|
19
|
100
|
|
|
|
56
|
for (my $i = 0, my $step = $extended ? 2 : 1; $i < @specs; $i += $step) { |
248
|
80
|
|
|
|
|
77
|
my $arg = $specs[$i]; |
249
|
80
|
|
|
|
|
50
|
my ($name, $type, $optional, $dflt); |
250
|
80
|
100
|
|
|
|
74
|
if ($extended) { |
251
|
50
|
|
|
|
|
34
|
$name = $arg; |
252
|
50
|
|
|
|
|
39
|
my $spec = $specs[$i+1]; |
253
|
50
|
100
|
|
|
|
45
|
if (ref $spec) { |
254
|
|
|
|
|
|
|
# Given as an array ref -> optional, with possible default |
255
|
35
|
50
|
|
|
|
47
|
logcroak "specs for optional '$name' are $spec, expected ARRAY" |
256
|
|
|
|
|
|
|
unless ref $spec eq 'ARRAY'; |
257
|
35
|
|
|
|
|
48
|
($type, $dflt) = @$spec; |
258
|
35
|
|
|
|
|
42
|
$optional = 1; |
259
|
|
|
|
|
|
|
} else { |
260
|
|
|
|
|
|
|
# simple scalar is type, argument is mandatory |
261
|
15
|
|
|
|
|
12
|
$type = $spec; |
262
|
15
|
|
|
|
|
12
|
$optional = 0; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
} else { |
265
|
|
|
|
|
|
|
# Can be either "name" or "name=Type" |
266
|
30
|
|
|
|
|
52
|
($name, $type) = $arg =~ /^-?(\w+)=(\S+)/; |
267
|
30
|
100
|
|
|
|
36
|
$name = $arg unless defined $name; |
268
|
30
|
|
|
|
|
23
|
$optional = $all_optional; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
80
|
100
|
|
|
|
99
|
$name = lc($name) if $case_insensitive; |
272
|
80
|
|
|
|
|
64
|
$name =~ s/^-//; |
273
|
|
|
|
|
|
|
|
274
|
80
|
50
|
|
|
|
105
|
logcroak "argument name cannot be empty" if $name eq ''; |
275
|
80
|
50
|
|
|
|
87
|
logcroak "argument name must be scalar, not $name" if ref $name; |
276
|
80
|
50
|
|
|
|
127
|
logcroak "duplicate argument definition for '$name'" if $seen{$name}++; |
277
|
|
|
|
|
|
|
|
278
|
80
|
100
|
|
|
|
241
|
push(@args, [ |
|
|
100
|
|
|
|
|
|
279
|
|
|
|
|
|
|
$name, |
280
|
|
|
|
|
|
|
defined($type) ? $type : undef, |
281
|
|
|
|
|
|
|
$optional, |
282
|
|
|
|
|
|
|
defined($dflt) ? $dflt : undef |
283
|
|
|
|
|
|
|
]); |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# |
287
|
|
|
|
|
|
|
# If caching, generate the subroutine that will perform the checks. |
288
|
|
|
|
|
|
|
# |
289
|
|
|
|
|
|
|
# We use logxcroak to report errors to the caller of the caller |
290
|
|
|
|
|
|
|
# of *getargs, i.e. the caller of the routine for which we're checking |
291
|
|
|
|
|
|
|
# the arguments. |
292
|
|
|
|
|
|
|
# |
293
|
|
|
|
|
|
|
|
294
|
19
|
100
|
|
|
|
26
|
if ($subname ne '') { |
295
|
2
|
50
|
|
|
|
8
|
my $lc = $case_insensitive ? 'lc' : ''; |
296
|
2
|
|
|
|
|
4
|
my $sub = &q(<<'EOS'); |
297
|
|
|
|
|
|
|
:sub { |
298
|
|
|
|
|
|
|
: my $aref_orig = shift; |
299
|
|
|
|
|
|
|
: my @result; |
300
|
|
|
|
|
|
|
: my $cur; |
301
|
|
|
|
|
|
|
: my $isthere; |
302
|
|
|
|
|
|
|
: my $ctype; |
303
|
|
|
|
|
|
|
: local $Getargs::Long::dflt; |
304
|
|
|
|
|
|
|
: my $i = 0; |
305
|
|
|
|
|
|
|
EOS |
306
|
2
|
|
|
|
|
5
|
$sub .= &q(<
|
307
|
|
|
|
|
|
|
: logxcroak 3, "expected an even number of arguments" if \@\$aref_orig % 2; |
308
|
|
|
|
|
|
|
: |
309
|
|
|
|
|
|
|
: my \%args = map { |
310
|
|
|
|
|
|
|
: (\$i++ % 2) ? \$_ : $lc(/^-/ ? substr(\$_, 1) : \$_) } \@\$aref_orig; |
311
|
|
|
|
|
|
|
EOS |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
# Sanity check: no argument can be given twice |
314
|
2
|
|
|
|
|
4
|
$sub .= &q(<
|
315
|
|
|
|
|
|
|
: spot_dups(\$aref_orig, $case_insensitive, 3) |
316
|
|
|
|
|
|
|
: if 2 * scalar(keys \%args) != \@\$aref_orig; |
317
|
|
|
|
|
|
|
: |
318
|
|
|
|
|
|
|
EOS |
319
|
|
|
|
|
|
|
# Work on a copy if extra and no inplace |
320
|
2
|
50
|
33
|
|
|
5
|
if ($extra && !$inplace) { |
321
|
0
|
|
|
|
|
0
|
$sub .= &q(<<'EOS'); |
322
|
|
|
|
|
|
|
: my $aref = [@$aref_orig]; |
323
|
|
|
|
|
|
|
EOS |
324
|
|
|
|
|
|
|
} else { |
325
|
2
|
|
|
|
|
3
|
$sub .= &q(<<'EOS'); |
326
|
|
|
|
|
|
|
: my $aref = $aref_orig; |
327
|
|
|
|
|
|
|
EOS |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
# Index arguments if inplace editing or extra |
331
|
2
|
100
|
66
|
|
|
6
|
if ($inplace || $extra) { |
332
|
1
|
|
|
|
|
3
|
$sub .= &q(<<'EOS'); |
333
|
|
|
|
|
|
|
: |
334
|
|
|
|
|
|
|
: my $idx; |
335
|
|
|
|
|
|
|
: my %idx; |
336
|
|
|
|
|
|
|
: for (my $j = 0; $j < @$aref; $j += 2) { |
337
|
|
|
|
|
|
|
: my $key = $aref->[$j]; |
338
|
|
|
|
|
|
|
: $key =~ s/^-//; |
339
|
|
|
|
|
|
|
EOS |
340
|
1
|
50
|
|
|
|
3
|
$sub .= &q(<<'EOS') if $case_insensitive; |
341
|
|
|
|
|
|
|
: $key = lc($key); |
342
|
|
|
|
|
|
|
EOS |
343
|
1
|
|
|
|
|
2
|
$sub .= &q(<<'EOS'); |
344
|
|
|
|
|
|
|
: $idx{$key} = $j; |
345
|
|
|
|
|
|
|
: } |
346
|
|
|
|
|
|
|
: |
347
|
|
|
|
|
|
|
EOS |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
2
|
|
|
|
|
3
|
foreach my $arg (@args) { |
351
|
10
|
|
|
|
|
13
|
my ($name, $type, $optional, $dflt) = @$arg; |
352
|
10
|
|
|
|
|
10
|
my $has_default = defined $dflt; |
353
|
10
|
|
|
|
|
19
|
local $^W = 0; # Shut up Test::Harness |
354
|
10
|
|
|
|
|
27
|
$sub .= &q(<
|
355
|
|
|
|
|
|
|
: # Argument [name=$name, type=$type, optional=$optional, dflt=$dflt] |
356
|
|
|
|
|
|
|
: \$cur = undef; |
357
|
|
|
|
|
|
|
: \$isthere = 0; |
358
|
|
|
|
|
|
|
: if (exists \$args{$name}) { |
359
|
|
|
|
|
|
|
: \$isthere = 1; |
360
|
|
|
|
|
|
|
: my \$val = delete \$args{$name}; |
361
|
|
|
|
|
|
|
: \$cur = \\\$val; |
362
|
|
|
|
|
|
|
EOS |
363
|
10
|
100
|
66
|
|
|
31
|
$sub .= &q(<
|
364
|
|
|
|
|
|
|
: # Splice argument out |
365
|
|
|
|
|
|
|
: \$idx = \$idx{$name}; |
366
|
|
|
|
|
|
|
EOS |
367
|
10
|
100
|
66
|
|
|
25
|
$sub .= &q(<<'EOS') if $inplace || $extra; |
368
|
|
|
|
|
|
|
: splice(@$aref, $idx, 2); |
369
|
|
|
|
|
|
|
: while (my ($k, $v) = each %idx) { |
370
|
|
|
|
|
|
|
: $idx{$k} -= 2 if $v > $idx; |
371
|
|
|
|
|
|
|
: } |
372
|
|
|
|
|
|
|
EOS |
373
|
10
|
|
|
|
|
13
|
$sub .= &q(<<'EOS'); |
374
|
|
|
|
|
|
|
: } |
375
|
|
|
|
|
|
|
EOS |
376
|
10
|
100
|
|
|
|
11
|
if ($optional) { |
377
|
7
|
100
|
|
|
|
11
|
if ($has_default) { |
378
|
4
|
|
|
|
|
4
|
$sub .= &q(<
|
379
|
|
|
|
|
|
|
: else { |
380
|
|
|
|
|
|
|
: eval { |
381
|
|
|
|
|
|
|
: package Getargs::Long::_; |
382
|
|
|
|
|
|
|
: no strict; |
383
|
|
|
|
|
|
|
: \$Getargs::Long::dflt = |
384
|
|
|
|
|
|
|
EOS |
385
|
4
|
|
|
|
|
21
|
my $obj = Data::Dumper->new([$dflt], []); |
386
|
4
|
|
|
|
|
90
|
$obj->Purity(1); |
387
|
4
|
|
|
|
|
79
|
$sub .= $obj->Dumpxs; |
388
|
4
|
|
|
|
|
7
|
$sub .= &q(<<'EOS'); |
389
|
|
|
|
|
|
|
: }; |
390
|
|
|
|
|
|
|
: $cur = \$Getargs::Long::dflt; |
391
|
|
|
|
|
|
|
: } |
392
|
|
|
|
|
|
|
EOS |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
} else { |
395
|
3
|
|
|
|
|
7
|
$sub .= &q(<
|
396
|
|
|
|
|
|
|
: logxcroak 3, "mandatory argument '$name' missing" unless \$isthere; |
397
|
|
|
|
|
|
|
EOS |
398
|
|
|
|
|
|
|
} |
399
|
10
|
100
|
|
|
|
17
|
if ($type ne '') { |
400
|
7
|
100
|
|
|
|
8
|
if ($optional) { |
401
|
5
|
|
|
|
|
11
|
$sub .= &q(<
|
402
|
|
|
|
|
|
|
: logxcroak 3, "argument '$name' cannot be undef" |
403
|
|
|
|
|
|
|
: if \$isthere && !defined \$\$cur; |
404
|
|
|
|
|
|
|
EOS |
405
|
|
|
|
|
|
|
} else { |
406
|
2
|
|
|
|
|
6
|
$sub .= &q(<
|
407
|
|
|
|
|
|
|
: logxcroak 3, "argument '$name' cannot be undef" unless defined \$\$cur; |
408
|
|
|
|
|
|
|
EOS |
409
|
|
|
|
|
|
|
} |
410
|
7
|
100
|
|
|
|
10
|
my $opt_is_there = $optional ? "\$isthere &&" : ""; |
411
|
7
|
100
|
|
|
|
14
|
if ($type =~ /^[isn]$/) { # Make sure it's a scalar |
412
|
|
|
|
|
|
|
# XXX Check that i is integer, s string and n natural |
413
|
3
|
|
|
|
|
8
|
$sub .= &q(<
|
414
|
|
|
|
|
|
|
: logxcroak 3, |
415
|
|
|
|
|
|
|
: "argument '$name' must be scalar (type '$type') but is \$\$cur" |
416
|
|
|
|
|
|
|
: if $opt_is_there ref \$\$cur; |
417
|
|
|
|
|
|
|
EOS |
418
|
|
|
|
|
|
|
} else { |
419
|
4
|
|
|
|
|
11
|
$sub .= &q(<
|
420
|
|
|
|
|
|
|
: \$ctype = \$isthere ? ref \$\$cur : undef; |
421
|
|
|
|
|
|
|
: logxcroak 3, "argument '$name' must be of type $type but is \$ctype" |
422
|
|
|
|
|
|
|
: if $opt_is_there (UNIVERSAL::isa(\$\$cur, 'UNIVERSAL') ? |
423
|
|
|
|
|
|
|
: !\$\$cur->isa('$type') : |
424
|
|
|
|
|
|
|
: \$ctype ne '$type'); |
425
|
|
|
|
|
|
|
EOS |
426
|
|
|
|
|
|
|
} |
427
|
|
|
|
|
|
|
} |
428
|
10
|
|
|
|
|
12
|
$sub .= &q(<<'EOS'); |
429
|
|
|
|
|
|
|
: push(@result, defined($cur) ? $$cur : undef); |
430
|
|
|
|
|
|
|
: |
431
|
|
|
|
|
|
|
EOS |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
# If we're strict, we must report unprocessed switches |
435
|
2
|
100
|
|
|
|
7
|
$sub .= &q(<<'EOS') if $strict; |
436
|
|
|
|
|
|
|
: |
437
|
|
|
|
|
|
|
: spot_unknown(\%args, 3) if scalar keys %args; |
438
|
|
|
|
|
|
|
: |
439
|
|
|
|
|
|
|
EOS |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
# Add extra unprocessed switches to the result list |
442
|
2
|
50
|
|
|
|
4
|
$sub .= &q(<<'EOS') if $extra; |
443
|
|
|
|
|
|
|
: push(@result, @$aref); |
444
|
|
|
|
|
|
|
EOS |
445
|
2
|
|
|
|
|
5
|
$sub .= &q(<<'EOS'); |
446
|
|
|
|
|
|
|
: return @result; |
447
|
|
|
|
|
|
|
:} |
448
|
|
|
|
|
|
|
EOS |
449
|
2
|
|
|
|
|
63
|
logdbg 'debug', "anonymous subroutine: $sub"; |
450
|
1
|
|
|
1
|
|
6
|
my $code = eval $sub; |
|
1
|
|
|
1
|
|
0
|
|
|
1
|
|
|
1
|
|
155
|
|
|
1
|
|
|
1
|
|
4
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
196
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
233
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
270
|
|
|
2
|
|
|
|
|
596
|
|
451
|
2
|
50
|
|
|
|
8
|
if (chop($@)) { |
452
|
0
|
|
|
|
|
0
|
logerr "can't create subroutine for checking args of $subname: $@"; |
453
|
0
|
|
|
|
|
0
|
logwarn "ignoring caching directive for $subname"; |
454
|
|
|
|
|
|
|
} else { |
455
|
2
|
|
|
|
|
5
|
$subcache{$subname} = $code; |
456
|
2
|
|
|
|
|
56
|
logdbg 'info', "calling newly built subroutine $code"; |
457
|
2
|
|
|
|
|
153
|
return &$code($args); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
# |
462
|
|
|
|
|
|
|
# No caching made, perform validation by interpreting the structure |
463
|
|
|
|
|
|
|
# |
464
|
|
|
|
|
|
|
# There is some unfortunate code duplication between the following checks |
465
|
|
|
|
|
|
|
# and the above routine-construction logic. Some place are identical, |
466
|
|
|
|
|
|
|
# but the main argument processing loop is noticeably different, even |
467
|
|
|
|
|
|
|
# though the same logic is used. |
468
|
|
|
|
|
|
|
# |
469
|
|
|
|
|
|
|
|
470
|
17
|
|
|
|
|
322
|
logdbg 'info', "interpreting structure to validate arguments"; |
471
|
|
|
|
|
|
|
|
472
|
17
|
|
|
|
|
904
|
my @result; |
473
|
|
|
|
|
|
|
my $cur; |
474
|
0
|
|
|
|
|
0
|
my $ctype; |
475
|
|
|
|
|
|
|
|
476
|
17
|
|
|
|
|
15
|
my $i = 0; |
477
|
17
|
|
|
|
|
7
|
my %args; |
478
|
|
|
|
|
|
|
|
479
|
17
|
100
|
66
|
|
|
45
|
$args = [@$args] if $extra && !$inplace; # Work on a copy |
480
|
|
|
|
|
|
|
|
481
|
17
|
50
|
|
|
|
32
|
logxcroak 2, "expected an even number of arguments" if @$args % 2; |
482
|
|
|
|
|
|
|
|
483
|
17
|
100
|
|
|
|
20
|
if ($case_insensitive) { |
484
|
10
|
100
|
|
|
|
14
|
%args = map { ($i++ % 2) ? $_ : lc(/^-/ ? substr($_, 1) : $_) } @$args; |
|
66
|
100
|
|
|
|
146
|
|
485
|
|
|
|
|
|
|
} else { |
486
|
7
|
50
|
|
|
|
13
|
%args = map { ($i++ % 2) ? $_ : (/^-/ ? substr($_, 1) : $_) } @$args; |
|
52
|
100
|
|
|
|
100
|
|
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
# Sanity check: no argument can be given twice |
490
|
17
|
50
|
|
|
|
47
|
spot_dups($args, $case_insensitive, 2) |
491
|
|
|
|
|
|
|
if 2 * scalar(keys %args) != @$args; |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
# Index arguments if inplace editing or extra |
494
|
17
|
|
|
|
|
14
|
my %idx; |
495
|
17
|
100
|
100
|
|
|
46
|
if ($inplace || $extra) { |
496
|
7
|
|
|
|
|
14
|
for (my $j = 0; $j < @$args; $j += 2) { |
497
|
19
|
|
|
|
|
17
|
my $key = $args->[$j]; |
498
|
19
|
|
|
|
|
23
|
$key =~ s/^-//; |
499
|
19
|
100
|
|
|
|
26
|
$key = lc($key) if $case_insensitive; |
500
|
19
|
|
|
|
|
31
|
$idx{$key} = $j; |
501
|
|
|
|
|
|
|
} |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
# Process each argument |
505
|
17
|
|
|
|
|
19
|
foreach my $arg (@args) { |
506
|
68
|
|
|
|
|
70
|
my ($name, $type, $optional, $dflt) = @$arg; |
507
|
68
|
|
|
|
|
43
|
my $cur; |
508
|
68
|
|
|
|
|
41
|
my $isthere = 0; |
509
|
68
|
100
|
|
|
|
86
|
if (exists $args{$name}) { |
|
|
100
|
|
|
|
|
|
510
|
50
|
|
|
|
|
35
|
$isthere = 1; |
511
|
50
|
|
|
|
|
48
|
my $val = delete $args{$name}; |
512
|
50
|
|
|
|
|
40
|
$cur = \$val; |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
# Splice argument out if requested |
515
|
50
|
100
|
100
|
|
|
120
|
if ($inplace || $extra) { |
516
|
17
|
|
|
|
|
15
|
my $idx = $idx{$name}; |
517
|
17
|
|
|
|
|
15
|
splice(@$args, $idx, 2); |
518
|
17
|
|
|
|
|
29
|
while (my ($k, $v) = each %idx) { |
519
|
53
|
100
|
|
|
|
154
|
$idx{$k} -= 2 if $v > $idx; |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
} elsif ($optional) { |
523
|
15
|
100
|
|
|
|
23
|
$cur = \$dflt if defined $dflt; |
524
|
|
|
|
|
|
|
} else { |
525
|
3
|
|
|
|
|
43
|
logxcroak 2, "mandatory argument '$name' missing"; |
526
|
|
|
|
|
|
|
} |
527
|
|
|
|
|
|
|
|
528
|
65
|
100
|
|
|
|
73
|
push(@result, defined($cur) ? $$cur : undef); |
529
|
65
|
100
|
66
|
|
|
142
|
next if !defined $type || $type eq ''; |
530
|
|
|
|
|
|
|
|
531
|
42
|
100
|
|
|
|
37
|
if ($optional) { |
532
|
29
|
50
|
66
|
|
|
69
|
logxcroak 2, "argument '$name' cannot be undef" |
533
|
|
|
|
|
|
|
if $isthere && !defined $$cur; |
534
|
|
|
|
|
|
|
} else { |
535
|
13
|
100
|
|
|
|
37
|
logxcroak 2, |
536
|
|
|
|
|
|
|
"argument '$name' cannot be undef" unless defined $$cur; |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
# XXX Check that i is integer, s string and n natural |
540
|
41
|
100
|
|
|
|
65
|
if ($type =~ /^[isn]$/) { # Make sure it's a scalar |
541
|
16
|
50
|
100
|
|
|
75
|
logxcroak 2, |
|
|
|
66
|
|
|
|
|
542
|
|
|
|
|
|
|
"argument '$name' must be scalar (type '$type') but is $$cur" |
543
|
|
|
|
|
|
|
if (!$optional || $isthere) && ref $$cur; |
544
|
|
|
|
|
|
|
} else { |
545
|
25
|
100
|
|
|
|
36
|
my $ctype = $isthere ? ref $$cur : undef; |
546
|
25
|
100
|
100
|
|
|
160
|
logxcroak 2, "argument '$name' must be of type $type but is $ctype" |
|
|
100
|
66
|
|
|
|
|
547
|
|
|
|
|
|
|
if (!$optional || $isthere) && |
548
|
|
|
|
|
|
|
(UNIVERSAL::isa($$cur, 'UNIVERSAL') ? |
549
|
|
|
|
|
|
|
!$$cur->isa($type) : |
550
|
|
|
|
|
|
|
$ctype ne $type); |
551
|
|
|
|
|
|
|
} |
552
|
|
|
|
|
|
|
} |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
# If we're strict, we must report unprocessed switches |
555
|
12
|
100
|
100
|
|
|
34
|
spot_unknown(\%args, 2) if $strict && scalar keys %args; |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# Add extra unprocessed switches to the result list |
558
|
8
|
100
|
|
|
|
15
|
push(@result, @$args) if $extra; |
559
|
|
|
|
|
|
|
|
560
|
8
|
|
|
|
|
39
|
return @result; |
561
|
|
|
|
|
|
|
} |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
# |
564
|
|
|
|
|
|
|
# spot_dups |
565
|
|
|
|
|
|
|
# |
566
|
|
|
|
|
|
|
# Given a list of arguments in $aref, where we know there are duplicate "keys", |
567
|
|
|
|
|
|
|
# identify them and croak by listing the culprits. |
568
|
|
|
|
|
|
|
# |
569
|
|
|
|
|
|
|
sub spot_dups { |
570
|
0
|
|
|
0
|
0
|
0
|
my ($aref, $ignorecase, $level) = @_; |
571
|
0
|
|
|
|
|
0
|
my %seen; |
572
|
|
|
|
|
|
|
my @duplicates; |
573
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @$aref; $i += 2) { |
574
|
0
|
0
|
|
|
|
0
|
my $key = $ignorecase ? lc($aref->[$i]) : $aref->[$i]; |
575
|
0
|
|
|
|
|
0
|
$key =~ s/^-//; |
576
|
0
|
0
|
|
|
|
0
|
push(@duplicates, "-$key") if $seen{$key}++; |
577
|
|
|
|
|
|
|
} |
578
|
0
|
0
|
|
|
|
0
|
logconfess "bug in Getargs::Long -- should have found duplicates" |
579
|
|
|
|
|
|
|
unless @duplicates; |
580
|
0
|
|
|
|
|
0
|
logxcroak ++$level, |
581
|
|
|
|
|
|
|
"multiple switches given for: " . join(", ", @duplicates); |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
# |
585
|
|
|
|
|
|
|
# spot_unknown |
586
|
|
|
|
|
|
|
# |
587
|
|
|
|
|
|
|
# Report keys held in supplied hashref as unknown switches. |
588
|
|
|
|
|
|
|
# |
589
|
|
|
|
|
|
|
sub spot_unknown { |
590
|
5
|
|
|
5
|
0
|
5
|
my ($href, $level) = @_; |
591
|
5
|
|
|
|
|
12
|
my @unprocessed = map { "-$_" } keys %$href; |
|
5
|
|
|
|
|
14
|
|
592
|
5
|
50
|
|
|
|
13
|
my $es = @unprocessed == 1 ? '' : 'es'; |
593
|
5
|
|
|
|
|
93
|
logxcroak ++$level, "unknown switch$es: " . join(", ", @unprocessed); |
594
|
|
|
|
|
|
|
} |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
sub q { |
597
|
79
|
|
|
79
|
0
|
51
|
local $_ = shift; |
598
|
79
|
|
|
|
|
223
|
s/^://gm; |
599
|
79
|
|
|
|
|
124
|
return $_; |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
1; |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
__END__ |