lib/Getopt/ArgParse/Parser.pm

filter: Statementstmt 96.7% 571 / 590 Branchbran 85.0% 267 / 314 Conditioncond 79.7% 114 / 143 MC/DCmcdc 61.0% 47 / 77 Subroutinesub 97.9% 47 / 48 Podpod 0.0% 0 / 15 totaltotal 88.1% 1046 / 1187 CC 244 SCAR 32.5
CC
244
Cov
91%
add_argument
71.2
_post_parse_processing
27.2
_format_usage_by_spec
20.2
CRAP 25.8
« blib/lib/Getopt/ArgParse/Parser.pm ↑ Summary blib/lib/Getopt/ArgParse/Namespace.pm »
1 package Getopt::ArgParse::Parser;
2  
3 20 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Getopt::ArgParse::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use constant {
13 20 TYPE_UNDEF => 0,
14 TYPE_SCALAR => 1,
15 TYPE_ARRAY => 2,
16 TYPE_COUNT => 3,
17 TYPE_PAIR => 4, # key=value pair
18 TYPE_BOOL => 5,
19  
20 CONST_TRUE => 1,
21 CONST_FALSE => 0,
22  
23 # Export these?
24 ScalarArg => 'scalar',
25 ArrayArg => 'Array',
26 PairArg => 'Pair',
27 CountArg => 'Count',
28 BoolArg => 'Bool',
29  
30 # Internal
31 ERROR_PREFIX => 'Getopt::ArgParse: ',
32 20 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Allow customization
35 # default actions
36 20 my %Action2ClassMap = (
37 '_store' => 'Getopt::ArgParse::ActionStore',
38 '_append' => 'Getopt::ArgParse::ActionAppend',
39 '_count' => 'Getopt::ArgParse::ActionCount',
40 # Not supported - Maybe in the future
41 # '_help' => 'Getopt::ArgParse::ActionHelp',
42 # '_version' => 'Getopt::ArgParse::ActionVersion',
43 );
44  
45 20 my %Type2ConstMap = (
46 '' => TYPE_UNDEF(),
47 'Scalar' => TYPE_SCALAR(),
48 'Array' => TYPE_ARRAY(),
49 'Count' => TYPE_COUNT(),
50 'Pair' => TYPE_PAIR(),
51 'Bool' => TYPE_BOOL(),
52 );
53  
54  
55 sub _croak {
56 38 ▶ die join('', @_);
Subroutine _croak: called CC=1 SCAR=0.0
57 }
58  
59 # Program name. Default $0
60  
61 24 ▶has prog => ( is => 'rw', required => 1, default => sub { File::Basename::basename($0) }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
62  
63 # short one
64 33 ▶has help => ( is => 'rw', required => 1, default => sub { '' }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 # long one
67 32 ▶has description => ( is => 'rw', required => 1, default => sub { '' }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
68  
69 60 ▶has epilog => ( is => 'rw', required => 1, default => sub { '' }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
70  
71 27 ▶has error_prefix => (is => 'rw', default => sub { ERROR_PREFIX() }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
72  
73 33 ▶has aliases => (is => 'ro', default => sub { [] }); # for subcommand only
Subroutine __ANON__: called CC=1 SCAR=0.0
74  
75 # namespace() - Read/write
76 # Contains the parsed results.
77 has namespace => (
78 is => 'rw',
79 isa => sub {
80 48 ▶ return undef unless $_[0]; # allow undef
Subroutine __ANON__: called CC=5 SCAR=18.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 5 43 48
81 43 my $class = blessed $_[0];
82 43 ▶ die 'namespace doesn\'t comform to the required interface'
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class->can("set_attr") and $class->can("get_attr") 0 43 43
Condition33%2/6
$class and $class->can("set_attr") and $class->can("get_attr")
!ll&&!rl&&r
0043
$class and $class->can("set_attr")
!ll&&!rl&&r
0043
MC/DC0%0/3
$class and $class->can("set_attr") and $class->can("get_attr")
$class $class->can("set_attr") $class->can("get_attr")
Truth table25%1/4
A and B and C
A= $classB= $class->can("set_attr")C= $class->can("get_attr")
ABCresult
0XX0
10X0
1100
1111
83 unless $class && $class->can('set_attr') && $class->can('get_attr');
84 },
85 20 );
86  
87 # parent - Readonly
88 has parents => (
89 is => 'ro',
90 isa => sub {
91 62 ▶ my $parents = shift;
Subroutine __ANON__: called CC=4 SCAR=15.2
92 62 for my $parent (@$parents) {
93 5 my $parent_class = blessed $parent;
94 5 ▶ die 'parent is not a Getopt::ArgParse::Parser'
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_class and $parent_class->isa(__PACKAGE__) 0 5 5
Condition33%1/3
$parent_class and $parent_class->isa(__PACKAGE__)
!ll&&!rl&&r
005
MC/DC0%0/2
$parent_class and $parent_class->isa(__PACKAGE__)
$parent_class $parent_class->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $parent_classB= $parent_class->isa(__PACKAGE__)
ABresult
0X0
100
111
95 unless $parent_class && $parent_class->isa(__PACKAGE__);
96 }
97 },
98 32 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 20 );
100  
101 # parser_configs - Read/write
102  
103 # The configurations that will be passed to Getopt::Long::Configure(
104 # $self->parser_configs ) when parse_args is invoked.
105 62 ▶has parser_configs => ( is => 'rw', required => 1, default => sub { [] }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
106  
107 # Behavioural properties
108 #
109 # Print usage message if help is no, by default. Turn this off by
110 # setting this to a false value
111 20 has print_usage_if_help => (is => 'ro', default => 1);
112  
113 # internal properties
114  
115 62 ▶has _option_position => ( is => 'rw', required => 1, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
116  
117 # The current subcommand the same as namespace->current_command
118 20 has _command => ( is => 'rw');
119  
120 sub BUILD {
121 62 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
122  
123 62 $self->{-option_specs} = {};
124 62 $self->{-position_specs} = {};
125  
126 62 $self->add_argument(
127 '--help', '-h',
128 type => 'Bool',
129 dest => 'help',
130 help => 'show this help message and exit',
131 reset => 1,
132 );
133  
134 # merge
135 62 for my $parent (@{$self->parents}) {
136 5 $self->copy($parent);
137 }
138 }
139  
140 #
141 sub _check_parent {
142 18 ▶ my $parent = shift;
Subroutine _check_parent: called CC=3 SCAR=12.5
143 18 my $parent_class = blessed $parent;
144 18 ▶ _croak 'Parent is not a Getopt::ArgParse::Parser'
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_class and $parent_class->isa(__PACKAGE__) 0 18 18
Condition33%1/3
$parent_class and $parent_class->isa(__PACKAGE__)
!ll&&!rl&&r
0018
MC/DC0%0/2
$parent_class and $parent_class->isa(__PACKAGE__)
$parent_class $parent_class->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $parent_classB= $parent_class->isa(__PACKAGE__)
ABresult
0X0
100
111
145 unless $parent_class && $parent_class->isa(__PACKAGE__);
146 }
147  
148 sub copy {
149 5 ▶ my $self = shift;
Subroutine copy: called CC=2 SCAR=7.0
Pod undocumented
150 5 my $parent = shift;
151  
152 5 ▶ _croak 'Parent is missing' unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 5 5
153 5 _check_parent($parent);
154  
155 5 $self->copy_args($parent);
156 5 $self->copy_parsers($parent);
157 }
158  
159 sub copy_args {
160 7 ▶ my $self = shift;
Subroutine copy_args: called CC=2 SCAR=7.0
Pod undocumented
161 7 my $parent = shift;
162  
163 7 ▶ _croak 'Parent is missing' unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 7 7
164 7 _check_parent($parent);
165  
166 7 $self->add_arguments( @{ $parent->{-pristine_add_arguments} } );
167 }
168  
169 sub copy_parsers {
170 6 ▶ my $self = shift;
Subroutine copy_parsers: called CC=5 SCAR=16.1
Pod undocumented
171 6 my $parent = shift;
172  
173 6 ▶ _croak 'Parent is missing' unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 6 6
174 6 _check_parent($parent);
175  
176 6 ▶ if (exists $parent->{-subparsers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parent->{"-subparsers"}) 2 4 6
177 $self->add_subparsers(
178 2 @{$parent->{-pristine_add_subparsers}->[0]}
179 );
180  
181 2 for my $args (@{$parent->{-pristine_add_parser}}) {
182 6 my $command = $args->[0];
183 6 ▶ next if $command eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "help" 2 4 6
184 $self->add_parser(
185 @$args,
186 4 parents => [ $parent->{-subparsers}{-parsers}{$command} ],
187 );
188 }
189 }
190 }
191  
192 #
193 # subcommands
194 #
195 sub add_subparsers {
196 16 ▶ my $self = shift;
Subroutine add_subparsers: called CC=6 SCAR=17.9
Pod undocumented
197  
198 16 push @{$self->{-pristine_add_subparsers}}, [ @_ ];
199  
200 16 ▶ _croak $self->error_prefix . 'Incorrect number of arguments' if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 1 15 16
201  
202 15 my $args = { @_ };
203  
204 15 ▶ my $title = (delete $args->{title} || 'Subcommands') . ':';
Condition100%2/2
delete $args->{'title'} || 'Subcommands'
l!l
87
MC/DC100%1/1
delete $args->{'title'} || 'Subcommands'
delete $args->{'title'}
Truth table100%2/2
A
A= delete $args->{'title'}
Aresult
11
00
205 15 ▶ my $description = delete $args->{description} || '';
Condition100%2/2
delete $args->{'description'} || ''
l!l
213
MC/DC100%1/1
delete $args->{'description'} || ''
delete $args->{'description'}
Truth table100%2/2
A
A= delete $args->{'description'}
Aresult
11
00
206  
207 15 ▶ _croak $self->error_prefix . sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if keys %$args 1 14 15
208 'Unknown parameters: %s',
209 join(',', keys %$args)
210 ) if keys %$args;
211  
212 14 ▶ if (exists $self->{-subparsers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-subparsers"}) 1 13 14
213 1 _croak $self->error_prefix . 'Subparsers already added';
214 }
215  
216 13 $self->{-subparsers}{-title} = $title;
217 13 $self->{-subparsers}{-description} = $description;
218 13 $self->{-subparsers}{-alias_map} = {};
219  
220 13 my $hp = $self->add_parser(
221 'help',
222 help => 'display help information about ' . $self->prog,
223 );
224  
225 13 $hp->add_arguments(
226 [
227 '--all', '-a',
228 help => 'Show the full usage',
229 type => 'Bool',
230 ],
231 [
232 'command',
233 help => 'Show the usage for this command',
234 dest => 'help_command',
235 nargs => 1,
236 ],
237 );
238  
239 13 return $self;
240 }
241  
242 # $command, aliases => [], help => ''
243 sub add_parser {
244 36 ▶ my $self = shift;
Subroutine add_parser: called CC=15 SCAR=27.1
Pod undocumented
245  
246 36 ▶ _croak $self->error_prefix . 'add_subparsers() is not called first' unless $self->{-subparsers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-subparsers"} 1 35 36
247  
248 35 my $command = shift;
249  
250 35 ▶ _croak $self->error_prefix . 'Subcommand is empty' unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 1 34 35
251  
252 34 ▶ _croak $self->error_prefix . 'Incorrect number of arguments' if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 1 33 34
253  
254 33 ▶ if (exists $self->{-subparsers}{-parsers}{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-subparsers"}{"-parsers"}{$command}) 1 32 33
255 1 _croak $self->error_prefix . "Subcommand $command already defined";
256 }
257  
258 32 my $args = { @_ };
259  
260 32 ▶ my $parents = delete $args->{parents} || [];
Condition100%2/2
delete $args->{'parents'} || []
l!l
428
MC/DC100%1/1
delete $args->{'parents'} || []
delete $args->{'parents'}
Truth table100%2/2
A
A= delete $args->{'parents'}
Aresult
11
00
261 32 push @{ $self->{-pristine_add_parser} }, [ $command, %$args ];
262  
263 32 ▶ _croak $self->error_prefix . 'Add_subparsers() is not called first' unless $self->{-subparsers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-subparsers"} 0 32 32
264  
265 32 ▶ my $help = delete $args->{help} || '';
Condition100%2/2
delete $args->{'help'} || ''
l!l
239
MC/DC100%1/1
delete $args->{'help'} || ''
delete $args->{'help'}
Truth table100%2/2
A
A= delete $args->{'help'}
Aresult
11
00
266 32 ▶ my $description = delete $args->{description} || '';
Condition100%2/2
delete $args->{'description'} || ''
l!l
131
MC/DC100%1/1
delete $args->{'description'} || ''
delete $args->{'description'}
Truth table100%2/2
A
A= delete $args->{'description'}
Aresult
11
00
267 32 ▶ my $aliases = delete $args->{aliases} || [];
Condition100%2/2
delete $args->{'aliases'} || []
l!l
527
MC/DC100%1/1
delete $args->{'aliases'} || []
delete $args->{'aliases'}
Truth table100%2/2
A
A= delete $args->{'aliases'}
Aresult
11
00
268  
269 32 ▶ _croak $self->error_prefix . 'Aliases is not an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
if ref $aliases ne "ARRAY" 1 31 32
270 if ref($aliases) ne 'ARRAY';
271  
272 31 ▶ _croak $self->error_prefix . sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if keys %$args 1 30 31
273 'Unknown parameters: %s',
274 join(',', keys %$args)
275 ) if keys %$args;
276  
277 30 my $alias_map = {};
278  
279 30 for my $alias ($command, @$aliases) {
280 35 ▶ if (exists $self->{-subparsers}{-alias_map}{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-subparsers"}{"-alias_map"}{$alias}) 1 34 35
281 _croak $self->error_prefix
282 . "Alias=$alias already used by command="
283 1 . $self->{-subparsers}{-alias_map}{$alias};
284 }
285 }
286  
287 29 $self->{-subparsers}{-alias_map}{$_} = $command for ($command, @$aliases);
288  
289 29 my $prog = $command;
290  
291 # $prog .= ' (' . join(', ', @$aliases) . ')' if @$aliases;
292  
293 29 $self->{-subparsers}{-aliases}{$command} = $aliases;
294 29 return $self->{-subparsers}{-parsers}{$command} = __PACKAGE__->new(
295 prog => $prog,
296 aliases => $aliases, # subcommand
297 help => $help,
298 parents => $parents,
299 description => $description,
300 error_prefix => $self->error_prefix,
301 print_usage_if_help => $self->print_usage_if_help,
302 );
303 }
304  
305 0 ▶sub get_parser { $_[0]->_get_subcommand_parser(@_) }
Subroutine get_parser: not called CC=1 SCAR=6.9
Pod undocumented
306  
307 20 *add_arg = \&add_argument;
308  
309 20 *add_args = \&add_arguments;
310  
311 # add_arguments([arg_spec], [arg_spec1], ...)
312 # Add multiple arguments.
313 # Interface method
314 sub add_arguments {
315 22 ▶ my $self = shift;
Subroutine add_arguments: called CC=2 SCAR=6.9
Pod undocumented
316  
317 22 $self->add_argument(@$_) for @_;
318  
319 22 return $self;
320 }
321  
322 #
323 sub add_argument {
324 201 ▶ my $self = shift;
Subroutine add_argument: called CC=67 SCAR=42.7
Pod undocumented
325  
326 201 ▶ return unless @_; # mostly harmless
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 200 201
327 #
328 # FIXME: This is for merginng parent parents This is a dirty hack
329 # and should be done properly by merging internal specs
330 # and subcommand merging is missing
331 #
332 200 push @{ $self->{-pristine_add_arguments} }, [ @_ ];
333  
334 200 my ($name, $flags, $rest) = $self->_parse_for_name_and_flags([ @_ ]);
335  
336 200 ▶ _croak $self->error_prefix . 'Incorrect number of arguments' if scalar(@$rest) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@$rest) % 2 1 199 200
337  
338 199 ▶ _croak $self->error_prefix . 'Empty option name' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 198 199
339  
340 198 my $args = { @$rest };
341  
342 198 my @flags = @{ $flags };
343  
344 ################
345 # nargs - positional only
346 ################
347 ################
348 # type
349 ################
350 198 ▶ my $type_name = delete $args->{type} || 'Scalar';
Condition100%2/2
delete $args->{'type'} || 'Scalar'
l!l
13959
MC/DC100%1/1
delete $args->{'type'} || 'Scalar'
delete $args->{'type'}
Truth table100%2/2
A
A= delete $args->{'type'}
Aresult
11
00
351 198 ▶ my $type = $Type2ConstMap{$type_name} if exists $Type2ConstMap{$type_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Type2ConstMap{$type_name} 197 1 198
352 198 ▶ _croak $self->error_prefix . "Unknown type=$type_name" unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 197 198
353  
354 197 my $nargs = delete $args->{nargs};
355  
356 197 ▶ if ( defined $nargs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nargs) 28 169 197
357 28 ▶ _croak $self->error_prefix . 'Nargs only allowed for positional options' if @flags;
Branch: 1 branch
BranchTrueFalseTotal
if @flags 1 27 28
358  
359 27 ▶ if ( $type != TYPE_PAIR
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 4 and $type != 2 and $nargs ne 1 and $nargs ne "?") 3 24 27
Condition100%9/9
$type != 4 and $type != 2 and $nargs ne 1 and $nargs ne "?"
!ll&&!rl&&r
2313
$type != 4 and $type != 2 and $nargs ne 1
!ll&&!rl&&r
10134
$type != 4 and $type != 2
!ll&&!rl&&r
3717
MC/DC100%4/4
$type != 4 and $type != 2 and $nargs ne 1 and $nargs ne "?"
$type != 4 $type != 2 $nargs ne 1 $nargs ne "?"
Truth table100%5/5
A and B and C and D
A= $type != 4B= $type != 2C= $nargs ne 1D= $nargs ne "?"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
360 && $type != TYPE_ARRAY
361 && $nargs ne '1'
362 && $nargs ne '?'
363 ) {
364 3 $type = TYPE_ARRAY;
365 }
366 }
367  
368 196 ▶ if ($type == TYPE_COUNT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 3) { } 10 186 196
elsif ($type == 2 or $type == 4) { } 23 163 186
369 10 ▶ $args->{action} = '_count' unless defined $args->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"action"} 10 0 10
370 10 ▶ $args->{default} = 0 unless defined $args->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"default"} 9 1 10
371 ▶ } elsif ($type == TYPE_ARRAY || $type == TYPE_PAIR) {
Condition100%3/3
$type == 2 or $type == 4
l!l&&r!l&&!r
158163
MC/DC100%2/2
$type == 2 or $type == 4
$type == 2 $type == 4
Truth table100%3/3
A or B
A= $type == 2B= $type == 4
ABresult
1X1
011
000
372 23 ▶ $args->{action} = '_append' unless defined $args->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"action"} 23 0 23
373 } else {
374 # pass
375 }
376  
377 ################
378 # action
379 ################
380 196 ▶ my $action_name = delete $args->{action} || '_store';
Condition100%2/2
delete $args->{'action'} || '_store'
l!l
33163
MC/DC100%1/1
delete $args->{'action'} || '_store'
delete $args->{'action'}
Truth table100%2/2
A
A= delete $args->{'action'}
Aresult
11
00
381  
382 my $action = $Action2ClassMap{$action_name}
383 196 ▶ if exists $Action2ClassMap{$action_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Action2ClassMap{$action_name} 196 0 196
384  
385 196 ▶ $action = $action_name unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 196 196
386  
387 {
388 196 local $SIG{__WARN__};
389 196 local $SIG{__DIE__};
390  
391 196 eval "require $action";
392  
393 196 ▶ _croak $self->error_prefix . "Cannot load $action for action=$action_name" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 196 196
394 };
395  
396 ################
397 # split
398 ################
399 196 my $split = delete $args->{split};
400 196 ▶ if (defined $split && !$split && $split =~ /^ +$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $split and not $split and $split =~ /^ +$/) 0 196 196
Condition50%3/6
defined $split and not $split and $split =~ /^ +$/
!ll&&!rl&&r
19600
defined $split and not $split
!ll&&!rl&&r
19330
MC/DC0%0/3
defined $split and not $split and $split =~ /^ +$/
defined $split not $split $split =~ /^ +$/
Truth table50%2/4
A and not(B) and C
A= defined $splitB= not $splitC= $split =~ /^ +$/
ABCresult
0XX0
10X0
1100
1111
401 0 _croak $self->error_prefix . 'Cannot use whitespaces to split';
402 }
403  
404 196 ▶ if (defined $split && $type != TYPE_ARRAY && $type != TYPE_PAIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $split and $type != 2 and $type != 4) 0 196 196
Condition83%5/6
defined $split and $type != 2 and $type != 4
!ll&&!rl&&r
19510
defined $split and $type != 2
!ll&&!rl&&r
19321
MC/DC0%0/3
defined $split and $type != 2 and $type != 4
defined $split $type != 2 $type != 4
Truth table75%3/4
A and B and C
A= defined $splitB= $type != 2C= $type != 4
ABCresult
0XX0
10X0
1100
1111
405 0 _croak $self->error_prefix . 'Split only for Array and Pair';
406 }
407  
408 ################
409 # default
410 ################
411 196 my $default;
412 196 ▶ if (exists $args->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"default"}) 20 176 196
413 20 my $val = delete $args->{default};
414  
415 20 ▶ if (ref($val) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 2 18 20
elsif (ref $val eq 'HASH') { } 5 13 18
416 2 $default = $val;
417 } elsif (ref($val) eq 'HASH') {
418 5 ▶ _croak $self->error_prefix . 'HASH default only for type Pair'
Branch: 1 branch
BranchTrueFalseTotal
if $type != 4 1 4 5
419 if $type != TYPE_PAIR;
420 4 $default = $val;
421 } else {
422 13 $default = [ $val ];
423 }
424  
425 19 ▶ if ($type != TYPE_PAIR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 4) 15 4 19
426 15 ▶ if ($type != TYPE_ARRAY && scalar(@$default) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 2 and scalar @$default > 1) 1 14 15
Condition100%3/3
$type != 2 and scalar @$default > 1
!ll&&!rl&&r
1131
MC/DC100%2/2
$type != 2 and scalar @$default > 1
$type != 2 scalar @$default > 1
Truth table100%3/3
A and B
A= $type != 2B= scalar @$default > 1
ABresult
0X0
100
111
427 1 _croak $self->error_prefix . 'Multiple default values for scalar type: $name';
428 }
429 }
430 }
431  
432 ################
433 # choices
434 ################
435 194 ▶ my $choices = delete $args->{choices} || undef;
Condition100%2/2
delete $args->{'choices'} || undef
l!l
3191
MC/DC100%1/1
delete $args->{'choices'} || undef
delete $args->{'choices'}
Truth table100%2/2
A
A= delete $args->{'choices'}
Aresult
11
00
436 194 ▶ if ( $choices
Branch: 1 branch
BranchTrueFalseTotal
if ($choices and ref $choices ne "CODE" and ref $choices ne "ARRAY") 0 194 194
Condition83%5/6
$choices and ref $choices ne "CODE" and ref $choices ne "ARRAY"
!ll&&!rl&&r
19220
$choices and ref $choices ne "CODE"
!ll&&!rl&&r
19112
MC/DC0%0/3
$choices and ref $choices ne "CODE" and ref $choices ne "ARRAY"
$choices ref $choices ne "CODE" ref $choices ne "ARRAY"
Truth table75%3/4
A and B and C
A= $choicesB= ref $choices ne "CODE"C= ref $choices ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
437 && ref($choices) ne 'CODE'
438 && ref($choices) ne 'ARRAY' )
439 {
440 0 _croak $self->error_prefix . "Must provide choices in an arrayref or a coderef";
441 }
442  
443 194 ▶ my $choices_i = delete $args->{choices_i} || undef;
Condition100%2/2
delete $args->{'choices_i'} || undef
l!l
3191
MC/DC100%1/1
delete $args->{'choices_i'} || undef
delete $args->{'choices_i'}
Truth table100%2/2
A
A= delete $args->{'choices_i'}
Aresult
11
00
444  
445 194 ▶ if ($choices && $choices_i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($choices and $choices_i) 1 193 194
Condition100%3/3
$choices and $choices_i
!ll&&!rl&&r
19121
MC/DC100%2/2
$choices and $choices_i
$choices $choices_i
Truth table100%3/3
A and B
A= $choicesB= $choices_i
ABresult
0X0
100
111
446 1 _croak $self->error_prefix . 'Not allow to specify choices and choices_i';
447 }
448  
449 193 ▶ if ( $choices_i
Branch: 1 branch
BranchTrueFalseTotal
if ($choices_i and ref $choices_i ne "ARRAY") 1 192 193
Condition100%3/3
$choices_i and ref $choices_i ne "ARRAY"
!ll&&!rl&&r
19111
MC/DC100%2/2
$choices_i and ref $choices_i ne "ARRAY"
$choices_i ref $choices_i ne "ARRAY"
Truth table100%3/3
A and B
A= $choices_iB= ref $choices_i ne "ARRAY"
ABresult
0X0
100
111
450 && ref($choices_i) ne 'ARRAY' )
451 {
452 1 _croak $self->error_prefix . "Must provide choices_i in an arrayref";
453 }
454  
455 ################
456 # required
457 ################
458 192 ▶ my $required = delete $args->{required} || '';
Condition100%2/2
delete $args->{'required'} || ''
l!l
15177
MC/DC100%1/1
delete $args->{'required'} || ''
delete $args->{'required'}
Truth table100%2/2
A
A= delete $args->{'required'}
Aresult
11
00
459  
460 192 ▶ if ($type == TYPE_BOOL || $type == TYPE_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 5 or $type == 3) 117 75 192
Condition100%3/3
$type == 5 or $type == 3
l!l&&r!l&&!r
1071075
MC/DC100%2/2
$type == 5 or $type == 3
$type == 5 $type == 3
Truth table100%3/3
A or B
A= $type == 5B= $type == 3
ABresult
1X1
011
000
461 117 $required = ''; # TYPE_BOOL and TYPE_COUNT will already have default values
462 }
463  
464 ################
465 # help
466 ################
467 192 ▶ my $help = delete $args->{help} || '';
Condition100%2/2
delete $args->{'help'} || ''
l!l
10884
MC/DC100%1/1
delete $args->{'help'} || ''
delete $args->{'help'}
Truth table100%2/2
A
A= delete $args->{'help'}
Aresult
11
00
468  
469 ################
470 # metavar
471 ################
472 192 ▶ my $metavar = delete $args->{metavar} || uc($name);
Condition33%1/3
delete $args->{'metavar'} || uc $name
l!l&&r!l&&!r
01920
MC/DC0%0/2
delete $args->{'metavar'} || uc $name
delete $args->{'metavar'} uc $name
Truth table33%1/3
A || B
A= delete $args->{'metavar'}B= uc $name
ABresult
1X1
011
000
473  
474 192 ▶ $metavar = ''
Branch: 1 branch
BranchTrueFalseTotal
if $type == 5 or $action_name eq "_count" 117 75 192
Condition100%3/3
$type == 5 or $action_name eq "_count"
l!l&&r!l&&!r
1071075
MC/DC100%2/2
$type == 5 or $action_name eq "_count"
$type == 5 $action_name eq "_count"
Truth table100%3/3
A or B
A= $type == 5B= $action_name eq "_count"
ABresult
1X1
011
000
475 if $type == TYPE_BOOL
476 || $action_name eq '_count';
477  
478 ################
479 # dest
480 ################
481 192 ▶ my $dest = delete $args->{dest} || $name;
Condition66%2/3
delete $args->{'dest'} || $name
l!l&&r!l&&!r
891030
MC/DC0%0/2
delete $args->{'dest'} || $name
delete $args->{'dest'} $name
Truth table66%2/3
A || B
A= delete $args->{'dest'}B= $name
ABresult
1X1
011
000
482 192 $dest =~ s/-/_/g; # option-name becomes option_name
483  
484 192 ▶ if (@flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flags) { } 152 40 192
485 152 ▶ while (my ($d, $s) = each %{$self->{-option_specs}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"-option_specs"};}) 139 151 290
486 139 ▶ if ($dest ne $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest ne $d) 123 16 139
487 123 for my $f (@flags) {
488 _croak $self->error_prefix . "Flag $f already used for a different option ($d)"
489 173 ▶ if grep { $f eq $_ } @{$s->{flags}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$f eq $_;} @{$$s{"flags"};} 1 172 173
490 }
491 }
492 }
493  
494 151 ▶ if (exists $self->{-position_specs}{$dest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-position_specs"}{$dest}) 0 151 151
495 0 _croak $self->error_prefix . "Option dest=$dest already used by a positional argument";
496 }
497 } else {
498 40 ▶ if (exists $self->{-option_specs}{$dest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-option_specs"}{$dest}) 1 39 40
499 1 _croak $self->error_prefix . "Option dest=$dest already used by an optional argument";
500 }
501 }
502  
503 # never modify existing ones so that the parent's structure will
504 # not be modified
505 my $spec = {
506 name => $name,
507 flags => \@flags,
508 action => $action,
509 nargs => $nargs,
510 split => $split,
511 required => $required || '',
512 type => $type,
513 default => $default,
514 choices => $choices,
515 choices_i => $choices_i,
516 dest => $dest,
517 metavar => $metavar,
518 help => $help,
519 190 ▶ position => $self->{-option_position}++, # sort order
Condition100%2/2
$required || ''
l!l
13177
MC/DC100%1/1
$required || ''
$required
Truth table100%2/2
A
A= $required
Aresult
11
00
520 groups => [ '' ],
521 };
522  
523 190 my $specs;
524 190 ▶ if (@flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flags) { } 151 39 190
525 151 $specs = $self->{-option_specs};
526 } else {
527 39 $specs = $self->{-position_specs};
528 }
529  
530 # reset
531 190 ▶ if (delete $args->{reset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args->{"reset"}) 81 109 190
532 81 ▶ $self->namespace->set_attr($spec->{dest}, undef) if $self->namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->namespace 6 75 81
533 81 delete $specs->{$spec->{dest}};
534 }
535  
536 190 ▶ _croak $self->error_prefix . sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if keys %$args 0 190 190
537 'Unknown spec: %s',
538 join(',', keys %$args)
539 ) if keys %$args;
540  
541 # type check
542 190 ▶ if (exists $specs->{$spec->{dest}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$specs{$spec->{"dest"}}) 3 187 190
543 _croak $self->error_prefix . sprintf(
544 'Redefine option %s without reset',
545 $spec->{dest},
546 3 );
547 }
548  
549 # override
550 187 $specs->{$spec->{dest}} = $spec;
551  
552 # specs changed, need to force to resort specs by groups
553 187 ▶ delete $self->{-groups} if $self->{-groups};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-groups"} 0 187 187
554  
555 # Return $self for chaining, $self->add_argument()->add_argument()
556 # or use add_arguments
557 187 return $self;
558 }
559  
560 sub _parse_for_name_and_flags {
561 200 ▶ my $self = shift;
Subroutine _parse_for_name_and_flags: called CC=4 SCAR=13.9
562 200 my $args = shift;
563  
564 200 my ($name, @flags);
565 FLAG:
566 200 ▶ while (my $flag = shift @$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flag = shift @$args) 458 14 472
567 458 ▶ if (substr($flag, 0, 1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($flag, 0, 1) eq '-') { } 272 186 458
568 272 push @flags, $flag;
569 } else {
570 186 unshift @$args, $flag;
571 186 last FLAG;
572 }
573 }
574  
575 # It's a positional argument spec if there are no flags
576 200 ▶ $name = @flags ? $flags[0] : shift(@$args);
Branch: 1 branch
BranchTrueFalseTotal
@flags ? : 159 41 200
577 200 $name =~ s/^-+//g;
578  
579 200 return ( $name, \@flags, $args );
580 }
581  
582 #
583 # parse_args([@_])
584 #
585 # Parse @ARGV if called without passing arguments. It returns an
586 # instance of ArgParse::Namespace upon success
587 #
588 # Interface
589  
590 sub parse_args {
591 82 ▶ my $self = shift;
Subroutine parse_args: called CC=16 SCAR=27.7
Pod undocumented
592  
593 82 ▶ my @argv = scalar(@_) ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 80 2 82
594  
595 82 $self->{-saved_argv} = \@ARGV;
596 82 @ARGV = ();
597  
598 my @option_specs = sort {
599 $a->{position} <=> $b->{position}
600 82 } values %{$self->{-option_specs}};
601  
602 my @position_specs = sort {
603 $a->{position} <=> $b->{position}
604 82 } values %{$self->{-position_specs}};
605  
606 82 $self->{-argv} = \@argv;
607 # We still want to continue even if @argv is empty to allow:
608 # - namespace initialization
609 # - default values asssigned
610 # - post checks applied, e.g. required check
611  
612 82 ▶ $self->namespace(Getopt::ArgParse::Namespace->new) unless $self->namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->namespace 32 50 82
613  
614 82 my $parsed_subcmd;
615 82 $self->namespace->set_attr(current_command => undef);
616  
617 # If the first argument is a subcommand, it will parse for the
618 # subcommand
619 82 ▶ if (exists $self->{-subparsers} && scalar(@argv) && defined($argv[0]) && substr($argv[0], 0, 1) ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-subparsers"} and scalar @argv and defined $argv[0] and substr($argv[0], 0, 1) ne "-") 11 71 82
Condition87%7/8
exists $self->{"-subparsers"} and scalar @argv and defined $argv[0] and substr($argv[0], 0, 1) ne "-"
!ll&&!rl&&r
67411
exists $self->{"-subparsers"} and scalar @argv and defined $argv[0]
!ll&&!rl&&r
67015
exists $self->{"-subparsers"} and scalar @argv
!ll
6715
MC/DC66%2/3
exists $self->{"-subparsers"} and scalar @argv and defined $argv[0] and substr($argv[0], 0, 1) ne "-"
exists $self->{"-subparsers"} defined $argv[0] substr($argv[0], 0, 1) ne "-"
Truth table75%3/4
A and B and C
A= exists $self->{"-subparsers"}B= defined $argv[0]C= substr($argv[0], 0, 1) ne "-"
ABCresult
0XX0
10X0
1100
1111
620 # Subcommand must appear as the first argument
621 # or it will parse as the top command
622 11 my $cmd = shift @argv;
623 11 my $subparser = $self->_get_subcommand_parser($cmd);
624 11 ▶ _croak $self->error_prefix
Branch: 1 branch
BranchTrueFalseTotal
unless $subparser 1 10 11
625 . sprintf("%s is not a %s command. See help", $cmd, $self->prog)
626 unless $subparser;
627  
628 10 $parsed_subcmd = $self->_parse_subcommand($self->_command => $subparser);
629  
630 10 $self->namespace->set_attr(current_command => $self->_command);
631 }
632  
633 81 ▶ if (!$parsed_subcmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed_subcmd) { } 71 10 81
634 71 ▶ $self->_parse_optional_args(\@option_specs) if @option_specs;
Branch: 1 branch
BranchTrueFalseTotal
if @option_specs 71 0 71
635 64 ▶ $self->_parse_positional_args(\@position_specs) if @position_specs;
Branch: 1 branch
BranchTrueFalseTotal
if @position_specs 32 32 64
636  
637 56 ▶ if ($self->print_usage_if_help() && $self->namespace->get_attr('help')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->print_usage_if_help and $self->namespace->get_attr("help")) 1 55 56
Condition100%3/3
$self->print_usage_if_help and $self->namespace->get_attr("help")
!ll&&!rl&&r
4511
MC/DC100%2/2
$self->print_usage_if_help and $self->namespace->get_attr("help")
$self->print_usage_if_help $self->namespace->get_attr("help")
Truth table100%3/3
A and B
A= $self->print_usage_if_helpB= $self->namespace->get_attr("help")
ABresult
0X0
100
111
638 1 $self->print_usage();
639 1 exit(0);
640 }
641 } else {
642 10 ▶ if ($self->print_usage_if_help() && $self->_command() eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->print_usage_if_help and $self->_command eq "help") 2 8 10
Condition100%3/3
$self->print_usage_if_help and $self->_command eq "help"
!ll&&!rl&&r
172
MC/DC100%2/2
$self->print_usage_if_help and $self->_command eq "help"
$self->print_usage_if_help $self->_command eq "help"
Truth table100%3/3
A and B
A= $self->print_usage_if_helpB= $self->_command eq "help"
ABresult
0X0
100
111
643 2 ▶ if ($self->namespace->get_attr('help_command')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->namespace->get_attr('help_command')) { } 1 1 2
644 1 $self->print_command_usage();
645 1 exit(0);
646 } else {
647 1 $self->print_usage();
648 1 exit(0);
649 }
650 }
651 }
652  
653 # Return value
654 63 return $self->namespace;
655 }
656  
657 sub _get_subcommand_parser {
658 13 ▶ my $self = shift;
Subroutine _get_subcommand_parser: called CC=4 SCAR=13.9
659 13 my $alias = shift;
660  
661 13 ▶ return unless $alias;
Branch: 1 branch
BranchTrueFalseTotal
unless $alias 0 13 13
662  
663 my $command = $self->{-subparsers}{-alias_map}{$alias}
664 13 ▶ if exists $self->{-subparsers}{-alias_map}{$alias};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"-subparsers"}{"-alias_map"}{$alias} 12 1 13
665  
666 13 ▶ return unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 1 12 13
667  
668 12 $self->_command($command);
669 # The subcommand parser must exist if the alias is mapped
670 12 return $self->{-subparsers}{-parsers}{$command};
671 }
672  
673 sub _parse_subcommand {
674 10 ▶ my $self = shift;
Subroutine _parse_subcommand: called CC=1 SCAR=0.0
675 10 my ($cmd, $subparser) = @_;
676  
677 10 $subparser->namespace($self->namespace);
678 10 $subparser->parse_args(@{$self->{-argv}});
679  
680 10 $self->{-argv} = $subparser->{-argv};
681  
682 10 return 1;
683 }
684  
685 #
686 # After each call of parse_args(), call this to retrieve any
687 # unconsumed arguments
688 # Interface call
689 #
690 sub argv {
691 1 ▶ @{ $_[0]->{-argv} || [] };
Subroutine argv: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"-argv"} or [] 0 1 1
692 }
693  
694 sub _parse_optional_args {
695 71 ▶ my $self = shift;
Subroutine _parse_optional_args: called CC=9 SCAR=22.0
696 71 my $specs = shift;
697 71 my $options = {};
698 71 my $dest2spec = {};
699  
700 71 for my $spec ( @$specs ) {
701 158 my @values;
702 158 $dest2spec->{$spec->{dest}} = $self->_get_option_spec($spec);
703 158 ▶ if ( $spec->{type} == TYPE_ARRAY
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'type'} == 2 or $spec->{'type'} == 3 or $spec->{'type'} == 4 or $spec->{'type'} == 1) { } 66 92 158
Condition100%9/9
$spec->{"type"} == 2 or $spec->{"type"} == 3 or $spec->{"type"} == 4 or $spec->{"type"} == 1
l!l&&r!l&&!r
184892
$spec->{"type"} == 2 or $spec->{"type"} == 3 or $spec->{"type"} == 4
l!l&&r!l&&!r
144140
$spec->{"type"} == 2 or $spec->{"type"} == 3
l!l&&r!l&&!r
104144
MC/DC100%4/4
$spec->{"type"} == 2 or $spec->{"type"} == 3 or $spec->{"type"} == 4 or $spec->{"type"} == 1
$spec->{"type"} == 2 $spec->{"type"} == 3 $spec->{"type"} == 4 $spec->{"type"} == 1
Truth table100%5/5
A or B or C or D
A= $spec->{"type"} == 2B= $spec->{"type"} == 3C= $spec->{"type"} == 4D= $spec->{"type"} == 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
704 || $spec->{type} == TYPE_COUNT
705 || $spec->{type} == TYPE_PAIR
706 || $spec->{type} == TYPE_SCALAR
707 ) {
708 66 my @values;
709 66 $options->{ $dest2spec->{$spec->{dest}} } = \@values;
710 } else {
711 92 my $value;
712 92 $options->{ $dest2spec->{$spec->{dest}} } = \$value;
713 }
714 }
715  
716 71 Getopt::Long::Configure( @{ $self->parser_configs });
717  
718 71 my (@warns, $result);
719  
720 71 eval {
721 71 ▶ local $SIG{__WARN__} = sub { push @warns, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
722 71 local $SIG{__DIE__};
723  
724 71 $result = GetOptionsFromArray( $self->{-argv}, %$options );
725  
726 71 1;
727 };
728  
729 # die on errors
730 71 ▶ _croak $self->error_prefix, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 71 71
731  
732 71 ▶ _croak $self->error_prefix, @warns if @warns;
Branch: 1 branch
BranchTrueFalseTotal
if @warns 2 69 71
733  
734 69 ▶ _croak $self->error_prefix, 'Failed to parse for options' if !$result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 69 69
735  
736 69 Getopt::Long::Configure('default');
737  
738 69 $self->_post_parse_processing($specs, $options, $dest2spec);
739  
740 66 $self->_apply_action($specs, $options, $dest2spec);
741  
742 65 $self->_post_apply_processing($specs, $options, $dest2spec);
743 }
744  
745 sub _parse_positional_args {
746 32 ▶ my $self = shift;
Subroutine _parse_positional_args: called CC=15 SCAR=27.1
747 32 my $specs = shift;
748  
749 # short-circuit it if it's for help
750 32 ▶ return if $self->namespace->get_attr('help');
Branch: 1 branch
BranchTrueFalseTotal
if $self->namespace->get_attr("help") 1 31 32
751  
752 31 my $options = {};
753 31 my $dest2spec = {};
754  
755 31 for my $spec (@$specs) {
756 53 $dest2spec->{$spec->{dest}} = $spec->{dest};
757 53 my @values = ();
758 # Always assigne values to an option
759 53 $options->{$spec->{dest}} = \@values;
760 }
761  
762 POSITION_SPEC:
763 31 for my $spec (@$specs) {
764 53 my $values = $options->{$spec->{dest}};
765  
766 53 ▶ if ($spec->{type} == TYPE_BOOL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"type"} == 5) 0 53 53
767 0 _croak $self->error_prefix . 'Bool not allowed for positional arguments';
768 }
769  
770 53 my $number = 1;
771 53 ▶ my $nargs = defined $spec->{nargs} ? $spec->{nargs} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $spec->{'nargs'} ? : 32 21 53
772 53 ▶ if (defined $spec->{nargs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"nargs"}) 32 21 53
773 32 ▶ if ($nargs eq '?') {
Branch: 4 branches
BranchTrueFalseTotal
if ($nargs eq '?') { } 5 27 32
elsif ($nargs eq '+') { } 3 24 27
elsif ($nargs eq '*') { } 3 21 24
elsif (not $nargs =~ /^\d+$/) { } 1 20 21
774 5 $number = 1;
775 } elsif ($nargs eq '+') {
776 3 ▶ _croak $self->error_prefix . "Too few arguments: narg='+'" unless @{$self->{-argv}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"-argv"};} 1 2 3
777 2 $number = scalar @{$self->{-argv}};
778 } elsif ($nargs eq '*') { # remainder
779 3 $number = scalar @{$self->{-argv}};
780 } elsif ($nargs !~ /^\d+$/) {
781 1 _croak $self->error_prefix . 'Invalid nargs:' . $nargs;
782 } else {
783 20 $number = $nargs;
784 }
785 }
786  
787 51 ▶ push @$values, splice(@{$self->{-argv}}, 0, $number) if @{$self->{-argv}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"-argv"};} 37 14 51
788  
789 # If no values, let it pass for required checking
790 # If there are values, make sure there is the right number of
791 # values
792 51 ▶ if (scalar(@$values) && scalar(@$values) != $number) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$values and scalar @$values != $number) 3 48 51
Condition100%3/3
scalar @$values and scalar @$values != $number
!ll&&!rl&&r
14343
MC/DC100%2/2
scalar @$values and scalar @$values != $number
scalar @$values scalar @$values != $number
Truth table100%3/3
A and B
A= scalar @$valuesB= scalar @$values != $number
ABresult
0X0
100
111
793 _croak($self->error_prefix . sprintf(
794 'Too few arguments for %s: expected:%d,actual:%d',
795 3 $spec->{dest}, $number, scalar(@$values),
796 )
797 );
798 }
799 }
800  
801 26 $self->_post_parse_processing($specs, $options, $dest2spec);
802  
803 26 $self->_apply_action($specs, $options, $dest2spec);
804  
805 26 $self->_post_apply_processing($specs, $options, $dest2spec);
806 }
807  
808 #
809 sub _post_parse_processing {
810 95 ▶ my $self = shift;
Subroutine _post_parse_processing: called CC=27 SCAR=33.0
811 95 my ($option_specs, $options, $dest2spec) = @_;
812  
813 #
814 95 for my $spec ( @$option_specs ) {
815 198 my $values = $options->{ $dest2spec->{$spec->{dest}} };
816  
817 198 ▶ if (defined($values)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values) { } 198 0 198
818 198 ▶ if (ref $values eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $values eq "SCALAR") 90 108 198
819 90 ▶ if (defined($$values)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$values) { } 11 79 90
820 11 $values = [ $$values ];
821 } else {
822 79 $values = [];
823 }
824 }
825 } else {
826 0 $values = [];
827 }
828  
829 198 $options->{ $dest2spec->{$spec->{dest}} } = $values;
830  
831 # default
832 198 ▶ if (!defined($self->namespace->get_attr($spec->{dest}))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->namespace->get_attr($spec->{"dest"}) and scalar @$values < 1 and defined $spec->{"default"}) 8 190 198
Condition100%6/6
not defined $self->namespace->get_attr($spec->{"dest"}) and scalar @$values < 1 and defined $spec->{"default"}
!ll&&!rl&&r
135558
not defined $self->namespace->get_attr($spec->{"dest"}) and scalar @$values < 1
!ll&&!rl&&r
765963
MC/DC100%3/3
not defined $self->namespace->get_attr($spec->{"dest"}) and scalar @$values < 1 and defined $spec->{"default"}
not defined $self->namespace->get_attr($spec->{"dest"}) scalar @$values < 1 defined $spec->{"default"}
Truth table100%4/4
not(A) and B and C
A= not defined $self->namespace->get_attr($spec->{"dest"})B= scalar @$values < 1C= defined $spec->{"default"}
ABCresult
0XX0
10X0
1100
1111
833 && scalar(@$values) < 1
834 && defined($spec->{default}) )
835 {
836 8 ▶ if ($spec->{type} == TYPE_COUNT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spec->{'type'} == 3) { } 1 7 8
elsif ($spec->{'type'} == 5) { } 1 6 7
elsif ($spec->{'type'} == 4) { } 2 4 6
837 1 $self->namespace->set_attr($spec->{dest}, @{$spec->{default}});
838 } elsif ($spec->{type} == TYPE_BOOL) {
839 1 $self->namespace->set_attr($spec->{dest}, @{$spec->{default}});
840 } elsif ($spec->{type} == TYPE_PAIR) {
841 2 $self->namespace->set_attr($spec->{dest}, $spec->{default});
842 } else {
843 4 push @$values, @{$spec->{default}};
844 }
845 }
846  
847 # split and expand
848 # Pair are processed here as well
849 198 ▶ if ( my $delimit = $spec->{split} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delimit = $spec->{'split'}) { } 5 193 198
850 5 my @expanded;
851 5 for my $v (@$values) {
852 push @expanded,
853 map {
854 4 ▶ $spec->{type} == TYPE_PAIR ? { split('=', $_) } : $_
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'type'} == 4 ? : 3 9 12
855 } split($delimit, $v);
856 }
857  
858 5 $options->{ $dest2spec->{$spec->{dest} } } = \@expanded;
859 } else {
860 # Process PAIR only
861 193 ▶ if ($spec->{type} == TYPE_PAIR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"type"} == 4) 5 188 193
862 $options->{ $dest2spec->{$spec->{dest} } }
863 5 = [ map { { split('=', $_) } } @$values ];
864 }
865 }
866  
867 # choices
868 198 ▶ if ( $spec->{choices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"choices"}) 5 193 198
869 5 ▶ if (ref($spec->{choices}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec->{'choices'} eq 'CODE') { } 2 3 5
870 2 for my $v (@$values) {
871 2 $spec->{choices}->($v);
872 }
873 } else {
874 my %choices =
875 9 ▶ map { defined($_) ? $_ : '_undef' => 1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 9 0 9
876 3 @{$spec->{choices}};
877  
878 VALUE:
879 3 for my $v (@$values) {
880 2 ▶ my $k = defined($v) ? $v : '_undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
881 2 ▶ next VALUE if exists $choices{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $choices{$k} 1 1 2
882  
883 _croak $self->error_prefix . sprintf(
884 "Option %s value %s not in choices: [ %s ]",
885 1 $spec->{dest}, $v, join( ', ', @{ $spec->{choices} } ),
886 );
887 }
888 }
889 }
890  
891 196 ▶ if ( $spec->{choices_i} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"choices_i"}) 3 193 196
892 my %choices =
893 6 ▶ map { defined($_) ? uc($_) : '_undef' => 1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 6 0 6
894 3 @{$spec->{choices_i}};
895  
896 VALUE:
897 3 for my $v (@$values) {
898 3 ▶ my $k = defined($v) ? uc($v) : '_undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 3 0 3
899 3 ▶ next VALUE if exists $choices{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $choices{$k} 2 1 3
900  
901 _croak $self->error_prefix . sprintf(
902 "Option %s value %s not in choices: [ %s ] (case insensitive)",
903 1 $spec->{dest}, $v, join( ', ', @{ $spec->{choices_i} } ),
904 );
905 }
906 }
907 }
908  
909 92 return '';
910 }
911  
912 sub _apply_action {
913 92 ▶ my $self = shift;
Subroutine _apply_action: called CC=4 SCAR=13.9
914 92 my ($specs, $options, $dest2spec) = @_;
915  
916 92 for my $spec (@$specs) {
917 # Init
918 # We want to preserve already set attributes if the namespace
919 # is passed in.
920 #
921 # This is because one may want to load configs from a file
922 # into a namespace and then use the same namespace for parsing
923 # configs from command line.
924 #
925 $self->namespace->set_attr($spec->{dest}, undef)
926 189 ▶ unless defined($self->namespace->get_attr($spec->{dest}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->namespace->get_attr($spec->{"dest"}) 111 78 189
927  
928 my $error = $spec->{action}->apply(
929 $spec,
930 $self->namespace,
931 $options->{ $dest2spec->{$spec->{dest}} },
932 $spec->{name},
933 189 );
934  
935 189 ▶ _croak $self->error_prefix . $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 188 189
936 }
937  
938 91 return '';
939 }
940  
941 sub _post_apply_processing {
942 91 ▶ my $self = shift;
Subroutine _post_apply_processing: called CC=7 SCAR=19.5
943 91 my ($specs, $options, $dest2spec) = @_;
944  
945 #
946 # required is checked after applying actions
947 # This is because required checking is bypassed if help is on
948 #
949 91 for my $spec (@$specs) {
950 186 my $v = $self->namespace->get_attr($spec->{dest});
951  
952 # required
953 186 ▶ if ( $spec->{required} && not $self->namespace->get_attr('help') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"required"} and not $self->namespace->get_attr("help")) 15 171 186
Condition100%3/3
$spec->{"required"} and not $self->namespace->get_attr("help")
!ll&&!rl&&r
169215
MC/DC100%2/2
$spec->{"required"} and not $self->namespace->get_attr("help")
$spec->{"required"} not $self->namespace->get_attr("help")
Truth table100%3/3
A and not(B)
A= $spec->{"required"}B= not $self->namespace->get_attr("help")
ABresult
0X0
100
111
954 15 my $has_v;
955 15 ▶ if ($spec->{type} == TYPE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'type'} == 2) { } 6 9 15
elsif ($spec->{'type'} == 4) { } 0 9 9
956 6 $has_v = @$v;
957 } elsif ($spec->{type} == TYPE_PAIR) {
958 0 $has_v = scalar(keys %$v);
959 } else {
960 9 $has_v = defined $v;
961 }
962  
963 15 ▶ _croak $self->error_prefix . sprintf("Option %s is required\n", $spec->{dest}) unless $has_v;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_v 4 11 15
964 }
965 }
966 }
967  
968 # interface
969 sub print_usage {
970 3 ▶ my $self = shift;
Subroutine print_usage: called CC=2 SCAR=6.9
Pod undocumented
971  
972 3 my $usage = $self->format_usage;
973  
974 3 print STDERR $_, "\n" for @$usage;
975 }
976  
977 # interface
978 sub print_command_usage {
979 1 ▶ my $self = shift;
Subroutine print_command_usage: called CC=5 SCAR=19.4
Pod undocumented
980 1 ▶ my $command = shift
Condition33%2/6
shift() || $self->namespace->get_attr('help_command') || $self->namespace->get_attr('current_command')
l!l&&r!l&&!r
100
shift() or $self->namespace->get_attr("help_command")
l!l&&r!l&&!r
010
MC/DC0%0/3
shift() || $self->namespace->get_attr('help_command') || $self->namespace->get_attr('current_command')
shift() $self->namespace->get_attr("help_command") $self->namespace->get_attr('current_command')
Truth table25%1/4
A or B || C
A= shift()B= $self->namespace->get_attr("help_command")C= $self->namespace->get_attr('current_command')
ABCresult
1XX1
01X1
0011
0000
981 || $self->namespace->get_attr('help_command')
982 || $self->namespace->get_attr('current_command'); # running help command
983  
984 1 my $usage = $self->format_command_usage($command);
985 1 ▶ if ($usage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usage) { } 1 0 1
986 1 print STDERR $_, "\n" for @$usage;
987 } else {
988 0 print STDERR
989 $self->error_prefix,
990 sprintf('No help for %s. See help', $self->namespace->get_attr('help_command')),
991 "\n";
992 }
993 }
994  
995 # Interface
996 sub format_usage {
997 5 ▶ my $self = shift;
Subroutine format_usage: called CC=16 SCAR=27.7
Pod undocumented
998  
999 5 ▶ $self->_sort_specs_by_groups() unless $self->{-groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-groups"} 5 0 5
1000  
1001 5 my $old_wrap_columns = $Text::Wrap::columns;
1002  
1003 5 my @usage;
1004  
1005 5 my $aliases = $self->aliases;
1006 5 my $prog = $self->prog;
1007 5 ▶ $prog .= ' (' . join(', ', @$aliases) . ')' if @$aliases;
Branch: 1 branch
BranchTrueFalseTotal
if @$aliases 1 4 5
1008 5 push @usage, wrap('', '', $prog. ': ' . $self->help);
1009  
1010 5 my ($help, $option_string) = $self->_format_group_usage();
1011 5 $Text::Wrap::columns = 80;
1012  
1013 5 my $header = sprintf(
1014 'usage: %s %s',
1015 $self->prog, $option_string
1016 );
1017  
1018 5 push @usage, wrap('', '', $header);
1019  
1020 5 ▶ if ($self->description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->description) 2 3 5
1021 2 my @lines = split("\n", $self->description);
1022  
1023 2 my @paragraphs;
1024  
1025 2 my $para = '';
1026 2 for my $line (@lines) {
1027 32 ▶ if ($line =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) { } 2 30 32
1028 2 push @paragraphs, $para;
1029 2 $para = '';
1030 } else {
1031 30 ▶ $para .= ( $para ? ' ' : '' ) . $line;
Branch: 1 branch
BranchTrueFalseTotal
$para ? : 26 4 30
1032 }
1033 }
1034  
1035 2 push @paragraphs, $para;
1036 2 for (@paragraphs) {
1037 4 push @usage, '';
1038 4 push @usage, wrap('', '', $_);
1039 }
1040 }
1041  
1042 5 push @usage, '';
1043  
1044 5 push @usage, @$help;
1045  
1046 5 ▶ if (exists $self->{-subparsers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-subparsers"}) 3 2 5
1047 3 push @usage, '';
1048 3 push @usage, wrap('', '', $self->{-subparsers}{-title});
1049 3 ▶ push @usage, wrap('', '', $self->{-subparsers}{-description}) if $self->{-subparsers}{-description};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-subparsers"}{"-description"} 1 2 3
1050  
1051 3 my $max = 12;
1052  
1053 3 for my $command ( keys %{$self->{-subparsers}{-parsers}} ) {
1054 6 my $len = length($command);
1055 6 ▶ $max = $len if $len > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $max 0 6 6
1056 }
1057  
1058 3 $max *= -1;
1059  
1060 3 for my $command ( sort keys %{$self->{-subparsers}{-parsers}} ) {
1061 6 my $parser = $self->{-subparsers}{-parsers}{$command};
1062 6 my $tab_head = ' ' x ( -1 * $max + 2 );
1063  
1064 6 my @desc = split("\n", wrap('', '', $parser->help));
1065 6 ▶ my $desc = (shift @desc) || '';
Condition50%1/2
shift @desc || ''
l!l
60
MC/DC0%0/1
shift @desc || ''
shift @desc
Truth table50%1/2
A
A= shift @desc
Aresult
11
00
1066 6 $_ = $tab_head . $_ for @desc;
1067 6 push @usage, sprintf(" %${max}s %s", $command, join("\n", $desc, @desc));
1068 }
1069 }
1070  
1071 5 ▶ push @usage, '', wrap('', '', $self->epilog) if $self->epilog;
Branch: 1 branch
BranchTrueFalseTotal
if $self->epilog 1 4 5
1072  
1073 5 $Text::Wrap::columns = $old_wrap_columns; # restore to original
1074  
1075 5 return \@usage;
1076 }
1077  
1078 sub format_command_usage {
1079 2 ▶ my $self = shift;
Subroutine format_command_usage: called CC=2 SCAR=7.0
Pod undocumented
1080 2 my $alias = shift;
1081  
1082 2 my $subp = $self->_get_subcommand_parser($alias);
1083 2 ▶ return '' unless $subp;
Branch: 1 branch
BranchTrueFalseTotal
unless $subp 0 2 2
1084  
1085 2 return $subp->format_usage();
1086 }
1087  
1088 # FIXME: Maybe we should remove this grouping thing
1089 sub _sort_specs_by_groups {
1090 5 ▶ my $self = shift;
Subroutine _sort_specs_by_groups: called CC=5 SCAR=16.1
1091  
1092 5 my $specs = $self->{-option_specs};
1093  
1094 5 for my $dest ( keys %{ $specs } ) {
1095 15 for my $group ( @{ $specs->{$dest}{groups} } ) {
1096 15 push @{ $self->{-groups}{$group}{-option} }, $specs->{$dest};
1097 }
1098 }
1099  
1100 5 $specs = $self->{-position_specs};
1101  
1102 5 for my $dest ( keys %{ $specs } ) {
1103 2 for my $group ( @{ $specs->{$dest}{groups} } ) {
1104 2 push @{ $self->{-groups}{$group}{-position} }, $specs->{$dest};
1105 }
1106 }
1107 }
1108  
1109 sub _format_group_usage {
1110 5 ▶ my $self = shift;
Subroutine _format_group_usage: called CC=13 SCAR=26.4
1111 5 my $group = '';
1112 # my $group = shift || '';
1113  
1114 5 ▶ unless ($self->{-groups}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"-groups"}) 0 5 5
1115 0 $self->_sort_specs_by_groups();
1116 }
1117  
1118 5 my $old_wrap_columns = $Text::Wrap::columns;
1119 5 $Text::Wrap::columns = 80;
1120  
1121 5 my @usage;
1122  
1123 my @option_specs = sort {
1124 $a->{position} <=> $b->{position}
1125 5 ▶ } @{ $self->{-groups}{$group}{-option} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-groups"}{$group}{"-option"} or [] 0 5 5
1126  
1127 my @flag_items = map {
1128 5 ($_->{required} ? '' : '[')
1129 15 ▶ . join('|', @{$_->{flags}})
Branch: 1 branch
BranchTrueFalseTotal
$_->{'required'} ? : 2 13 15
1130 15 ▶ . ($_->{required} ? '' : ']')
Branch: 1 branch
BranchTrueFalseTotal
$_->{'required'} ? : 2 13 15
1131 } @option_specs;
1132  
1133 my @position_specs = sort {
1134 $a->{position} <=> $b->{position}
1135 5 ▶ } @{ $self->{-groups}{$group}{-position} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-groups"}{$group}{"-position"} or [] 4 1 5
1136  
1137 my @position_items = map {
1138 5 ($_->{required} ? '' : '[')
1139 . $_->{metavar}
1140 2 ▶ . ($_->{required} ? '' : ']')
Branch: 2 branches
BranchTrueFalseTotal
$_->{'required'} ? : 2 0 2
$_->{'required'} ? : 2 0 2
1141 } @position_specs;
1142  
1143 5 ▶ my @subcommand_items = ('<command>', '[<args>]') if exists $self->{-subparsers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"-subparsers"} 3 2 5
1144  
1145 5 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) 0 5 5
1146 0 ▶ push @usage, wrap('', '', $group . ': ' . ($self->{-group_description}{$group} || '') );
Condition0%0/2
$self->{'-group_description'}{$group} || ''
l!l
00
MC/DC0%0/1
$self->{'-group_description'}{$group} || ''
$self->{'-group_description'}{$group}
Truth table0%0/2
A
A= $self->{'-group_description'}{$group}
Aresult
11
00
1147 }
1148  
1149 5 ▶ if (@position_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@position_specs) 1 4 5
1150 1 push @usage, 'positional arguments:';
1151 1 push @usage, @{ $self->_format_usage_by_spec(\@position_specs) };
1152 }
1153  
1154 5 ▶ if (@option_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@option_specs) 5 0 5
1155 5 push @usage, 'optional arguments:';
1156 5 push @usage, @{ $self->_format_usage_by_spec(\@option_specs) };
1157 }
1158  
1159 5 $Text::Wrap::columns = $old_wrap_columns; # restore to original
1160  
1161 5 return ( \@usage, join(' ', @position_items, @flag_items, @subcommand_items) ) ;
1162 }
1163  
1164 sub _format_usage_by_spec {
1165 6 ▶ my $self = shift;
Subroutine _format_usage_by_spec: called CC=18 SCAR=30.1
1166 6 my $specs = shift;
1167  
1168 6 ▶ return unless $specs;
Branch: 1 branch
BranchTrueFalseTotal
unless $specs 0 6 6
1169  
1170 6 my @usage;
1171 6 my $max = 10;
1172 6 my @item_help;
1173  
1174 6 for my $spec ( @$specs ) {
1175 my $item = sprintf(
1176 "%s %s",
1177 17 join(', ', @{$spec->{flags}}),
1178 $spec->{metavar},
1179 17 );
1180 17 my $len = length($item);
1181 17 ▶ $max = $len if $len > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $max 11 6 17
1182  
1183 # generate default string
1184 17 my $default = '';
1185 17 my $values = [];
1186  
1187 17 ▶ if (defined $spec->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"default"}) 3 14 17
1188 3 ▶ if (ref $spec->{default} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec->{'default'} eq 'HASH') { } 1 2 3
elsif (ref $spec->{'default'} eq 'ARRAY') { } 2 0 2
1189 1 ▶ while (my ($k, $v) = each %{$spec->{default}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$spec{"default"};}) 2 1 3
1190 2 push @$values, "$k=$v";
1191 }
1192 } elsif (ref $spec->{default} eq "ARRAY") {
1193 2 $values = $spec->{default};
1194 } else {
1195 0 $values = [ $spec->{default} ];
1196 }
1197 }
1198  
1199 17 ▶ if (@$values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 3 14 17
1200 3 $default = 'Default: ' . join(', ', @$values);
1201 }
1202  
1203 # generate choice string
1204 17 my $choices;
1205 17 my $case = '';
1206  
1207 17 ▶ if ($spec->{choices} && ref $spec->{choices} ne 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'choices'} and ref $spec->{'choices'} ne 'CODE') { } 0 17 17
elsif ($spec->{'choices_i'}) { } 0 17 17
Condition33%1/3
$spec->{"choices"} and ref $spec->{"choices"} ne "CODE"
!ll&&!rl&&r
1700
MC/DC0%0/2
$spec->{"choices"} and ref $spec->{"choices"} ne "CODE"
$spec->{"choices"} ref $spec->{"choices"} ne "CODE"
Truth table33%1/3
A and B
A= $spec->{"choices"}B= ref $spec->{"choices"} ne "CODE"
ABresult
0X0
100
111
1208 0 $choices = $spec->{choices};
1209 0 $case = 'case sensitive';
1210 } elsif ($spec->{choices_i}) {
1211 0 $choices = $spec->{choices_i};
1212 0 $case = 'case insensitive';
1213 } else {
1214 17 $choices = undef;
1215 }
1216  
1217 17 my $choice_str = '';
1218 17 ▶ if ($choices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($choices) 0 17 17
1219 0 $choice_str = 'Choices: [' . join(', ', @$choices) . '], ' . $case . "\n";
1220 }
1221  
1222 push @item_help, [
1223 $item,
1224 ($spec->{required} ? ' ' : '?'),
1225 17 ▶ join("\n", ($spec->{help} || 'This is option ' . $spec->{dest}), $choice_str . $default),
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'required'} ? : 4 13 17
Condition100%2/2
$spec->{'help'} || 'This is option ' . $spec->{'dest'}
l!l
710
MC/DC100%1/1
$spec->{'help'} || 'This is option ' . $spec->{'dest'}
$spec->{'help'}
Truth table100%2/2
A
A= $spec->{'help'}
Aresult
11
00
1226 ];
1227 }
1228  
1229 6 $max *= -1;
1230 6 my $format = " %${max}s %s %s";
1231 6 $Text::Wrap::columns = 60;
1232 6 for my $ih (@item_help) {
1233 17 my $item_len = length($ih->[0]);
1234 # The prefixed whitespace in subsequent lines in the wrapped
1235 # help string
1236 17 my $sub_tab = " " x (-1 * $max + 4 + 4 + 2);
1237 17 my @help = split("\n", wrap('', '', $ih->[2]));
1238  
1239 17 ▶ my $help = (shift @help) || '' ; # head
Condition50%1/2
shift @help || ''
l!l
170
MC/DC0%0/1
shift @help || ''
shift @help
Truth table50%1/2
A
A= shift @help
Aresult
11
00
1240 17 $_ = $sub_tab . $_ for @help; # tail
1241  
1242 17 push @usage, sprintf($format, $ih->[0], $ih->[1], join("\n", $help, @help));
1243 }
1244  
1245 6 return \@usage;
1246 }
1247  
1248 # translate option spec to the one accepted by
1249 # Getopt::Long::GetOptions
1250 sub _get_option_spec {
1251 158 ▶ my $self = shift;
Subroutine _get_option_spec: called CC=9 SCAR=22.3
1252 158 my $spec = shift;
1253  
1254 158 my @flags = @{ $spec->{flags} };
1255 158 $_ =~ s/^-+// for @flags;
1256 158 my $name = join('|', @flags);
1257 158 my $type = 's';
1258 158 my $desttype = '';
1259  
1260 158 my $optional_flag = '='; # not optional
1261  
1262 158 ▶ if ($spec->{type} == TYPE_SCALAR) {
Branch: 6 branches
BranchTrueFalseTotal
if ($spec->{'type'} == 1) { } 48 110 158
elsif ($spec->{'type'} == 2) { } 10 100 110
elsif ($spec->{'type'} == 4) { } 4 96 100
elsif ($spec->{'type'} == 0) { } 0 96 96
elsif ($spec->{'type'} == 5) { } 92 4 96
elsif ($spec->{'type'} == 3) { } 4 0 4
1263 48 $desttype = '@';
1264 } elsif ($spec->{type} == TYPE_ARRAY) {
1265 10 $desttype = '@';
1266 } elsif ($spec->{type} == TYPE_PAIR) {
1267 4 $desttype = '@';
1268 } elsif ($spec->{type} == TYPE_UNDEF) {
1269 0 $optional_flag = ':';
1270 } elsif ($spec->{type} == TYPE_BOOL) {
1271 92 $type = '';
1272 92 $optional_flag = '';
1273 92 $desttype = '';
1274 } elsif ($spec->{type} == TYPE_COUNT) {
1275 # pass
1276 4 $type = '';
1277 4 $optional_flag = '';
1278 4 $desttype = '+';
1279 } else {
1280 # pass
1281 # should never be here
1282 0 ▶ _croak $self->error_prefix . 'Unknown type:' . ($spec->{type} || 'undef');
Condition0%0/2
$spec->{'type'} || 'undef'
l!l
00
MC/DC0%0/1
$spec->{'type'} || 'undef'
$spec->{'type'}
Truth table0%0/2
A
A= $spec->{'type'}
Aresult
11
00
1283 }
1284  
1285 158 my $repeat = '';
1286  
1287 158 my $opt = join('', $name, $optional_flag, $type, $repeat, $desttype);
1288  
1289 158 return $opt;
1290 }
1291  
1292 20 1;
1293  
1294 __END__
« blib/lib/Getopt/ArgParse/Parser.pm ↑ Summary blib/lib/Getopt/ArgParse/Namespace.pm »