ArgParse/Parser.pm

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