line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Long::Util; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
70742
|
use 5.010001; |
|
1
|
|
|
|
|
15
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
420
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
10
|
|
|
|
|
|
|
parse_getopt_long_opt_spec |
11
|
|
|
|
|
|
|
humanize_getopt_long_opt_spec |
12
|
|
|
|
|
|
|
detect_getopt_long_script |
13
|
|
|
|
|
|
|
gen_getopt_long_spec_from_getopt_std_spec |
14
|
|
|
|
|
|
|
array_getopt_long_spec_to_hash |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
18
|
|
|
|
|
|
|
our $DATE = '2022-11-14'; # DATE |
19
|
|
|
|
|
|
|
our $DIST = 'Getopt-Long-Util'; # DIST |
20
|
|
|
|
|
|
|
our $VERSION = '0.899'; # VERSION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our %SPEC; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$SPEC{parse_getopt_long_opt_spec} = { |
25
|
|
|
|
|
|
|
v => 1.1, |
26
|
|
|
|
|
|
|
summary => 'Parse a single Getopt::Long option specification', |
27
|
|
|
|
|
|
|
description => <<'_', |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Will produce a hash with some keys: |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
* `is_arg` (if true, then option specification is the special `<>` for argument |
32
|
|
|
|
|
|
|
callback) |
33
|
|
|
|
|
|
|
* `opts` (array of option names, in the order specified in the opt spec) |
34
|
|
|
|
|
|
|
* `type` (string, type name) |
35
|
|
|
|
|
|
|
* `desttype` (either '', or '@' or '%'), |
36
|
|
|
|
|
|
|
* `is_neg` (true for `--opt!`) |
37
|
|
|
|
|
|
|
* `is_inc` (true for `--opt+`) |
38
|
|
|
|
|
|
|
* `min_vals` (int, usually 0 or 1) |
39
|
|
|
|
|
|
|
* `max_vals` (int, usually 0 or 1 except for option that requires multiple |
40
|
|
|
|
|
|
|
values) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Will return undef if it can't parse the string. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
_ |
45
|
|
|
|
|
|
|
args => { |
46
|
|
|
|
|
|
|
optspec => { |
47
|
|
|
|
|
|
|
schema => 'str*', |
48
|
|
|
|
|
|
|
req => 1, |
49
|
|
|
|
|
|
|
pos => 0, |
50
|
|
|
|
|
|
|
}, |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
args_as => 'array', |
53
|
|
|
|
|
|
|
result_naked => 1, |
54
|
|
|
|
|
|
|
result => { |
55
|
|
|
|
|
|
|
schema => 'hash*', |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
examples => [ |
58
|
|
|
|
|
|
|
{ |
59
|
|
|
|
|
|
|
args => {optspec => 'help|h|?'}, |
60
|
|
|
|
|
|
|
result => {dash_prefix=>'', opts=>['help', 'h', '?']}, |
61
|
|
|
|
|
|
|
}, |
62
|
|
|
|
|
|
|
{ |
63
|
|
|
|
|
|
|
args => {optspec=>'--foo=s'}, |
64
|
|
|
|
|
|
|
result => {dash_prefix=>'--', opts=>['foo'], type=>'s', desttype=>''}, |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
], |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
# BEGIN_BLOCK: parse_getopt_long_opt_spec |
69
|
|
|
|
|
|
|
sub parse_getopt_long_opt_spec { |
70
|
25
|
|
|
25
|
1
|
1219
|
my $optspec = shift; |
71
|
25
|
100
|
|
|
|
67
|
return {is_arg=>1, dash_prefix=>'', opts=>[]} |
72
|
|
|
|
|
|
|
if $optspec eq '<>'; |
73
|
23
|
100
|
|
|
|
326
|
$optspec =~ qr/\A |
74
|
|
|
|
|
|
|
(?P-{0,2}) |
75
|
|
|
|
|
|
|
(?P[A-Za-z0-9_][A-Za-z0-9_-]*) |
76
|
|
|
|
|
|
|
(?P (?: \| (?:[^:|!+=:-][^:|!+=:]*) )*)? |
77
|
|
|
|
|
|
|
(?: |
78
|
|
|
|
|
|
|
(?P!) | |
79
|
|
|
|
|
|
|
(?P\+) | |
80
|
|
|
|
|
|
|
(?: |
81
|
|
|
|
|
|
|
= |
82
|
|
|
|
|
|
|
(?P[siof]) |
83
|
|
|
|
|
|
|
(?P|[%@])? |
84
|
|
|
|
|
|
|
(?: |
85
|
|
|
|
|
|
|
\{ |
86
|
|
|
|
|
|
|
(?: (?P\d+), )? |
87
|
|
|
|
|
|
|
(?P\d+) |
88
|
|
|
|
|
|
|
\} |
89
|
|
|
|
|
|
|
)? |
90
|
|
|
|
|
|
|
) | |
91
|
|
|
|
|
|
|
(?: |
92
|
|
|
|
|
|
|
: |
93
|
|
|
|
|
|
|
(?P[siof]) |
94
|
|
|
|
|
|
|
(?P|[%@])? |
95
|
|
|
|
|
|
|
) | |
96
|
|
|
|
|
|
|
(?: |
97
|
|
|
|
|
|
|
: |
98
|
|
|
|
|
|
|
(?P-?\d+) |
99
|
|
|
|
|
|
|
(?P|[%@])? |
100
|
|
|
|
|
|
|
) | |
101
|
|
|
|
|
|
|
(?: |
102
|
|
|
|
|
|
|
: |
103
|
|
|
|
|
|
|
(?P\+) |
104
|
|
|
|
|
|
|
(?P|[%@])? |
105
|
|
|
|
|
|
|
) |
106
|
|
|
|
|
|
|
)? |
107
|
|
|
|
|
|
|
\z/x |
108
|
|
|
|
|
|
|
or return; |
109
|
1
|
|
|
1
|
|
435
|
my %res = %+; |
|
1
|
|
|
|
|
434
|
|
|
1
|
|
|
|
|
1840
|
|
|
21
|
|
|
|
|
367
|
|
110
|
|
|
|
|
|
|
|
111
|
21
|
100
|
|
|
|
91
|
if (defined $res{optnum}) { |
112
|
1
|
|
|
|
|
3
|
$res{type} = 'i'; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
21
|
100
|
|
|
|
48
|
if ($res{aliases}) { |
116
|
7
|
|
|
|
|
13
|
my @als; |
117
|
7
|
|
|
|
|
23
|
for my $al (split /\|/, $res{aliases}) { |
118
|
18
|
100
|
|
|
|
40
|
next unless length $al; |
119
|
11
|
50
|
|
|
|
24
|
next if $al eq $res{name}; |
120
|
11
|
50
|
|
|
|
22
|
next if grep {$_ eq $al} @als; |
|
4
|
|
|
|
|
13
|
|
121
|
11
|
|
|
|
|
27
|
push @als, $al; |
122
|
|
|
|
|
|
|
} |
123
|
7
|
|
|
|
|
25
|
$res{opts} = [$res{name}, @als]; |
124
|
|
|
|
|
|
|
} else { |
125
|
14
|
|
|
|
|
38
|
$res{opts} = [$res{name}]; |
126
|
|
|
|
|
|
|
} |
127
|
21
|
|
|
|
|
38
|
delete $res{name}; |
128
|
21
|
|
|
|
|
29
|
delete $res{aliases}; |
129
|
|
|
|
|
|
|
|
130
|
21
|
100
|
|
|
|
58
|
$res{is_neg} = 1 if $res{is_neg}; |
131
|
21
|
100
|
|
|
|
38
|
$res{is_inc} = 1 if $res{is_inc}; |
132
|
|
|
|
|
|
|
|
133
|
21
|
|
|
|
|
102
|
\%res; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
# END_BLOCK: parse_getopt_long_opt_spec |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$SPEC{humanize_getopt_long_opt_spec} = { |
138
|
|
|
|
|
|
|
v => 1.1, |
139
|
|
|
|
|
|
|
description => <<'_', |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Convert option specification into a more human-friendly |
142
|
|
|
|
|
|
|
notation that is suitable for including in help/usage text, for example: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
help|h|? -> "--help, -h, -?" |
145
|
|
|
|
|
|
|
help|h|? -> "--help | -h | -?" # if you provide 'separator' |
146
|
|
|
|
|
|
|
--foo=s -> "--foo=s" |
147
|
|
|
|
|
|
|
--foo=s -> "--foo=somelabel" # if you provide 'value_label' |
148
|
|
|
|
|
|
|
--foo:s -> "--foo[=s]" |
149
|
|
|
|
|
|
|
--foo=s@ -> "(--foo=s)+" |
150
|
|
|
|
|
|
|
--foo=s% -> "(--foo key=value)+" |
151
|
|
|
|
|
|
|
--foo=s% -> "(--foo somelabel1=somelabel2)+" # if you provide 'key_label' and 'value_label' |
152
|
|
|
|
|
|
|
--debug! -> "--(no)debug" |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
It also produces POD-formatted string for use in POD documentation: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
--foo=s -> {plaintext=>"--foo=s", pod=>"B<--foo>=I"} |
157
|
|
|
|
|
|
|
# if you set 'extended' to true |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Will die if can't parse the optspec string. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
_ |
162
|
|
|
|
|
|
|
args => { |
163
|
|
|
|
|
|
|
optspec => { |
164
|
|
|
|
|
|
|
schema => 'str*', |
165
|
|
|
|
|
|
|
req => 1, |
166
|
|
|
|
|
|
|
pos => 0, |
167
|
|
|
|
|
|
|
}, |
168
|
|
|
|
|
|
|
separator => { |
169
|
|
|
|
|
|
|
schema => 'str*', |
170
|
|
|
|
|
|
|
default => ', ', |
171
|
|
|
|
|
|
|
}, |
172
|
|
|
|
|
|
|
key_label => { |
173
|
|
|
|
|
|
|
schema => 'str*', |
174
|
|
|
|
|
|
|
default => 'key', |
175
|
|
|
|
|
|
|
}, |
176
|
|
|
|
|
|
|
opt_link => { |
177
|
|
|
|
|
|
|
schema => 'str*', # XXX url? podlink? |
178
|
|
|
|
|
|
|
}, |
179
|
|
|
|
|
|
|
value_label => { |
180
|
|
|
|
|
|
|
schema => 'str*', |
181
|
|
|
|
|
|
|
}, |
182
|
|
|
|
|
|
|
value_label_link => { |
183
|
|
|
|
|
|
|
schema => 'str*', # XXX url? podlink? |
184
|
|
|
|
|
|
|
}, |
185
|
|
|
|
|
|
|
extended => { |
186
|
|
|
|
|
|
|
summary => 'If set to true, will return a hash of multiple formats instead of a single plaintext format', |
187
|
|
|
|
|
|
|
schema => 'bool*', |
188
|
|
|
|
|
|
|
}, |
189
|
|
|
|
|
|
|
}, |
190
|
|
|
|
|
|
|
args_as => 'array', |
191
|
|
|
|
|
|
|
result_naked => 1, |
192
|
|
|
|
|
|
|
result => { |
193
|
|
|
|
|
|
|
schema => ['any*', {of=>[['str*'], ['hash*', {of=>'str*'}]]}], |
194
|
|
|
|
|
|
|
}, |
195
|
|
|
|
|
|
|
}; |
196
|
|
|
|
|
|
|
sub humanize_getopt_long_opt_spec { |
197
|
13
|
100
|
|
13
|
1
|
2858
|
my $opts = {}; $opts = shift if ref $_[0] eq 'HASH'; |
|
13
|
|
|
|
|
38
|
|
198
|
13
|
|
|
|
|
23
|
my $optspec = shift; |
199
|
|
|
|
|
|
|
|
200
|
13
|
50
|
|
|
|
25
|
my $parse = parse_getopt_long_opt_spec($optspec) |
201
|
|
|
|
|
|
|
or die "Can't parse opt spec $optspec"; |
202
|
|
|
|
|
|
|
|
203
|
13
|
100
|
|
|
|
35
|
return "argument" if $parse->{is_arg}; |
204
|
|
|
|
|
|
|
|
205
|
12
|
|
|
|
|
17
|
my $plain_res = ''; |
206
|
12
|
|
|
|
|
18
|
my $pod_res = ''; |
207
|
12
|
|
|
|
|
15
|
my $i = 0; |
208
|
12
|
|
|
|
|
19
|
for (@{ $parse->{opts} }) { |
|
12
|
|
|
|
|
27
|
|
209
|
18
|
|
|
|
|
24
|
$i++; |
210
|
18
|
|
|
|
|
25
|
my $opt_plain_res = ''; |
211
|
18
|
|
|
|
|
24
|
my $opt_pod_res = ''; |
212
|
18
|
100
|
100
|
|
|
46
|
if ($parse->{is_neg} && length($_) > 1) { |
213
|
2
|
|
|
|
|
6
|
$opt_plain_res .= "--(no)$_"; |
214
|
2
|
50
|
|
|
|
7
|
$opt_pod_res .= defined($opts->{opt_link}) ? "B{opt_link}>>" : "B<--(no)$_>"; |
215
|
|
|
|
|
|
|
} else { |
216
|
16
|
100
|
|
|
|
33
|
if (length($_) > 1) { |
217
|
11
|
|
|
|
|
21
|
$opt_plain_res .= "--$_"; |
218
|
11
|
50
|
|
|
|
28
|
$opt_pod_res .= defined($opts->{opt_link}) ? "B{opt_link}>>" : "B<--$_>"; |
219
|
|
|
|
|
|
|
} else { |
220
|
5
|
|
|
|
|
8
|
$opt_plain_res .= "-$_"; |
221
|
5
|
50
|
|
|
|
13
|
$opt_pod_res .= defined($opts->{opt_link}) ? "B{opt_link}>>" : "B<-$_>"; |
222
|
|
|
|
|
|
|
} |
223
|
16
|
100
|
66
|
|
|
54
|
if ($i==1 && ($parse->{type} || $parse->{opttype})) { |
|
|
|
66
|
|
|
|
|
224
|
|
|
|
|
|
|
# show value label |
225
|
8
|
|
100
|
|
|
30
|
my $key_label = $opts->{key_label} // 'key'; |
226
|
|
|
|
|
|
|
my $value_label = $opts->{value_label} // |
227
|
8
|
|
66
|
|
|
26
|
$parse->{type} // $parse->{opttype}; |
|
|
|
33
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
8
|
50
|
|
|
|
18
|
$opt_plain_res .= "[" if $parse->{opttype}; |
230
|
8
|
100
|
66
|
|
|
29
|
$opt_plain_res .= ($parse->{type} && $parse->{desttype} eq '%' ? " " : "="); |
231
|
8
|
100
|
|
|
|
20
|
$opt_plain_res .= "$key_label=" if $parse->{desttype} eq '%'; |
232
|
8
|
|
|
|
|
13
|
$opt_plain_res .= $value_label; |
233
|
8
|
50
|
|
|
|
15
|
$opt_plain_res .= "]" if $parse->{opttype}; |
234
|
|
|
|
|
|
|
|
235
|
8
|
50
|
|
|
|
16
|
$opt_pod_res .= "[" if $parse->{opttype}; |
236
|
8
|
100
|
66
|
|
|
28
|
$opt_pod_res .= ($parse->{type} && $parse->{desttype} eq '%' ? " " : "="); |
237
|
8
|
100
|
|
|
|
34
|
$opt_pod_res .= "I<$key_label>=" if $parse->{desttype} eq '%'; |
238
|
8
|
50
|
|
|
|
20
|
$opt_pod_res .= defined $opts->{value_label_link} ? "I{value_label_link}>>" : "I<$value_label>"; |
239
|
8
|
50
|
|
|
|
16
|
$opt_pod_res .= "]" if $parse->{opttype}; |
240
|
|
|
|
|
|
|
} |
241
|
16
|
100
|
100
|
|
|
64
|
$opt_plain_res = "($opt_plain_res)+" if ($parse->{desttype} // '') =~ /@|%/; |
242
|
16
|
100
|
100
|
|
|
57
|
$opt_pod_res = "($opt_pod_res)+" if ($parse->{desttype} // '') =~ /@|%/; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
18
|
100
|
50
|
|
|
73
|
$plain_res .= ($opts->{separator} // ", ") if length($plain_res); |
246
|
18
|
100
|
50
|
|
|
45
|
$pod_res .= ($opts->{separator} // ", ") if length($pod_res); |
247
|
|
|
|
|
|
|
|
248
|
18
|
|
|
|
|
29
|
$plain_res .= $opt_plain_res; |
249
|
18
|
|
|
|
|
34
|
$pod_res .= $opt_pod_res; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
12
|
100
|
|
|
|
24
|
if ($opts->{extended}) { |
253
|
|
|
|
|
|
|
return { |
254
|
1
|
|
|
|
|
10
|
plaintext => $plain_res, |
255
|
|
|
|
|
|
|
pod => $pod_res, |
256
|
|
|
|
|
|
|
}; |
257
|
|
|
|
|
|
|
} else { |
258
|
11
|
|
|
|
|
66
|
$plain_res; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
$SPEC{detect_getopt_long_script} = { |
263
|
|
|
|
|
|
|
v => 1.1, |
264
|
|
|
|
|
|
|
summary => 'Detect whether a file is a Getopt::Long-based CLI script', |
265
|
|
|
|
|
|
|
description => <<'_', |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
The criteria are: |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
* the file must exist and readable; |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
* (optional, if `include_noexec` is false) file must have its executable mode |
272
|
|
|
|
|
|
|
bit set; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
* content must start with a shebang C<#!>; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
* either: must be perl script (shebang line contains 'perl') and must contain |
277
|
|
|
|
|
|
|
something like `use Getopt::Long`; |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
_ |
280
|
|
|
|
|
|
|
args => { |
281
|
|
|
|
|
|
|
filename => { |
282
|
|
|
|
|
|
|
summary => 'Path to file to be checked', |
283
|
|
|
|
|
|
|
schema => 'str*', |
284
|
|
|
|
|
|
|
pos => 0, |
285
|
|
|
|
|
|
|
cmdline_aliases => {f=>{}}, |
286
|
|
|
|
|
|
|
}, |
287
|
|
|
|
|
|
|
string => { |
288
|
|
|
|
|
|
|
summary => 'String to be checked', |
289
|
|
|
|
|
|
|
schema => 'buf*', |
290
|
|
|
|
|
|
|
}, |
291
|
|
|
|
|
|
|
include_noexec => { |
292
|
|
|
|
|
|
|
summary => 'Include scripts that do not have +x mode bit set', |
293
|
|
|
|
|
|
|
schema => 'bool*', |
294
|
|
|
|
|
|
|
default => 1, |
295
|
|
|
|
|
|
|
}, |
296
|
|
|
|
|
|
|
}, |
297
|
|
|
|
|
|
|
args_rels => { |
298
|
|
|
|
|
|
|
'req_one' => ['filename', 'string'], |
299
|
|
|
|
|
|
|
}, |
300
|
|
|
|
|
|
|
}; |
301
|
|
|
|
|
|
|
sub detect_getopt_long_script { |
302
|
0
|
|
|
0
|
1
|
0
|
my %args = @_; |
303
|
|
|
|
|
|
|
|
304
|
0
|
0
|
0
|
|
|
0
|
(defined($args{filename}) xor defined($args{string})) |
305
|
|
|
|
|
|
|
or return [400, "Please specify either filename or string"]; |
306
|
0
|
|
0
|
|
|
0
|
my $include_noexec = $args{include_noexec} // 1; |
307
|
|
|
|
|
|
|
|
308
|
0
|
|
|
|
|
0
|
my $yesno = 0; |
309
|
0
|
|
|
|
|
0
|
my $reason = ""; |
310
|
0
|
|
|
|
|
0
|
my %extrameta; |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
|
|
0
|
my $str = $args{string}; |
313
|
|
|
|
|
|
|
DETECT: |
314
|
|
|
|
|
|
|
{ |
315
|
0
|
0
|
|
|
|
0
|
if (defined $args{filename}) { |
|
0
|
|
|
|
|
0
|
|
316
|
0
|
|
|
|
|
0
|
my $fn = $args{filename}; |
317
|
0
|
0
|
|
|
|
0
|
unless (-f $fn) { |
318
|
0
|
|
|
|
|
0
|
$reason = "'$fn' is not a file"; |
319
|
0
|
|
|
|
|
0
|
last; |
320
|
|
|
|
|
|
|
}; |
321
|
0
|
0
|
0
|
|
|
0
|
if (!$include_noexec && !(-x _)) { |
322
|
0
|
|
|
|
|
0
|
$reason = "'$fn' is not an executable"; |
323
|
0
|
|
|
|
|
0
|
last; |
324
|
|
|
|
|
|
|
} |
325
|
0
|
|
|
|
|
0
|
my $fh; |
326
|
0
|
0
|
|
|
|
0
|
unless (open $fh, "<", $fn) { |
327
|
0
|
|
|
|
|
0
|
$reason = "Can't be read"; |
328
|
0
|
|
|
|
|
0
|
last; |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
# for efficiency, we read a bit only here |
331
|
0
|
|
|
|
|
0
|
read $fh, $str, 2; |
332
|
0
|
0
|
|
|
|
0
|
unless ($str eq '#!') { |
333
|
0
|
|
|
|
|
0
|
$reason = "Does not start with a shebang (#!) sequence"; |
334
|
0
|
|
|
|
|
0
|
last; |
335
|
|
|
|
|
|
|
} |
336
|
0
|
|
|
|
|
0
|
my $shebang = <$fh>; |
337
|
0
|
0
|
|
|
|
0
|
unless ($shebang =~ /perl/) { |
338
|
0
|
|
|
|
|
0
|
$reason = "Does not have 'perl' in the shebang line"; |
339
|
0
|
|
|
|
|
0
|
last; |
340
|
|
|
|
|
|
|
} |
341
|
0
|
|
|
|
|
0
|
seek $fh, 0, 0; |
342
|
|
|
|
|
|
|
{ |
343
|
0
|
|
|
|
|
0
|
local $/; |
|
0
|
|
|
|
|
0
|
|
344
|
0
|
|
|
|
|
0
|
$str = <$fh>; |
345
|
|
|
|
|
|
|
} |
346
|
0
|
|
|
|
|
0
|
close $fh; |
347
|
|
|
|
|
|
|
} |
348
|
0
|
0
|
|
|
|
0
|
unless ($str =~ /\A#!/) { |
349
|
0
|
|
|
|
|
0
|
$reason = "Does not start with a shebang (#!) sequence"; |
350
|
0
|
|
|
|
|
0
|
last; |
351
|
|
|
|
|
|
|
} |
352
|
0
|
0
|
|
|
|
0
|
unless ($str =~ /\A#!.*perl/) { |
353
|
0
|
|
|
|
|
0
|
$reason = "Does not have 'perl' in the shebang line"; |
354
|
0
|
|
|
|
|
0
|
last; |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
# NOTE: the presence of \s* pattern after ^ causes massive slowdown of |
358
|
|
|
|
|
|
|
# the regex when we reach many thousands of lines, so we use split() |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
#if ($str =~ /^\s*(use|require)\s+(Getopt::Long(?:::Complete)?)(\s|;)/m) { |
361
|
|
|
|
|
|
|
# $yesno = 1; |
362
|
|
|
|
|
|
|
# $extrameta{'func.module'} = $2; |
363
|
|
|
|
|
|
|
# last DETECT; |
364
|
|
|
|
|
|
|
#} |
365
|
|
|
|
|
|
|
|
366
|
0
|
|
|
|
|
0
|
for (split /^/, $str) { |
367
|
0
|
0
|
|
|
|
0
|
if (/^\s*(use|require)\s+(Getopt::Long(?:::Complete|::Less|::EvenLess)?)(\s|;|$)/) { |
368
|
0
|
|
|
|
|
0
|
$yesno = 1; |
369
|
0
|
|
|
|
|
0
|
$extrameta{'func.module'} = $2; |
370
|
0
|
|
|
|
|
0
|
last DETECT; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
0
|
|
|
|
|
0
|
$reason = "Can't find any statement requiring Getopt::Long(?::Complete|::Less|::EvenLess)? module"; |
375
|
|
|
|
|
|
|
} # DETECT |
376
|
|
|
|
|
|
|
|
377
|
0
|
|
|
|
|
0
|
[200, "OK", $yesno, {"func.reason"=>$reason, %extrameta}]; |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
$SPEC{gen_getopt_long_spec_from_getopt_std_spec} = { |
381
|
|
|
|
|
|
|
v => 1.1, |
382
|
|
|
|
|
|
|
summary => 'Generate Getopt::Long spec from Getopt::Std spec', |
383
|
|
|
|
|
|
|
args => { |
384
|
|
|
|
|
|
|
spec => { |
385
|
|
|
|
|
|
|
summary => 'Getopt::Std spec string', |
386
|
|
|
|
|
|
|
schema => 'str*', |
387
|
|
|
|
|
|
|
req => 1, |
388
|
|
|
|
|
|
|
pos => 0, |
389
|
|
|
|
|
|
|
}, |
390
|
|
|
|
|
|
|
is_getopt => { |
391
|
|
|
|
|
|
|
summary => 'Whether to assume spec is for getopt() or getopts()', |
392
|
|
|
|
|
|
|
description => <<'_', |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
By default spec is assumed to be for getopts() instead of getopt(). This means |
395
|
|
|
|
|
|
|
that for a spec like `abc:`, `a` and `b` don't take argument while `c` does. But |
396
|
|
|
|
|
|
|
if `is_getopt` is true, the meaning of `:` is reversed: `a` and `b` take |
397
|
|
|
|
|
|
|
arguments while `c` doesn't. |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
_ |
400
|
|
|
|
|
|
|
schema => 'bool', |
401
|
|
|
|
|
|
|
}, |
402
|
|
|
|
|
|
|
}, |
403
|
|
|
|
|
|
|
result_naked => 1, |
404
|
|
|
|
|
|
|
result => { |
405
|
|
|
|
|
|
|
schema => 'hash*', |
406
|
|
|
|
|
|
|
}, |
407
|
|
|
|
|
|
|
}; |
408
|
|
|
|
|
|
|
sub gen_getopt_long_spec_from_getopt_std_spec { |
409
|
0
|
|
|
0
|
1
|
0
|
my %args = @_; |
410
|
|
|
|
|
|
|
|
411
|
0
|
|
|
|
|
0
|
my $is_getopt = $args{is_getopt}; |
412
|
0
|
|
|
|
|
0
|
my $spec = {}; |
413
|
|
|
|
|
|
|
|
414
|
0
|
|
|
|
|
0
|
while ($args{spec} =~ /(.)(:?)/g) { |
415
|
|
|
|
|
|
|
$spec->{$1 . ($is_getopt ? ($2 ? "" : "=s") : ($2 ? "=s" : ""))} = |
416
|
0
|
0
|
|
0
|
|
0
|
sub {}; |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
0
|
|
|
|
|
0
|
$spec; |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
$SPEC{array_getopt_long_spec_to_hash} = { |
423
|
|
|
|
|
|
|
v => 1.1, |
424
|
|
|
|
|
|
|
summary => 'Convert array form of Getopt::Long spec to hash', |
425
|
|
|
|
|
|
|
description => <<'_', |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
's `GetOptions` function accepts a list of arguments. The first |
428
|
|
|
|
|
|
|
optional argument is a hash for option storage. After that, a list of option |
429
|
|
|
|
|
|
|
specs (e.g. `foo=s`), each optionally followed by a reference to specify |
430
|
|
|
|
|
|
|
destination (e.g. a reference to scalar, or array, or code). |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
Die on failure (e.g. invalid option spec). |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
This routine converts that array into a hash of option specs as keys and |
435
|
|
|
|
|
|
|
destinations as values. If an option spec does not have a destination, its |
436
|
|
|
|
|
|
|
destination is set to `undef`. If hash storage is specified then the destination |
437
|
|
|
|
|
|
|
will fall back to the hash storage's appropriate key when a specific destination |
438
|
|
|
|
|
|
|
is not specified. |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
Note that by converting to hash, 1) duplicate option specs are merged; and 2) |
441
|
|
|
|
|
|
|
order of option specs is not preserved. |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
_ |
444
|
|
|
|
|
|
|
args => { |
445
|
|
|
|
|
|
|
spec => { |
446
|
|
|
|
|
|
|
summary => 'Getopt::Long spec', |
447
|
|
|
|
|
|
|
schema => 'array*', |
448
|
|
|
|
|
|
|
req => 1, |
449
|
|
|
|
|
|
|
pos => 0, |
450
|
|
|
|
|
|
|
}, |
451
|
|
|
|
|
|
|
}, |
452
|
|
|
|
|
|
|
args_as => 'array', |
453
|
|
|
|
|
|
|
result_naked => 1, |
454
|
|
|
|
|
|
|
result => { |
455
|
|
|
|
|
|
|
schema => 'hash*', |
456
|
|
|
|
|
|
|
}, |
457
|
|
|
|
|
|
|
}; |
458
|
|
|
|
|
|
|
sub array_getopt_long_spec_to_hash { |
459
|
4
|
|
|
4
|
1
|
11805
|
my $go_spec = [ @_ ]; |
460
|
4
|
|
|
|
|
9
|
my $hash_spec = {}; |
461
|
|
|
|
|
|
|
|
462
|
4
|
|
|
|
|
7
|
my $hash_storage; |
463
|
4
|
100
|
100
|
|
|
22
|
$hash_storage = shift @$go_spec |
464
|
|
|
|
|
|
|
if @$go_spec && ref $go_spec->[0] eq 'HASH'; |
465
|
|
|
|
|
|
|
|
466
|
4
|
|
|
|
|
12
|
while (@$go_spec) { |
467
|
9
|
|
|
|
|
16
|
my $opt_spec = shift @$go_spec; |
468
|
9
|
|
|
|
|
12
|
my $dest; |
469
|
9
|
100
|
66
|
|
|
36
|
if (@$go_spec && ref $go_spec->[0]) { |
|
|
100
|
|
|
|
|
|
470
|
5
|
|
|
|
|
9
|
$dest = shift @$go_spec; |
471
|
|
|
|
|
|
|
} elsif ($hash_storage) { |
472
|
2
|
50
|
|
|
|
7
|
my $res = parse_getopt_long_opt_spec($opt_spec) |
473
|
|
|
|
|
|
|
or die "Invalid option spec '$opt_spec'"; |
474
|
2
|
|
|
|
|
5
|
my $name = $res->{opts}[0]; |
475
|
2
|
100
|
|
|
|
6
|
$hash_storage->{$name} = undef unless exists $hash_storage->{$name}; |
476
|
|
|
|
|
|
|
$dest = ref $hash_storage->{$name} ? |
477
|
|
|
|
|
|
|
$hash_storage->{$name} : |
478
|
2
|
100
|
|
|
|
9
|
\($hash_storage->{$name}); |
479
|
|
|
|
|
|
|
} |
480
|
9
|
|
|
|
|
24
|
$hash_spec->{$opt_spec} = $dest; |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
|
483
|
4
|
|
|
|
|
26
|
$hash_spec; |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
1; |
487
|
|
|
|
|
|
|
# ABSTRACT: Utilities for Getopt::Long |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
__END__ |