line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
2
|
|
|
|
|
|
|
package Getopt::O2;
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
91572
|
use 5.010;
|
|
2
|
|
|
|
|
8
|
|
5
|
2
|
|
|
2
|
|
9
|
use strict;
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
34
|
|
6
|
2
|
|
|
2
|
|
8
|
use warnings;
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
95
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
10
|
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
13
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.1.0';
|
11
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
12
|
2
|
|
|
2
|
|
967
|
use English '-no_match_vars';
|
|
2
|
|
|
|
|
2802
|
|
|
2
|
|
|
|
|
10
|
|
13
|
2
|
|
|
2
|
|
1501
|
use Readonly;
|
|
2
|
|
|
|
|
6538
|
|
|
2
|
|
|
|
|
154
|
|
14
|
|
|
|
|
|
|
Readonly my $USAGE_MARGIN => 80;
|
15
|
|
|
|
|
|
|
Readonly my $USAGE_OPTIONS_LENGTH => 29;
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
14
|
use Carp 'confess';
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
85
|
|
18
|
2
|
|
|
2
|
|
10
|
use Scalar::Util 'looks_like_number';
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
4404
|
|
19
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
20
|
|
|
|
|
|
|
sub new
|
21
|
|
|
|
|
|
|
{
|
22
|
27
|
100
|
|
27
|
0
|
43918
|
my $class = ref $_[0] ? ref $_[0] : $_[0];
|
23
|
27
|
|
|
|
|
80
|
my $self = bless {
|
24
|
|
|
|
|
|
|
shortOptions => {},
|
25
|
|
|
|
|
|
|
longOptions => {},
|
26
|
|
|
|
|
|
|
options => {}
|
27
|
|
|
|
|
|
|
}, $class;
|
28
|
|
|
|
|
|
|
|
29
|
27
|
|
|
|
|
56
|
return $self
|
30
|
|
|
|
|
|
|
}
|
31
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
32
|
|
|
|
|
|
|
sub getopt ## no critic (Subroutines::ProhibitExcessComplexity)
|
33
|
|
|
|
|
|
|
{
|
34
|
27
|
|
|
27
|
1
|
7888
|
my $self = shift;
|
35
|
27
|
|
|
|
|
37
|
my $dest = shift;
|
36
|
27
|
|
|
|
|
30
|
my $args = shift;
|
37
|
27
|
|
|
|
|
44
|
my ($arg,$key,$rule,%context,@arguments);
|
38
|
|
|
|
|
|
|
|
39
|
27
|
|
|
|
|
123
|
$self->{'options'} = {%$dest};
|
40
|
27
|
|
|
|
|
107
|
$self->parse_rules();
|
41
|
|
|
|
|
|
|
|
42
|
22
|
|
|
|
|
48
|
PROCESS_ARGUMENTS: while (@ARGV) {
|
43
|
46
|
|
|
|
|
56
|
$arg = shift @ARGV;
|
44
|
|
|
|
|
|
|
|
45
|
46
|
100
|
100
|
|
|
303
|
if (!defined $arg || !length $arg || '-' eq $arg || $arg !~ /^-/) {
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
46
|
5
|
|
|
|
|
7
|
push @arguments, $arg;
|
47
|
5
|
|
|
|
|
6
|
next PROCESS_ARGUMENTS;
|
48
|
|
|
|
|
|
|
} elsif ('--' eq $arg) {
|
49
|
2
|
|
|
|
|
5
|
push @arguments, @ARGV;
|
50
|
2
|
|
|
|
|
4
|
last PROCESS_ARGUMENTS;
|
51
|
|
|
|
|
|
|
}
|
52
|
|
|
|
|
|
|
|
53
|
39
|
100
|
|
|
|
86
|
if ($arg !~ /^--/) {
|
54
|
14
|
|
|
|
|
20
|
$key = (substr $arg, 1, 1);
|
55
|
14
|
|
|
|
|
21
|
$rule = $self->{'shortOptions'}->{$key};
|
56
|
14
|
100
|
|
|
|
25
|
$self->error('No such option "-%s"', $key)
|
57
|
|
|
|
|
|
|
unless defined $rule;
|
58
|
13
|
|
|
|
|
19
|
$rule = $self->{'longOptions'}->{$rule};
|
59
|
|
|
|
|
|
|
|
60
|
13
|
100
|
|
|
|
20
|
if (length $arg > 2) {
|
61
|
5
|
100
|
|
|
|
10
|
if ($rule->type) { ## no critic (ControlStructures::ProhibitDeepNests)
|
62
|
1
|
|
|
|
|
3
|
unshift @ARGV, (substr $arg, 2);
|
63
|
|
|
|
|
|
|
} else {
|
64
|
4
|
|
|
|
|
9
|
unshift @ARGV, '-'.(substr $arg, 2);
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
}
|
67
|
|
|
|
|
|
|
} else {
|
68
|
25
|
|
|
|
|
40
|
$key = (substr $arg, 2);
|
69
|
|
|
|
|
|
|
|
70
|
25
|
100
|
|
|
|
61
|
if (~(index $key, '=')) {
|
71
|
1
|
|
|
|
|
4
|
($key,$arg) = (split /=/, $key, 2);
|
72
|
1
|
|
|
|
|
3
|
unshift @ARGV, $arg;
|
73
|
|
|
|
|
|
|
}
|
74
|
|
|
|
|
|
|
|
75
|
25
|
|
|
|
|
42
|
$rule = $self->{'longOptions'}->{$key};
|
76
|
25
|
100
|
|
|
|
39
|
unless (defined $rule) {
|
77
|
5
|
100
|
|
|
|
20
|
$self->error('No such option "--%s"', $key)
|
78
|
|
|
|
|
|
|
if 0 != (index $key, 'no-');
|
79
|
3
|
|
|
|
|
6
|
$key = (substr $key, 3);
|
80
|
3
|
|
|
|
|
5
|
$rule = $self->{'longOptions'}->{$key};
|
81
|
|
|
|
|
|
|
|
82
|
3
|
100
|
100
|
|
|
15
|
$self->error('No such option "--no-%s" or negatable "--%s"', $key, $key)
|
83
|
|
|
|
|
|
|
unless defined $rule && $rule->negatable;
|
84
|
1
|
|
|
|
|
2
|
$rule->{'_negate'} = 1;
|
85
|
|
|
|
|
|
|
}
|
86
|
|
|
|
|
|
|
}
|
87
|
|
|
|
|
|
|
|
88
|
34
|
100
|
|
|
|
53
|
if (defined $rule->context) {
|
89
|
1
|
|
|
|
|
11
|
foreach (@{$rule->context->{'need'}}) {
|
|
1
|
|
|
|
|
4
|
|
90
|
|
|
|
|
|
|
$self->error('Option "--%s" cannot be used in this context.', $rule->long)
|
91
|
1
|
50
|
|
|
|
5
|
unless exists $context{$_};
|
92
|
|
|
|
|
|
|
}
|
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
0
|
delete $context{$_} foreach @{$rule->context->{'clear'}};
|
|
0
|
|
|
|
|
0
|
|
95
|
0
|
|
|
|
|
0
|
$context{$_} = 1 foreach @{$rule->context->{'set'}};
|
|
0
|
|
|
|
|
0
|
|
96
|
|
|
|
|
|
|
}
|
97
|
|
|
|
|
|
|
|
98
|
33
|
100
|
|
|
|
47
|
if ($rule->multiple) {
|
99
|
|
|
|
|
|
|
$self->{'options'}->{$rule->long} = 0
|
100
|
3
|
100
|
|
|
|
6
|
unless exists $self->{'options'}->{$rule->long};
|
101
|
3
|
|
|
|
|
6
|
++$self->{'options'}->{$rule->long};
|
102
|
3
|
|
|
|
|
6
|
$rule->mark_used;
|
103
|
3
|
|
|
|
|
5
|
next PROCESS_ARGUMENTS;
|
104
|
|
|
|
|
|
|
}
|
105
|
|
|
|
|
|
|
|
106
|
30
|
100
|
|
|
|
43
|
unless (defined $rule->type) {
|
107
|
6
|
|
|
|
|
13
|
$arg = undef;
|
108
|
|
|
|
|
|
|
} else {
|
109
|
24
|
|
|
|
|
69
|
$arg = $self->get_value();
|
110
|
24
|
100
|
|
|
|
55
|
$self->error('Option "--%s" requires a value.', $rule->long)
|
111
|
|
|
|
|
|
|
unless defined $arg;
|
112
|
|
|
|
|
|
|
|
113
|
22
|
100
|
|
|
|
29
|
delete $self->{'options'}->{$rule->long}
|
114
|
|
|
|
|
|
|
if $rule->is_unused;
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$self->{'options'}->{$rule->long} = []
|
117
|
22
|
100
|
100
|
|
|
35
|
if $rule->is_list && !defined $self->{'options'}->{$rule->long};
|
118
|
|
|
|
|
|
|
|
119
|
22
|
|
|
|
|
37
|
given($rule->type) {
|
120
|
22
|
|
|
|
|
49
|
when('s') {
|
121
|
|
|
|
|
|
|
}
|
122
|
|
|
|
|
|
|
|
123
|
11
|
|
|
|
|
14
|
when('i') {
|
124
|
2
|
100
|
|
|
|
10
|
$self->error('Argument "%s" to "--%s" isn\'t numeric', $arg, $rule->long)
|
125
|
|
|
|
|
|
|
unless looks_like_number($arg);
|
126
|
1
|
|
|
|
|
3
|
$arg = int $arg;
|
127
|
|
|
|
|
|
|
}
|
128
|
|
|
|
|
|
|
|
129
|
9
|
|
|
|
|
12
|
when('?') {
|
130
|
|
|
|
|
|
|
$self->error('Value "%s" to argument "--%s" is invalid.', $arg, $rule->long)
|
131
|
9
|
100
|
|
|
|
10
|
unless $arg ~~ @{$rule->values || []};
|
|
9
|
100
|
|
|
|
19
|
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
}
|
134
|
|
|
|
|
|
|
|
135
|
19
|
100
|
|
|
|
34
|
if ($rule->is_list) {
|
136
|
13
|
100
|
|
|
|
16
|
if ('?' ne $rule->type) { ## no critic (ControlStructures::ProhibitDeepNests)
|
137
|
7
|
|
|
|
|
9
|
push @{$self->{'options'}->{$rule->long}}, $arg;
|
|
7
|
|
|
|
|
12
|
|
138
|
|
|
|
|
|
|
} else {
|
139
|
5
|
|
|
|
|
8
|
push @{$self->{'options'}->{$rule->long}}, $arg
|
140
|
6
|
100
|
100
|
|
|
7
|
unless ($rule->keep_unique && $arg ~~ @{$self->{'options'}->{$rule->long}});
|
|
4
|
|
|
|
|
8
|
|
141
|
|
|
|
|
|
|
}
|
142
|
|
|
|
|
|
|
|
143
|
13
|
|
|
|
|
21
|
$rule->mark_used;
|
144
|
13
|
|
|
|
|
26
|
next PROCESS_ARGUMENTS;
|
145
|
|
|
|
|
|
|
}
|
146
|
|
|
|
|
|
|
}
|
147
|
|
|
|
|
|
|
|
148
|
12
|
|
|
|
|
26
|
$rule->mark_used;
|
149
|
|
|
|
|
|
|
|
150
|
12
|
100
|
|
|
|
19
|
if (defined $rule->action) {
|
151
|
1
|
|
|
|
|
3
|
$arg = $rule->action->($arg, $key, $rule);
|
152
|
|
|
|
|
|
|
} else {
|
153
|
11
|
100
|
|
|
|
23
|
$arg = $rule->{'_negate'} ? '' : 1
|
|
|
100
|
|
|
|
|
|
154
|
|
|
|
|
|
|
unless defined $arg;
|
155
|
|
|
|
|
|
|
}
|
156
|
|
|
|
|
|
|
|
157
|
11
|
|
|
|
|
14
|
$self->{'options'}->{$rule->long} = $arg;
|
158
|
|
|
|
|
|
|
}
|
159
|
|
|
|
|
|
|
|
160
|
10
|
|
|
|
|
51
|
$self->check_rule_obligations(%context);
|
161
|
|
|
|
|
|
|
|
162
|
10
|
|
|
|
|
13
|
%$dest = %{$self->{'options'}};
|
|
10
|
|
|
|
|
26
|
|
163
|
10
|
100
|
|
|
|
23
|
@$args = @arguments if ref $args;
|
164
|
10
|
|
|
|
|
21
|
$self->{'options'} = {};
|
165
|
10
|
|
|
|
|
22
|
return $self
|
166
|
|
|
|
|
|
|
}
|
167
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
168
|
|
|
|
|
|
|
sub error
|
169
|
|
|
|
|
|
|
{
|
170
|
1
|
|
|
1
|
1
|
7
|
return shift->usage(1, shift(), @_);
|
171
|
|
|
|
|
|
|
}
|
172
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
173
|
|
|
|
|
|
|
# Returns program name for display in usage
|
174
|
|
|
|
|
|
|
sub get_program
|
175
|
|
|
|
|
|
|
{
|
176
|
5
|
|
|
5
|
1
|
9
|
my $program = $ENV{_};
|
177
|
5
|
|
|
|
|
30
|
$program =~ s{.*/([^/]+)$}{$1};
|
178
|
5
|
50
|
|
|
|
17
|
$program = $PROGRAM_NAME if 'perl' eq $program;
|
179
|
5
|
|
|
|
|
46
|
return $program;
|
180
|
|
|
|
|
|
|
}
|
181
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
182
|
|
|
|
|
|
|
# Return short program description string; displayed in usage
|
183
|
|
|
|
|
|
|
sub get_program_description
|
184
|
|
|
|
|
|
|
{
|
185
|
2
|
|
|
2
|
1
|
4
|
my $class = ref $_[0];
|
186
|
2
|
|
|
|
|
11
|
return qq{another example of this programmer's lazyness: it forgot the description (and should implement ${class}::get_program_description())}
|
187
|
|
|
|
|
|
|
}
|
188
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
189
|
|
|
|
|
|
|
sub get_value
|
190
|
|
|
|
|
|
|
{
|
191
|
24
|
100
|
|
24
|
0
|
37
|
return unless @ARGV;
|
192
|
23
|
|
|
|
|
32
|
my $value = $ARGV[0];
|
193
|
|
|
|
|
|
|
return shift @ARGV
|
194
|
23
|
50
|
100
|
|
|
138
|
if !defined $value || !length $value || '-' eq $value || $value !~ /^-/;
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
195
|
0
|
0
|
|
|
|
0
|
return if $value ne '--';
|
196
|
0
|
|
|
|
|
0
|
shift @ARGV;
|
197
|
0
|
0
|
|
|
|
0
|
return unless @ARGV;
|
198
|
0
|
|
|
|
|
0
|
$value = shift @ARGV;
|
199
|
0
|
|
|
|
|
0
|
unshift @ARGV, '--';
|
200
|
0
|
|
|
|
|
0
|
return $value;
|
201
|
|
|
|
|
|
|
}
|
202
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
203
|
|
|
|
|
|
|
sub get_option_rules
|
204
|
|
|
|
|
|
|
{
|
205
|
7
|
|
|
7
|
1
|
170
|
my $self = shift;
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
return
|
208
|
7
|
|
|
1
|
|
56
|
'h|help' => ['Display this help message', sub {$self->usage(0)}],
|
|
1
|
|
|
|
|
7
|
|
209
|
|
|
|
|
|
|
'v|verbose+' => 'Increase program verbosity',
|
210
|
|
|
|
|
|
|
undef
|
211
|
|
|
|
|
|
|
}
|
212
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
213
|
|
|
|
|
|
|
sub parse_rules ## no critic (Subroutines::ProhibitExcessComplexity)
|
214
|
|
|
|
|
|
|
{
|
215
|
30
|
|
|
30
|
0
|
40
|
my $self = shift;
|
216
|
30
|
|
|
|
|
58
|
my @rules = $self->get_option_rules();
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
## Perl Critic false positive on "$}" at the end of the reg-ex
|
219
|
|
|
|
|
|
|
## no critic (Variables::ProhibitPunctuationVars)
|
220
|
30
|
|
|
|
|
1433
|
state $pattern = qr{^
|
221
|
|
|
|
|
|
|
(?:(?P!))?
|
222
|
|
|
|
|
|
|
(?:(?P[[:alpha:]])[|])?
|
223
|
|
|
|
|
|
|
(?P[[:alpha:]](?:[[:alpha:]-]*)[[:alpha:]])
|
224
|
|
|
|
|
|
|
(?:
|
225
|
|
|
|
|
|
|
(?:=(?P[si?]@?))
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
(?P[+])
|
228
|
|
|
|
|
|
|
)?
|
229
|
|
|
|
|
|
|
$}x;
|
230
|
|
|
|
|
|
|
## use critic
|
231
|
|
|
|
|
|
|
|
232
|
30
|
|
|
|
|
45
|
my ($arg,$opt,@parsed);
|
233
|
|
|
|
|
|
|
|
234
|
30
|
|
|
|
|
71
|
while (@rules) {
|
235
|
68
|
|
|
|
|
91
|
$arg = shift @rules;
|
236
|
68
|
100
|
|
|
|
146
|
unless (defined $arg) {
|
237
|
9
|
100
|
|
|
|
17
|
push @parsed, undef if wantarray;
|
238
|
9
|
|
|
|
|
16
|
next;
|
239
|
|
|
|
|
|
|
}
|
240
|
59
|
|
|
|
|
66
|
$opt = $arg;
|
241
|
59
|
100
|
|
|
|
251
|
confess('Not enough rules') unless @rules;
|
242
|
58
|
|
|
|
|
72
|
$arg = shift @rules;
|
243
|
|
|
|
|
|
|
|
244
|
58
|
100
|
|
|
|
105
|
$arg = [$arg] unless ref $arg;
|
245
|
58
|
100
|
|
|
|
495
|
confess("Invalid rule pattern '$opt'") if $opt !~ $pattern;
|
246
|
57
|
|
|
|
|
439
|
my $rule = Getopt::O2::Rule->new($arg, %LAST_PAREN_MATCH);
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
confess(sprintf q{Option spec '%s' redefines long option '%s'}, $opt, $rule->long)
|
249
|
56
|
100
|
|
|
|
157
|
if exists $self->{'longOptions'}->{$rule->long};
|
250
|
|
|
|
|
|
|
|
251
|
55
|
100
|
|
|
|
91
|
if (defined $rule->short) {
|
252
|
|
|
|
|
|
|
confess(sprintf q{Option spec '%s' redefines short option '%s'}, $opt, $rule->short)
|
253
|
34
|
100
|
|
|
|
52
|
if exists $self->{'shortOptions'}->{$rule->short};
|
254
|
33
|
|
|
|
|
46
|
$self->{'shortOptions'}->{$rule->short} = $rule->long;
|
255
|
|
|
|
|
|
|
}
|
256
|
|
|
|
|
|
|
|
257
|
54
|
100
|
|
|
|
87
|
if (defined $rule->default) {
|
258
|
4
|
|
|
|
|
6
|
$self->{'options'}->{$rule->long} = $rule->default;
|
259
|
|
|
|
|
|
|
}
|
260
|
|
|
|
|
|
|
|
261
|
54
|
|
|
|
|
80
|
$self->{'longOptions'}->{$rule->long} = $rule;
|
262
|
54
|
100
|
|
|
|
132
|
push @parsed, $rule if wantarray
|
263
|
|
|
|
|
|
|
}
|
264
|
|
|
|
|
|
|
|
265
|
25
|
100
|
|
|
|
56
|
return $self unless wantarray;
|
266
|
3
|
|
|
|
|
9
|
return @parsed;
|
267
|
|
|
|
|
|
|
}
|
268
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
269
|
|
|
|
|
|
|
sub show_option_default_values
|
270
|
|
|
|
|
|
|
{
|
271
|
3
|
|
|
3
|
0
|
4
|
return 1;
|
272
|
|
|
|
|
|
|
}
|
273
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
274
|
|
|
|
|
|
|
sub check_rule_obligations
|
275
|
|
|
|
|
|
|
{
|
276
|
10
|
|
|
10
|
0
|
12
|
my $self = shift;
|
277
|
10
|
|
|
|
|
25
|
my %context = @_;
|
278
|
|
|
|
|
|
|
my @missing = sort grep {
|
279
|
18
|
|
|
|
|
29
|
is_rule_missing($self->{'longOptions'}->{$_}, %context)
|
280
|
10
|
|
|
|
|
12
|
} keys %{$self->{'longOptions'}};
|
|
10
|
|
|
|
|
23
|
|
281
|
|
|
|
|
|
|
|
282
|
10
|
50
|
|
|
|
27
|
return $self unless @missing;
|
283
|
0
|
|
|
|
|
0
|
@missing = map {"`$_`"} @missing;
|
|
0
|
|
|
|
|
0
|
|
284
|
0
|
|
|
|
|
0
|
my $one = 1 == scalar @missing;
|
285
|
0
|
0
|
|
|
|
0
|
my $missing = $one
|
286
|
|
|
|
|
|
|
? $missing[0]
|
287
|
|
|
|
|
|
|
: sprintf '%s and %s', join(', ', @missing[0..$#missing-1]), $missing[-1];
|
288
|
|
|
|
|
|
|
|
289
|
0
|
0
|
|
|
|
0
|
return $self->usage(1, 'Missing required option%s %s.', $one ? '' : 's', $missing);
|
290
|
|
|
|
|
|
|
}
|
291
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
292
|
|
|
|
|
|
|
sub is_rule_missing
|
293
|
|
|
|
|
|
|
{
|
294
|
18
|
|
|
18
|
0
|
22
|
my $rule = shift;
|
295
|
18
|
|
|
|
|
27
|
my %context = @_;
|
296
|
|
|
|
|
|
|
|
297
|
18
|
50
|
|
|
|
23
|
return unless $rule->required;
|
298
|
0
|
0
|
|
|
|
0
|
return unless $rule->is_unused;
|
299
|
0
|
0
|
|
|
|
0
|
return 1 unless $rule->context;
|
300
|
|
|
|
|
|
|
|
301
|
0
|
|
|
|
|
0
|
foreach (@{$rule->context->{'need'}}) {
|
|
0
|
|
|
|
|
0
|
|
302
|
0
|
0
|
|
|
|
0
|
return 1 if $context{$_};
|
303
|
|
|
|
|
|
|
}
|
304
|
|
|
|
|
|
|
|
305
|
0
|
|
|
|
|
0
|
return;
|
306
|
|
|
|
|
|
|
}
|
307
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
308
|
|
|
|
|
|
|
sub usage ## no critic (Subroutines::ProhibitExcessComplexity)
|
309
|
|
|
|
|
|
|
{
|
310
|
3
|
|
|
3
|
1
|
1552
|
my $self = shift;
|
311
|
3
|
|
|
|
|
25
|
my ($exitCode,$message,@args) = @_;
|
312
|
|
|
|
|
|
|
|
313
|
3
|
100
|
|
|
|
8
|
if (defined $message) {
|
314
|
1
|
|
|
|
|
6
|
$message = sprintf "Error: $message", @args;
|
315
|
|
|
|
|
|
|
} else {
|
316
|
2
|
|
|
|
|
8
|
$message = sprintf '%s - %s', $self->get_program(), $self->get_program_description();
|
317
|
|
|
|
|
|
|
}
|
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
print STDERR "$_\n"
|
320
|
3
|
|
|
|
|
10
|
foreach wrap_string($message, 0, 8, $USAGE_MARGIN);
|
321
|
3
|
|
|
|
|
19
|
printf STDERR "\nUsage: %s [options...]\n\nValid options:\n\n", $self->get_program();
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
## no critic (Variables::ProhibitLocalVars)
|
324
|
3
|
|
|
|
|
14
|
local $self->{'longOptions'} = undef;
|
325
|
3
|
|
|
|
|
6
|
local $self->{'shortOptions'} = undef;
|
326
|
|
|
|
|
|
|
## use critic
|
327
|
|
|
|
|
|
|
|
328
|
3
|
|
|
|
|
8
|
my @rules = $self->parse_rules();
|
329
|
3
|
|
|
|
|
6
|
my ($rule,$line,$long,$len,$show_default,$have_required);
|
330
|
|
|
|
|
|
|
|
331
|
3
|
|
|
|
|
13
|
$show_default = $self->show_option_default_values();
|
332
|
3
|
|
|
|
|
27
|
$have_required = 0;
|
333
|
|
|
|
|
|
|
|
334
|
3
|
|
|
|
|
9
|
PROCESS_RULES: while (@rules) {
|
335
|
|
|
|
|
|
|
#@type Getopt::O2::Rule
|
336
|
17
|
|
|
|
|
34
|
$rule = shift @rules;
|
337
|
|
|
|
|
|
|
|
338
|
17
|
100
|
|
|
|
27
|
unless (defined $rule) {
|
339
|
4
|
|
|
|
|
30
|
print STDERR "\n";
|
340
|
4
|
|
|
|
|
13
|
next PROCESS_RULES;
|
341
|
|
|
|
|
|
|
}
|
342
|
|
|
|
|
|
|
|
343
|
13
|
|
|
|
|
16
|
$line = ' ';
|
344
|
13
|
|
|
|
|
23
|
$long = $rule->long;
|
345
|
13
|
100
|
|
|
|
21
|
$long = "(no-)$long" if $rule->negatable;
|
346
|
|
|
|
|
|
|
|
347
|
13
|
100
|
|
|
|
20
|
unless (defined $rule->short) {
|
348
|
2
|
|
|
|
|
4
|
$long = "--$long";
|
349
|
|
|
|
|
|
|
} else {
|
350
|
11
|
|
|
|
|
20
|
$long = " [--$long]";
|
351
|
11
|
|
|
|
|
14
|
$line .= '-'.$rule->short;
|
352
|
|
|
|
|
|
|
}
|
353
|
|
|
|
|
|
|
|
354
|
13
|
|
|
|
|
19
|
$line = "$line$long";
|
355
|
13
|
100
|
|
|
|
17
|
$line .= ' ARG' if defined $rule->type;
|
356
|
|
|
|
|
|
|
|
357
|
13
|
100
|
|
|
|
40
|
$line .= ' ' x ($USAGE_OPTIONS_LENGTH - $len)
|
358
|
|
|
|
|
|
|
if $USAGE_OPTIONS_LENGTH > ($len = length($line) + 2);
|
359
|
13
|
|
|
|
|
96
|
$line = "$line: ";
|
360
|
13
|
|
|
|
|
118
|
print STDERR $line;
|
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
print STDERR "$_\n"
|
363
|
13
|
|
|
|
|
37
|
foreach wrap_string($rule->help($show_default), length $line, $USAGE_OPTIONS_LENGTH, $USAGE_MARGIN);
|
364
|
13
|
|
33
|
|
|
55
|
$have_required ||= $rule->required;
|
365
|
|
|
|
|
|
|
}
|
366
|
|
|
|
|
|
|
|
367
|
3
|
|
|
|
|
23
|
print STDERR "\n";
|
368
|
3
|
50
|
|
|
|
8
|
print STDERR "Options marked with '*' are required options.\n\n" if 0 != $have_required;
|
369
|
3
|
|
|
|
|
10
|
exit $exitCode;
|
370
|
|
|
|
|
|
|
}
|
371
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
372
|
|
|
|
|
|
|
sub wrap_string
|
373
|
|
|
|
|
|
|
{
|
374
|
16
|
|
|
16
|
0
|
53
|
my ($string,$firstIndent,$leftIndent,$wrapAt) = @_;
|
375
|
16
|
|
|
|
|
101
|
my (@lines,$len,$pos,$nChars);
|
376
|
|
|
|
|
|
|
|
377
|
16
|
|
|
|
|
29
|
for ($nChars = $wrapAt - $firstIndent; length $string; $nChars = $wrapAt - $leftIndent) {
|
378
|
23
|
|
|
|
|
25
|
$len = length $string;
|
379
|
|
|
|
|
|
|
|
380
|
23
|
100
|
|
|
|
43
|
if ($len < $nChars) {
|
381
|
16
|
|
|
|
|
20
|
push @lines, $string;
|
382
|
16
|
|
|
|
|
25
|
last;
|
383
|
|
|
|
|
|
|
}
|
384
|
|
|
|
|
|
|
|
385
|
7
|
|
|
|
|
17
|
$pos = strrpos((substr $string, 0, $nChars), ' ');
|
386
|
7
|
100
|
|
|
|
15
|
if (-1 == $pos) {
|
387
|
1
|
|
|
|
|
2
|
push @lines, (substr $string, 0, $nChars);
|
388
|
1
|
|
|
|
|
2
|
$string = (substr $string, $nChars);
|
389
|
|
|
|
|
|
|
} else {
|
390
|
6
|
|
|
|
|
13
|
push @lines, (substr $string, 0, $pos);
|
391
|
6
|
|
|
|
|
15
|
$string = (substr $string, $pos + 1);
|
392
|
|
|
|
|
|
|
}
|
393
|
|
|
|
|
|
|
}
|
394
|
|
|
|
|
|
|
|
395
|
16
|
100
|
|
|
|
24
|
if (@lines > 1) {
|
396
|
3
|
|
|
|
|
7
|
my $indent = ' ' x $leftIndent;
|
397
|
3
|
|
|
|
|
15
|
$lines[$_] = "$indent$lines[$_]" foreach (1..$#lines);
|
398
|
|
|
|
|
|
|
}
|
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
return @lines
|
401
|
16
|
|
|
|
|
284
|
}
|
402
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
403
|
|
|
|
|
|
|
sub strrpos
|
404
|
|
|
|
|
|
|
{
|
405
|
7
|
|
|
7
|
0
|
22
|
my ($string,$find) = @_;
|
406
|
7
|
|
|
|
|
16
|
my ($length) = length $find;
|
407
|
|
|
|
|
|
|
|
408
|
7
|
|
|
|
|
16
|
for (my $pos = length($string) - 1; $pos >= 0; --$pos) {
|
409
|
132
|
100
|
|
|
|
209
|
return $pos if $find eq (substr $string, $pos, $length);
|
410
|
|
|
|
|
|
|
}
|
411
|
|
|
|
|
|
|
|
412
|
1
|
|
|
|
|
2
|
return -1
|
413
|
|
|
|
|
|
|
}
|
414
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
415
|
|
|
|
|
|
|
package Getopt::O2::Rule; ## no critic (Modules::ProhibitMultiplePackages)
|
416
|
|
|
|
|
|
|
|
417
|
2
|
|
|
2
|
|
15
|
use strict;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
63
|
|
418
|
2
|
|
|
2
|
|
10
|
use warnings;
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
62
|
|
419
|
2
|
|
|
2
|
|
11
|
use feature ':5.10';
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
236
|
|
420
|
|
|
|
|
|
|
|
421
|
2
|
|
|
2
|
|
11
|
use Carp 'confess';
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
180
|
|
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
BEGIN {
|
424
|
|
|
|
|
|
|
## no critic (TestingAndDebugging::ProhibitNoStrict)
|
425
|
2
|
|
|
2
|
|
12
|
no strict 'refs';
|
|
2
|
|
|
|
|
22
|
|
|
2
|
|
|
|
|
190
|
|
426
|
2
|
|
|
2
|
|
7
|
foreach my $method (qw(action context default is_list keep_unique long multiple negatable required short type values)) {
|
427
|
24
|
|
|
718
|
|
1577
|
*{__PACKAGE__."::$method"} = sub {shift->{$method}}
|
|
718
|
|
|
|
|
1615
|
|
428
|
24
|
|
|
|
|
60
|
}
|
429
|
|
|
|
|
|
|
## use critic
|
430
|
|
|
|
|
|
|
}
|
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
sub new ## no critic (Subroutines::ProhibitExcessComplexity)
|
433
|
|
|
|
|
|
|
{
|
434
|
57
|
|
|
57
|
|
89
|
my $class = shift;
|
435
|
57
|
|
|
|
|
283
|
my ($arg, %options) = @_;
|
436
|
57
|
|
|
|
|
98
|
my (%rule);
|
437
|
|
|
|
|
|
|
|
438
|
57
|
|
|
|
|
98
|
$rule{'long'} = $options{'long'};
|
439
|
57
|
100
|
|
|
|
108
|
$rule{'short'} = $options{'short'} if exists $options{'short'};
|
440
|
|
|
|
|
|
|
|
441
|
57
|
|
100
|
|
|
181
|
$rule{'negatable'} = $options{'negatable'} // 0;
|
442
|
|
|
|
|
|
|
|
443
|
57
|
100
|
|
|
|
125
|
if ($options{'multiple'}) {
|
|
|
100
|
|
|
|
|
|
444
|
8
|
|
|
|
|
13
|
$rule{'multiple'} = 1
|
445
|
|
|
|
|
|
|
} elsif ($options{'type'}) {
|
446
|
22
|
|
|
|
|
47
|
$rule{'type'} = (substr $options{'type'}, 0, 1);
|
447
|
22
|
|
|
|
|
51
|
$rule{'is_list'} = ~(index $options{'type'}, '@');
|
448
|
|
|
|
|
|
|
$rule{'keep_unique'} = $options{'keep_unique'} // 1
|
449
|
22
|
100
|
50
|
|
|
50
|
if $rule{'is_list'};
|
450
|
|
|
|
|
|
|
}
|
451
|
|
|
|
|
|
|
|
452
|
57
|
|
|
|
|
79
|
$rule{'help'} = shift @$arg;
|
453
|
57
|
|
|
|
|
268
|
$rule{'help'} =~ s/^\s+|\s+$//g;
|
454
|
57
|
|
|
|
|
218
|
$rule{'help'} =~ s/\s+/ /g;
|
455
|
57
|
100
|
|
|
|
145
|
$rule{'help'} .= '.' if $rule{'help'} !~ /[.]$/;
|
456
|
|
|
|
|
|
|
|
457
|
57
|
100
|
|
|
|
94
|
if (@$arg) {
|
458
|
22
|
100
|
|
|
|
42
|
$rule{'action'} = shift @$arg
|
459
|
|
|
|
|
|
|
if 'CODE' eq ref $arg->[0];
|
460
|
22
|
100
|
|
|
|
165
|
confess('Invalid rule options; the remainder is a list with uneven members')
|
461
|
|
|
|
|
|
|
if 0 != (@$arg % 2);
|
462
|
21
|
|
|
|
|
106
|
%rule = (%rule, @$arg);
|
463
|
|
|
|
|
|
|
}
|
464
|
|
|
|
|
|
|
|
465
|
56
|
|
50
|
|
|
206
|
$rule{'required'} //= 0;
|
466
|
|
|
|
|
|
|
|
467
|
56
|
50
|
|
|
|
82
|
if ($rule{'required'}) {
|
468
|
0
|
0
|
|
|
|
0
|
confess 'A rule cannot be required and have a default value' if exists $rule{'default'};
|
469
|
0
|
0
|
|
|
|
0
|
confess 'A flag-rule cannot be required' unless exists $rule{'type'};
|
470
|
|
|
|
|
|
|
}
|
471
|
|
|
|
|
|
|
|
472
|
56
|
100
|
|
|
|
91
|
if (defined $rule{'context'}) {
|
473
|
2
|
|
|
|
|
7
|
$rule{'context'} = [split /,/, $rule{'context'}];
|
474
|
|
|
|
|
|
|
$rule{'context'} = {
|
475
|
1
|
|
|
|
|
4
|
set => [map {(substr $_, 1)} grep {/^[+]/} @{$rule{'context'}}],
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
5
|
|
476
|
0
|
|
|
|
|
0
|
clear => [map {(substr $_, 1)} grep {/^-/} @{$rule{'context'}}],
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
13
|
|
477
|
2
|
|
|
|
|
3
|
need => [grep {/^[^+-]/} @{$rule{'context'}}],
|
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
4
|
|
478
|
|
|
|
|
|
|
};
|
479
|
|
|
|
|
|
|
}
|
480
|
|
|
|
|
|
|
|
481
|
56
|
|
|
|
|
78
|
$rule{'_used'} = 0;
|
482
|
|
|
|
|
|
|
|
483
|
56
|
|
|
|
|
134
|
return bless \%rule, $class
|
484
|
|
|
|
|
|
|
}
|
485
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
486
|
|
|
|
|
|
|
sub is_unused
|
487
|
|
|
|
|
|
|
{
|
488
|
22
|
|
|
22
|
|
55
|
return !shift->{'_used'};
|
489
|
|
|
|
|
|
|
}
|
490
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
491
|
|
|
|
|
|
|
sub mark_used
|
492
|
|
|
|
|
|
|
{
|
493
|
28
|
|
|
28
|
|
33
|
my $self = shift;
|
494
|
28
|
|
|
|
|
31
|
$self->{'_used'} = 1;
|
495
|
28
|
|
|
|
|
30
|
return $self;
|
496
|
|
|
|
|
|
|
}
|
497
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
498
|
|
|
|
|
|
|
sub help
|
499
|
|
|
|
|
|
|
{
|
500
|
13
|
|
|
13
|
|
18
|
my $self = shift;
|
501
|
13
|
|
|
|
|
13
|
my $show_default = shift;
|
502
|
13
|
50
|
|
|
|
18
|
my $obligation_suffix = $self->required ? ' *' : '';
|
503
|
13
|
|
|
|
|
33
|
my $helpstr = $self->{'help'} . $obligation_suffix;
|
504
|
|
|
|
|
|
|
|
505
|
13
|
100
|
|
|
|
25
|
unless (defined $self->{'type'}) { # flags
|
|
|
100
|
|
|
|
|
|
506
|
10
|
|
|
|
|
30
|
return $helpstr;
|
507
|
0
|
|
|
|
|
0
|
} elsif ('?' ne $self->{'type'}) { # anything but ENUM
|
508
|
2
|
50
|
33
|
|
|
11
|
return $helpstr unless $show_default && defined $self->{'default'};
|
509
|
|
|
|
|
|
|
|
510
|
0
|
|
|
|
|
0
|
$helpstr =~ s/\s*[.]\s*$//;
|
511
|
0
|
|
|
|
|
0
|
return sprintf '%s (default: "%s").', $helpstr, $self->{'default'};
|
512
|
|
|
|
|
|
|
} else {
|
513
|
1
|
|
|
|
|
2
|
my @values = map {qq{"$_"}} @{$self->values};
|
|
3
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2
|
|
514
|
|
|
|
|
|
|
my $default_value = ($show_default && defined $self->{'default'})
|
515
|
1
|
50
|
33
|
|
|
13
|
? (sprintf ' [default: "%s"]', $self->{'default'})
|
516
|
|
|
|
|
|
|
: '';
|
517
|
1
|
|
|
|
|
10
|
return $helpstr . (sprintf ' (ARG must be %s or %s)%s',
|
518
|
|
|
|
|
|
|
(join ', ', @values[0..$#values-1]), $values[-1], $default_value);
|
519
|
|
|
|
|
|
|
}
|
520
|
|
|
|
|
|
|
}
|
521
|
|
|
|
|
|
|
##------------------------------------------------------------------------------
|
522
|
|
|
|
|
|
|
1;
|
523
|
|
|
|
|
|
|
__END__
|