Easer/V1.pm

filter: Statementstmt 88.5% 372 / 420 Branchbran 71.4% 163 / 228 Conditioncond 63.1% 96 / 152 MC/DCmcdc 39.0% 32 / 82 Subroutinesub 94.9% 56 / 59 Podpod 100% 47 / 47 totaltotal 77.5% 766 / 988 CC 192 SCAR 28.7
CC
192
Cov
79%
commandline_help
109.5
get_children
22.8
name_for_option
17.7
CRAP 17.7
↑ Summary Easer/V2.pm »
1 package App::Easer::V1;
2 22 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶no warnings qw< experimental::signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 { our $VERSION = '2.016' }
7  
8 22 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 our @EXPORT_OK = qw< d run appeaser_api >;
10  
11 103 ▶sub add_auto_commands ($application) {
Subroutine add_auto_commands: called CC=3 SCAR=11.0
Pod documented
12 103 my $commands = $application->{commands};
13 $commands->{help} //= {
14 103 ▶ name => 'help',
Condition100%2/2
$commands->{"help"} //= {"name", "help", "supports", ["help"], "help", "print a help message", "description", "print help for (sub)command", "allow-residual-options", 0, "leaf", 1, "execute", \&App::Easer::V1::stock_help}
l!l
7924
MC/DC100%1/1
$commands->{"help"} //= {"name", "help", "supports", ["help"], "help", "print a help message", "description", "print help for (sub)command", "allow-residual-options", 0, "leaf", 1, "execute", \&App::Easer::V1::stock_help}
$commands->{"help"}
Truth table100%2/2
A
A= $commands->{"help"}
Aresult
11
00
15 supports => ['help'],
16 help => 'print a help message',
17 description => 'print help for (sub)command',
18 'allow-residual-options' => 0,
19 leaf => 1,
20 execute => \&stock_help,
21 };
22 $commands->{commands} //= {
23 103 ▶ name => 'commands',
Condition100%2/2
$commands->{"commands"} //= {"name", "commands", "supports", ["commands"], "help", "list sub-commands", "description", "Print list of supported sub-commands", "allow-residual-options", 0, "leaf", 1, "execute", \&App::Easer::V1::stock_commands}
l!l
7924
MC/DC100%1/1
$commands->{"commands"} //= {"name", "commands", "supports", ["commands"], "help", "list sub-commands", "description", "Print list of supported sub-commands", "allow-residual-options", 0, "leaf", 1, "execute", \&App::Easer::V1::stock_commands}
$commands->{"commands"}
Truth table100%2/2
A
A= $commands->{"commands"}
Aresult
11
00
24 supports => ['commands'],
25 help => 'list sub-commands',
26 description => 'Print list of supported sub-commands',
27 'allow-residual-options' => 0,
28 leaf => 1,
29 execute => \&stock_commands,
30 };
31 103 return $application;
32 } ## end sub add_auto_commands ($application)
33  
34 3 ▶sub appeaser_api { return 'V1' }
Subroutine appeaser_api: called CC=1 SCAR=0.0
Pod documented
35  
36 183 ▶sub collect ($self, $spec, $args) {
Subroutine collect: called CC=4 SCAR=13.9
Pod documented
37 183 my @sequence;
38 183 my $config = {};
39 183 my @residual_args;
40  
41 183 my $merger = merger($self, $spec);
42  
43 183 for my $source_spec (sources($self, $spec, $args)) {
44 734 ▶ my ($src, $src_cnf) =
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $source_spec ? : 0 734 734
45 'ARRAY' eq ref $source_spec
46 ? $source_spec->@*
47 : ($source_spec, {});
48 734 $src = $self->{factory}->($src, 'collect'); # "resolve"
49 734 $src_cnf = {$spec->%*, $src_cnf->%*, config => $config};
50 734 my ($slice, $residual_args) = $src->($self, $src_cnf, $args);
51 734 ▶ push @residual_args, $residual_args->@* if defined $residual_args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $residual_args 183 551 734
52 734 push @sequence, $slice;
53 734 $config = $merger->(@sequence);
54 } ## end for my $source_spec (sources...)
55  
56 183 return ($config, \@residual_args);
57 } ## end sub collect
58  
59 184 ▶sub collect_options ($self, $spec, $args) {
Subroutine collect_options: called CC=3 SCAR=11.0
Pod documented
60 184 my $factory = $self->{factory};
61 my $collect = $spec->{collect}
62 184 ▶ // $self->{application}{configuration}{collect} // \&collect;
Condition80%4/5
($spec->{"collect"} // $self->{"application"}{"configuration"}{"collect"}) // \&App::Easer::V1::collect
l!l
1183
$spec->{"collect"} // $self->{"application"}{"configuration"}{"collect"}
l!l&&r!l&&!r
01183
MC/DC50%1/2
($spec->{"collect"} // $self->{"application"}{"configuration"}{"collect"}) // \&App::Easer::V1::collect
$spec->{"collect"} $self->{"application"}{"configuration"}{"collect"}
Truth table66%2/3
A // B
A= $spec->{"collect"}B= $self->{"application"}{"configuration"}{"collect"}
ABresult
1X1
011
000
63 184 my $collector = $factory->($collect, 'collect'); # "resolve"
64 184 (my $config, $args) = $collector->($self, $spec, $args);
65 184 push $self->{configs}->@*, $config;
66 184 return $args;
67 } ## end sub collect_options
68  
69 25 ▶sub commandline_help ($getopt) {
Subroutine commandline_help: called CC=24 SCAR=47.0
Pod documented
70 25 my @retval;
71  
72 25 my ($mode, $type, $desttype, $min, $max, $default);
73 25 ▶ if (substr($getopt, -1, 1) eq '!') {
Branch: 5 branches
BranchTrueFalseTotal
if (substr($getopt, -1, 1) eq '!') { } 10 15 25
elsif (substr($getopt, -1, 1) eq '+') { } 0 15 15
elsif ($getopt =~ s/( [:=]) # 1 mode ([siof]) # 2 type ([\@%])? # 3 desttype (?: \{ (\d*)? # 4 min ,? (\d*)? # 5 max \} )? \z//msux) { } 15 0 15
elsif ($getopt =~ s/: (\d+) ([\@%])? \z//msux) { } 0 0 0
elsif ($getopt =~ s/:+ ([\@%])? \z//msux) { } 0 0 0
74 10 $type = 'bool';
75 10 substr $getopt, -1, 1, '';
76 10 push @retval, 'boolean option';
77 }
78 elsif (substr($getopt, -1, 1) eq '+') {
79 0 $mode = 'increment';
80 0 substr $getopt, -1, 1, '';
81 0 push @retval,
82 'incremental option (adds 1 every time it is provided)';
83 } ## end elsif (substr($getopt, -1...))
84 elsif (
85 $getopt =~ s<(
86 [:=]) # 1 mode
87 ([siof]) # 2 type
88 ([@%])? # 3 desttype
89 (?:
90 \{
91 (\d*)? # 4 min
92 ,?
93 (\d*)? # 5 max
94 \}
95 )? \z><>mxs
96 )
97 {
98 15 ▶ $mode = $1 eq '=' ? 'mandatory' : 'optional';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '=' ? : 15 0 15
99 15 $type = $2;
100 15 $desttype = $3;
101 15 $min = $4;
102 15 $max = $5;
103 15 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 0 15 15
104 0 ▶ $mode = $min ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$min ? : 0 0 0
105 }
106 $type = {
107 s => 'string',
108 i => 'integer',
109 o => 'perl-extended-integer',
110 f => 'float',
111 15 }->{$type};
112 15 my $line = "$mode $type option";
113 15 ▶ $line .= ", at least $min times" if defined($min) && $min > 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $min > 1 0 15 15
Condition33%1/3
defined $min and $min > 1
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $min and $min > 1
defined $min $min > 1
Truth table33%1/3
A and B
A= defined $minB= $min > 1
ABresult
0X0
100
111
114 15 ▶ $line .= ", no more than $max times"
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and length $max 0 15 15
Condition33%1/3
defined $max and length $max
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $max and length $max
defined $max length $max
Truth table33%1/3
A and B
A= defined $maxB= length $max
ABresult
0X0
100
111
115 if defined($max) && length($max);
116 15 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 0 15 15
Condition33%1/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $desttype and $desttype eq "\@"
defined $desttype $desttype eq "\@"
Truth table33%1/3
A and B
A= defined $desttypeB= $desttype eq "\@"
ABresult
0X0
100
111
117 15 push @retval, $line;
118 } ## end elsif ($getopt =~ s<( )? \z><>mxs)
119 elsif ($getopt =~ s<: (\d+) ([@%])? \z><>mxs) {
120 0 $mode = 'optional';
121 0 $type = 'i';
122 0 $default = $1;
123 0 $desttype = $2;
124 0 my $line = "optional integer, defaults to $default";
125 0 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 0 0 0
Condition0%0/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desttype and $desttype eq "\@"
defined $desttype $desttype eq "\@"
Truth table0%0/3
A and B
A= defined $desttypeB= $desttype eq "\@"
ABresult
0X0
100
111
126 0 push @retval, $line;
127 } ## end elsif ($getopt =~ s<: (\d+) ([@%])? \z><>mxs)
128 elsif ($getopt =~ s<:+ ([@%])? \z><>mxs) {
129 0 $mode = 'optional';
130 0 $type = 'i';
131 0 $default = 'increment';
132 0 $desttype = $1;
133 0 my $line = "optional integer, current value incremented if omitted";
134 0 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 0 0 0
Condition0%0/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desttype and $desttype eq "\@"
defined $desttype $desttype eq "\@"
Truth table0%0/3
A and B
A= defined $desttypeB= $desttype eq "\@"
ABresult
0X0
100
111
135 0 push @retval, $line;
136 } ## end elsif ($getopt =~ s<:+ ([@%])? \z><>mxs)
137  
138 25 my @alternatives = split /\|/, $getopt;
139 25 ▶ if ($type eq 'bool') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bool') { } 10 15 25
elsif ($mode eq 'optional') { } 0 15 15
140 push @retval, map {
141 10 ▶ if (length($_) == 1) { "-$_" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 8 10 18
142 10 else { "--$_ | --no-$_" }
143 } @alternatives;
144 } ## end if ($type eq 'bool')
145 elsif ($mode eq 'optional') {
146 push @retval, map {
147 0 ▶ if (length($_) == 1) { "-$_ [<value>]" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 0 0 0
148 0 else { "--$_ [<value>]" }
149 } @alternatives;
150 } ## end elsif ($mode eq 'optional')
151 else {
152 push @retval, map {
153 15 ▶ if (length($_) == 1) { "-$_ <value>" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 15 15 30
154 15 else { "--$_ <value>" }
155 } @alternatives;
156 } ## end else [ if ($type eq 'bool') ]
157  
158 25 return @retval;
159 } ## end sub commandline_help ($getopt)
160  
161 184 ▶sub commit_configuration ($self, $spec, $args) {
Subroutine commit_configuration: called CC=2 SCAR=9.2
Pod documented
162 184 ▶ my $commit = $spec->{commit} // return;
Condition50%1/2
$spec->{"commit"} // (return)
l!l
0184
MC/DC0%0/1
$spec->{"commit"} // (return)
$spec->{"commit"}
Truth table50%1/2
A
A= $spec->{"commit"}
Aresult
11
00
163 0 $self->{factory}->($commit, 'commit')->($self, $spec, $args);
164 }
165  
166 0 ▶sub d (@stuff) {
Subroutine d: not called CC=2 SCAR=10.9
Pod documented
167 22 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 0 require Data::Dumper;
169 0 local $Data::Dumper::Indent = 1;
170 0 ▶ warn Data::Dumper::Dumper(@stuff % 2 ? \@stuff : {@stuff});
Branch: 1 branch
BranchTrueFalseTotal
@stuff % 2 ? : 0 0 0
171 } ## end sub d (@stuff)
172  
173 183 ▶sub default_getopt_config ($self, $spec) {
Subroutine default_getopt_config: called CC=3 SCAR=11.0
Pod documented
174 183 my @r = qw< gnu_getopt >;
175 183 ▶ push @r, qw< require_order pass_through >
Branch: 1 branch
BranchTrueFalseTotal
if has_children($self, $spec) 114 69 183
176 if has_children($self, $spec);
177 183 ▶ push @r, qw< pass_through > if $spec->{'allow-residual-options'};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"allow-residual-options"} 0 183 183
178 183 return \@r;
179 }
180  
181 93 ▶sub execute ($self, $args) {
Subroutine execute: called CC=3 SCAR=11.3
Pod documented
182 93 my $command = $self->{trail}[-1][0];
183 my $executable = fetch_spec_for($self, $command)->{execute}
184 93 ▶ or die "no executable for '$command'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $executable = fetch_spec_for($self, $command)->{"execute"} or die "no executable for '${command}'\n" 93 0 93
185 93 $executable = $self->{factory}->($executable, 'execute'); # "resolve"
186 93 ▶ my $config = $self->{configs}[-1] // {};
Condition50%1/2
$self->{"configs"}[-1] // {}
l!l
930
MC/DC0%0/1
$self->{"configs"}[-1] // {}
$self->{"configs"}[-1]
Truth table50%1/2
A
A= $self->{"configs"}[-1]
Aresult
11
00
187 93 return $executable->($self, $config, $args);
188 } ## end sub execute
189  
190 37 ▶sub fetch_subcommand_default ($self, $spec) {
Subroutine fetch_subcommand_default: called CC=5 SCAR=16.7
Pod documented
191 37 my $acfg = $self->{application}{configuration};
192 my $child = exists($spec->{'default-child'}) ? $spec->{'default-child'}
193 37 ▶ : exists($acfg->{'default-child'}) ? $acfg->{'default-child'}
Branch: 2 branches
BranchTrueFalseTotal
exists $spec->{'default-child'} ? : 37 0 37
exists $acfg->{'default-child'} ? : 0 0 0
194 : get_child($self, $spec, 'help'); # help is last resort
195 37 ▶ return ($child, $child) if defined $child && length $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child and length $child 14 23 37
Condition100%3/3
defined $child and length $child
!ll&&!rl&&r
12214
MC/DC100%2/2
defined $child and length $child
defined $child length $child
Truth table100%3/3
A and B
A= defined $childB= length $child
ABresult
0X0
100
111
196 23 return;
197 }
198  
199 184 ▶sub fetch_subcommand ($self, $spec, $args) {
Subroutine fetch_subcommand: called CC=7 SCAR=19.5
Pod documented
200 184 ▶ my ($subc, $alias) = fetch_subcommand_wh($self, $spec, $args)
Branch: 1 branch
BranchTrueFalseTotal
() = fetch_subcommand_wh($self, $spec, $args) or return 85 93 178
201 or return;
202 85 my $r = ref $subc;
203 85 ▶ if ($r eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 3 82 85
204 $subc = $spec->{children}[$subc->{index}]
205 3 ▶ if scalar(keys $subc->%*) == 1 && defined $subc->{index};
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$subc == 1 and defined $subc->{"index"} 2 1 3
Condition66%2/3
scalar keys %$subc == 1 and defined $subc->{"index"}
!ll&&!rl&&r
102
MC/DC50%1/2
scalar keys %$subc == 1 and defined $subc->{"index"}
scalar keys %$subc == 1 defined $subc->{"index"}
Truth table66%2/3
A and B
A= scalar keys %$subc == 1B= defined $subc->{"index"}
ABresult
0X0
100
111
206 3 $r = ref $subc;
207 3 ▶ return ($subc, $subc->{supports}[0]) if $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "HASH" 2 1 3
208 1 $alias = $subc;
209 }
210 83 ▶ die "invalid sub-command (ref to $r)" if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 83 83
211 83 return ($subc, $alias);
212 }
213  
214 184 ▶sub fetch_subcommand_wh ($self, $spec, $args) {
Subroutine fetch_subcommand_wh: called CC=14 SCAR=26.5
Pod documented
215 # if there's a dispatch, use that to figure out where to go next
216 # **this** might even overcome having children at all!
217 184 for my $cfg ($spec, $self->{application}{configuration}) {
218 366 ▶ next unless exists $cfg->{dispatch};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cfg->{"dispatch"} 364 2 366
219 2 my $sub = $self->{factory}->($cfg->{dispatch}, 'dispatch');
220 2 ▶ defined(my $child = $sub->($self, $spec, $args)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $child = &$sub($self, $spec, $args)) or return 2 0 2
221 2 return ($child, $child);
222 }
223  
224 # regular course here, no point in going forth without children
225 182 ▶ return unless has_children($self, $spec);
Branch: 1 branch
BranchTrueFalseTotal
unless has_children($self, $spec) 70 112 182
226  
227 # use defaults if there's no argument to investigate
228 112 ▶ return fetch_subcommand_default($self, $spec) unless $args->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 35 77 112
229  
230 # try to get a child from the first argument
231 77 ▶ if (my $child = get_child($self, $spec, $args->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = get_child($self, $spec, $args->[0])) 65 12 77
232 65 return ($child, shift $args->@*); # consumed arg name
233 }
234  
235 # the first argument didn't help, but we might want to fallback
236 12 for my $cfg ($spec, $self->{application}{configuration}) {
237 18 ▶ if (exists $cfg->{fallback}) { # executable
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{"fallback"}) 2 16 18
238 2 ▶ defined(my $fb = $cfg->{fallback}) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $fb = $cfg->{"fallback"}) or return 2 0 2
239 2 my $sub = $self->{factory}->($fb, 'fallback'); # "resolve"
240 2 ▶ defined(my $child = $sub->($self, $spec, $args)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $child = &$sub($self, $spec, $args)) or return 2 0 2
241 2 return ($child, $child);
242 }
243 16 ▶ if (exists $spec->{'fallback-to'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"fallback-to"}) 2 14 16
244 2 ▶ defined(my $fbto = $spec->{'fallback-to'}) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $fbto = $spec->{"fallback-to"}) or return 2 0 2
245 2 return ($fbto, $fbto);
246 }
247 return fetch_subcommand_default($self, $spec)
248 14 ▶ if $cfg->{'fallback-to-default'};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"fallback-to-default"} 2 12 14
249 }
250  
251 # no fallback at this point... it's an error, build a message and die!
252 6 my @names = map { $_->[1] } $self->{trail}->@*;
253 6 shift @names; # remove first one
254 6 my $path = join '/', @names, $args->[0]; # $args->[0] was the candidate
255 6 die "cannot find sub-command '$path'\n";
256 } ## end sub fetch_subcommand_wh
257  
258 104 ▶sub generate_factory ($c) {
Subroutine generate_factory: called CC=2 SCAR=7.0
Pod documented
259 104 my $w = \&stock_factory; # default factory
260 104 ▶ $w = stock_factory($c->{create}, 'factory', $c) if defined $c->{create};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"create"} 0 104 104
261 104 ▶ return sub ($e, $d = '') { $w->($e, $d, $c) };
Subroutine __ANON__: called CC=2 SCAR=6.9
262 }
263  
264 88 ▶sub get_child ($self, $spec, $name) {
Subroutine get_child: called CC=4 SCAR=13.9
Pod documented
265 88 for my $child (get_children($self, $spec)) {
266 157 my $command = fetch_spec_for($self, $child);
267 next
268 199 ▶ unless grep { $_ eq $name }
Condition100%2/2
$command->{"supports"} //= [$child]
l!l
1534
MC/DC100%1/1
$command->{"supports"} //= [$child]
$command->{"supports"}
Truth table100%2/2
A
A= $command->{"supports"}
Aresult
11
00
269 157 ▶ ($command->{supports} //= [$child])->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$command->{"supports"} //= [$child]} 81 76 157
270 76 return $child;
271 } ## end for my $child (get_children...)
272 12 return;
273 } ## end sub get_child
274  
275 10 ▶sub stock_ChildrenByPrefix ($self, $spec, @prefixes) {
Subroutine stock_ChildrenByPrefix: called CC=5 SCAR=16.1
Pod documented
276 10 require File::Spec;
277 my @expanded_inc = map {
278 10 my ($v, $dirs) = File::Spec->splitpath($_, 'no-file');
279 70 [$v, File::Spec->splitdir($dirs)];
280 } @INC;
281 10 my %seen;
282 return map {
283 10 my @parts = split m{::}mxs, $_ . 'x';
284 10 substr(my $bprefix = pop @parts, -1, 1, '');
285 map {
286 10 my ($v, @dirs) = $_->@*;
287 70 my $dirs = File::Spec->catdir(@dirs, @parts);
288 70 ▶ if (opendir my $dh, File::Spec->catpath($v, $dirs, '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, 'File::Spec'->catpath($v, $dirs, '')) { } 10 60 70
289 18 grep { ! $seen{$_}++ }
290 map {
291 18 substr(my $lastpart = $_, -3, 3, '');
292 18 join '::', @parts, $lastpart;
293 } grep {
294 10 my $path = File::Spec->catpath($v, $dirs, $_);
295 46 ▶ (-e $path && ! -d $path)
Condition66%6/9
-e $path && !(-d $path) && substr($_, 0, length $bprefix) eq $bprefix && substr($_, -3, 3) eq '.pm'
!ll&&!rl&&r
28018
-e $path and not -d $path and substr($_, 0, length $bprefix) eq $bprefix
!ll&&!rl&&r
28018
-e $path and not -d $path
!ll&&!rl&&r
02818
MC/DC25%1/4
-e $path && !(-d $path) && substr($_, 0, length $bprefix) eq $bprefix && substr($_, -3, 3) eq '.pm'
-e $path not -d $path substr($_, 0, length $bprefix) eq $bprefix substr($_, -3, 3) eq '.pm'
Truth table40%2/5
A and not(B) and C && D
A= -e $pathB= not -d $pathC= substr($_, 0, length $bprefix) eq $bprefixD= substr($_, -3, 3) eq '.pm'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
296 && substr($_, 0, length($bprefix)) eq $bprefix
297 && substr($_, -3, 3) eq '.pm'
298 } readdir $dh;
299 }
300 60 else { () }
301 } @expanded_inc;
302 } @prefixes;
303 }
304  
305 126 ▶sub expand_children ($self, $spec, $child_spec) {
Subroutine expand_children: called CC=2 SCAR=6.9
Pod documented
306 126 ▶ return $child_spec unless ref($child_spec) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child_spec eq "ARRAY" 116 10 126
307 10 my ($exe, @args) = $child_spec->@*;
308 10 return $self->{factory}->($exe, 'children')->($self, $spec, @args);
309 }
310  
311 482 ▶sub get_children ($self, $spec, $expand = 1) {
Subroutine get_children: called CC=15 SCAR=31.3
Pod documented
312 482 ▶ return if $spec->{leaf};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"leaf"} 71 411 482
313 411 ▶ return if exists($spec->{children}) && !$spec->{children};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"children"} and not $spec->{"children"} 23 388 411
Condition100%3/3
exists $spec->{"children"} and not $spec->{"children"}
!ll&&!rl&&r
15423423
MC/DC100%2/2
exists $spec->{"children"} and not $spec->{"children"}
exists $spec->{"children"} not $spec->{"children"}
Truth table100%3/3
A and not(B)
A= exists $spec->{"children"}B= not $spec->{"children"}
ABresult
0X0
100
111
314 388 ▶ my @children = ($spec->{children} // [])->@*;
Condition100%2/2
$spec->{"children"} // []
l!l
234154
MC/DC100%1/1
$spec->{"children"} // []
$spec->{"children"}
Truth table100%2/2
A
A= $spec->{"children"}
Aresult
11
00
315  
316 # set auto-leaves as 1 by default, new in 0.007002
317 $self->{application}{configuration}{'auto-leaves'} = 1
318 388 ▶ unless exists $self->{application}{configuration}{'auto-leaves'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"application"}{"configuration"}{"auto-leaves"} 10 378 388
319  
320 return
321 388 ▶ if $self->{application}{configuration}{'auto-leaves'}
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"application"}{"configuration"}{"auto-leaves"} and @children == 0 55 333 388
Condition100%3/3
$self->{"application"}{"configuration"}{"auto-leaves"} and @children == 0
!ll&&!rl&&r
9923455
MC/DC100%2/2
$self->{"application"}{"configuration"}{"auto-leaves"} and @children == 0
$self->{"application"}{"configuration"}{"auto-leaves"} @children == 0
Truth table100%3/3
A and B
A= $self->{"application"}{"configuration"}{"auto-leaves"}B= @children == 0
ABresult
0X0
100
111
322 && @children == 0; # no auto-children for leaves under auto-leaves
323  
324 # skip expansion if $expand is false (default is expand)
325 333 ▶ @children = map { expand_children($self, $spec, $_) } @children
Branch: 1 branch
BranchTrueFalseTotal
if $expand 107 226 333
326 if $expand;
327  
328 my @auto =
329 exists $self->{application}{configuration}{'auto-children'}
330 333 ▶ ? (($self->{application}{configuration}{'auto-children'} // [])->@*)
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'application'}{'configuration'}{'auto-children'} ? : 0 333 333
Condition0%0/2
$self->{"application"}{"configuration"}{"auto-children"} // []
l!l
00
MC/DC0%0/1
$self->{"application"}{"configuration"}{"auto-children"} // []
$self->{"application"}{"configuration"}{"auto-children"}
Truth table0%0/2
A
A= $self->{"application"}{"configuration"}{"auto-children"}
Aresult
11
00
331 : (qw< help commands >);
332 333 ▶ if (exists $spec->{'no-auto'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"no-auto"}) 0 333 333
333 0 ▶ if (ref $spec->{'no-auto'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec->{'no-auto'}) { } 0 0 0
elsif ($spec->{'no-auto'} eq '*') { } 0 0 0
334 0 my %no = map { $_ => 1 } $spec->{'no-auto'}->@*;
335 0 @auto = grep { !$no{$_} } @auto;
336 }
337 elsif ($spec->{'no-auto'} eq '*') {
338 0 @auto = ();
339 }
340 else {
341 0 die "invalid no-auto, array or '*' are allowed\n";
342 }
343 } ## end if (exists $spec->{'no-auto'...})
344 333 return (@children, @auto);
345 } ## end sub get_children
346  
347 # traverse a whole @$list of sub-commands from $start. This is used to
348 # list "commands" at a certain sub-level or show help
349 27 ▶sub get_descendant ($self, $start, $list) {
Subroutine get_descendant: called CC=6 SCAR=19.6
Pod documented
350 27 my $target = $start;
351 27 my $path;
352 27 for my $desc ($list->@*) {
353 11 ▶ $path = defined($path) ? "$path/$desc" : $desc;
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 0 11 11
354 11 ▶ my $command = fetch_spec_for($self, $target)
Branch: 1 branch
BranchTrueFalseTotal
my $command = fetch_spec_for($self, $target) or die "cannot find sub-command '${path}'\n" 11 0 11
355 or die "cannot find sub-command '$path'\n";
356 11 ▶ defined($target = get_child($self, $command, $desc))
Branch: 1 branch
BranchTrueFalseTotal
defined($target = get_child($self, $command, $desc)) or die "cannot find sub-command '${path}'\n" 11 0 11
357 or die "cannot find sub-command '$path'\n";
358 } ## end for my $desc ($list->@*)
359  
360 # check that this last is associated to a real command
361 27 ▶ return $target if fetch_spec_for($self, $target);
Branch: 1 branch
BranchTrueFalseTotal
if fetch_spec_for($self, $target) 27 0 27
362 0 die "cannot find sub-command '$path'\n";
363 } ## end sub get_descendant
364  
365 365 ▶sub has_children ($self, $spec) { get_children($self, $spec, 0) ? 1 : 0 }
Subroutine has_children: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
get_children($self, $spec, 0) ? : 226 139 365
366  
367 737 ▶sub hash_merge {
Subroutine hash_merge: called CC=8 SCAR=20.8
Pod documented
368 737 my (%retval, %is_overridable);
369 737 for my $href (@_) {
370 1850 for my $src_key (keys $href->%*) {
371 638 my $dst_key = $src_key;
372 638 my $this_overridable;
373 638 ▶ if ($dst_key =~ m{\A //= (.*) \z}mxs) { # overridable
Branch: 1 branch
BranchTrueFalseTotal
if ($dst_key =~ m[\A //= (.*) \z]msux) 354 284 638
374 354 $dst_key = $1;
375 354 ▶ $is_overridable{$dst_key} = 1 unless exists $retval{$dst_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $retval{$dst_key} 354 0 354
376 354 $this_overridable = 1;
377 }
378 $retval{$dst_key} = $href->{$src_key}
379 638 ▶ if $is_overridable{$dst_key} || ! exists($retval{$dst_key});
Branch: 1 branch
BranchTrueFalseTotal
if $is_overridable{$dst_key} or not exists $retval{$dst_key} 622 16 638
Condition100%3/3
$is_overridable{$dst_key} or not exists $retval{$dst_key}
l!l&&r!l&&!r
37724516
MC/DC100%2/2
$is_overridable{$dst_key} or not exists $retval{$dst_key}
$is_overridable{$dst_key} not exists $retval{$dst_key}
Truth table100%3/3
A or not(B)
A= $is_overridable{$dst_key}B= not exists $retval{$dst_key}
ABresult
1X1
011
000
380 638 ▶ $is_overridable{$dst_key} = 0 unless $this_overridable;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_overridable 284 354 638
381 }
382 }
383 737 return \%retval;
384 # was a simple: return {map { $_->%* } reverse @_};
385 }
386  
387 19 ▶sub list_commands ($self, $children) {
Subroutine list_commands: called CC=5 SCAR=16.2
Pod documented
388 19 my $retval = '';
389 19 open my $fh, '>', \$retval;
390 19 for my $child ($children->@*) {
391 49 my $command = fetch_spec_for($self, $child);
392 49 my $help = $command->{help};
393 49 ▶ my @aliases = ($command->{supports} // [$child])->@*;
Condition50%1/2
$command->{"supports"} // [$child]
l!l
490
MC/DC0%0/1
$command->{"supports"} // [$child]
$command->{"supports"}
Truth table50%1/2
A
A= $command->{"supports"}
Aresult
11
00
394 49 ▶ next unless @aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless @aliases 0 49 49
395 49 printf {$fh} "%15s: %s\n", shift(@aliases), $help;
396 49 ▶ printf {$fh} "%15s (also as: %s)\n", '', join ', ', @aliases
Branch: 1 branch
BranchTrueFalseTotal
if @aliases 2 47 49
397 if @aliases;
398 } ## end for my $child ($children...)
399 19 close $fh;
400 19 return $retval;
401 } ## end sub list_commands
402  
403 109 ▶sub load_application ($application) {
Subroutine load_application: called CC=7 SCAR=19.6
Pod documented
404 109 ▶ return $application if 'HASH' eq ref $application;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $application 104 5 109
405  
406 5 my $text;
407 5 ▶ if ('SCALAR' eq ref $application) {
Branch: 1 branch
BranchTrueFalseTotal
if ('SCALAR' eq ref $application) { } 2 3 5
408 2 $text = $$application;
409 }
410 else {
411 my $fh =
412 'GLOB' eq ref $application
413 ? $application
414 3 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
'GLOB' eq ref $application ? : 1 2 3
415 2 ▶ open my $fh, '<:encoding(UTF-8)', $application
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $application or die "cannot open '${application}'\n" 2 0 2
416 or die "cannot open '$application'\n";
417 2 $fh;
418 };
419 3 local $/; # slurp mode
420 3 $text = <$fh>;
421 3 close $fh;
422 } ## end else [ if ('SCALAR' eq ref $application)]
423  
424 return eval {
425 5 require JSON::PP;
426 5 JSON::PP::decode_json($text);
427 5 ▶ } // eval { eval $text; } // die "cannot load application\n";
Condition60%3/5
(eval { do { require JSON::PP; JSON::PP::decode_json($text) } } // eval { do { eval $text } }) // die("cannot load application\n")
l!l
50
eval { do { require JSON::PP; JSON::PP::decode_json($text) } } // eval { do { eval $text } }
l!l&&r!l&&!r
230
MC/DC0%0/2
(eval { do { require JSON::PP; JSON::PP::decode_json($text) } } // eval { do { eval $text } }) // die("cannot load application\n")
eval { do { require JSON::PP; JSON::PP::decode_json($text) } } eval { do { eval $text } }
Truth table66%2/3
A // B
A= eval { do { require JSON::PP; JSON::PP::decode_json($text) } }B= eval { do { eval $text } }
ABresult
1X1
011
000
428 } ## end sub load_application ($application)
429  
430 186 ▶sub merger ($self, $spec = {}) {
Subroutine merger: called CC=4 SCAR=15.8
Pod documented
431 my $merger = $spec->{merge}
432 186 ▶ // $self->{application}{configuration}{merge} // \&hash_merge;
Condition40%2/5
($spec->{"merge"} // $self->{"application"}{"configuration"}{"merge"}) // \&App::Easer::V1::hash_merge
l!l
0186
$spec->{"merge"} // $self->{"application"}{"configuration"}{"merge"}
l!l&&r!l&&!r
00186
MC/DC0%0/2
($spec->{"merge"} // $self->{"application"}{"configuration"}{"merge"}) // \&App::Easer::V1::hash_merge
$spec->{"merge"} $self->{"application"}{"configuration"}{"merge"}
Truth table33%1/3
A // B
A= $spec->{"merge"}B= $self->{"application"}{"configuration"}{"merge"}
ABresult
1X1
011
000
433 186 return $self->{factory}->($merger, 'merge'); # "resolve"
434 }
435  
436 204 ▶sub env_namer ($self, $cspec) {
Subroutine env_namer: called CC=3 SCAR=12.0
Pod documented
437 my $namenv = $cspec->{namenv}
438 204 ▶ // $self->{application}{configuration}{namenv} // \&stock_NamEnv;
Condition40%2/5
($cspec->{"namenv"} // $self->{"application"}{"configuration"}{"namenv"}) // \&App::Easer::V1::stock_NamEnv
l!l
0204
$cspec->{"namenv"} // $self->{"application"}{"configuration"}{"namenv"}
l!l&&r!l&&!r
00204
MC/DC0%0/2
($cspec->{"namenv"} // $self->{"application"}{"configuration"}{"namenv"}) // \&App::Easer::V1::stock_NamEnv
$cspec->{"namenv"} $self->{"application"}{"configuration"}{"namenv"}
Truth table33%1/3
A // B
A= $cspec->{"namenv"}B= $self->{"application"}{"configuration"}{"namenv"}
ABresult
1X1
011
000
439 204 $namenv = $self->{factory}->($namenv, 'namenv'); # "resolve"
440 204 ▶ return sub ($ospec) { $namenv->($self, $cspec, $ospec) };
Subroutine __ANON__: called CC=1 SCAR=0.0
441 } ## end sub name_for_option ($o)
442  
443 182 ▶sub name_for_option ($o) {
Subroutine name_for_option: called CC=6 SCAR=28.7
Pod documented
444 182 ▶ return $o->{name} if defined $o->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"name"} 0 182 182
445 182 ▶ return $1 if defined $o->{getopt} && $o->{getopt} =~ m{\A(\w+)}mxs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w+)/msux 182 0 182
Condition33%1/3
defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w+)/msux
!ll&&!rl&&r
00182
MC/DC0%0/2
defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w+)/msux
defined $o->{"getopt"} $o->{"getopt"} =~ /\A(\w+)/msux
Truth table33%1/3
A and B
A= defined $o->{"getopt"}B= $o->{"getopt"} =~ /\A(\w+)/msux
ABresult
0X0
100
111
446 return lc $o->{environment}
447 0 ▶ if defined $o->{environment} && $o->{environment} ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"environment"} and $o->{"environment"} ne 1 0 0 0
Condition0%0/3
defined $o->{"environment"} and $o->{"environment"} ne 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $o->{"environment"} and $o->{"environment"} ne 1
defined $o->{"environment"} $o->{"environment"} ne 1
Truth table0%0/3
A and B
A= defined $o->{"environment"}B= $o->{"environment"} ne 1
ABresult
0X0
100
111
448 0 return '~~~';
449 } ## end sub name_for_option ($o)
450  
451 184 ▶sub params_validate ($self, $spec, $args) {
Subroutine params_validate: called CC=3 SCAR=16.5
Pod documented
452 my $validator = $spec->{validate}
453 184 ▶ // $self->{application}{configuration}{validate} // return;
Condition40%2/5
($spec->{"validate"} // $self->{"application"}{"configuration"}{"validate"}) // (return)
l!l
0184
$spec->{"validate"} // $self->{"application"}{"configuration"}{"validate"}
l!l&&r!l&&!r
00184
MC/DC0%0/2
($spec->{"validate"} // $self->{"application"}{"configuration"}{"validate"}) // (return)
$spec->{"validate"} $self->{"application"}{"configuration"}{"validate"}
Truth table33%1/3
A // B
A= $spec->{"validate"}B= $self->{"application"}{"configuration"}{"validate"}
ABresult
1X1
011
000
454 0 require Params::Validate;
455 0 Params::Validate::validate($self->{configs}[-1]->%*, $validator);
456 } ## end sub params_validate
457  
458 8 ▶sub print_commands ($self, $target) {
Subroutine print_commands: called CC=3 SCAR=11.0
Pod documented
459 8 my $command = fetch_spec_for($self, $target);
460 my $fh =
461 8 ▶ $self->{application}{configuration}{'help-on-stderr'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'application'}{'configuration'}{'help-on-stderr'} ? : 0 8 8
462 ? \*STDERR
463 : \*STDOUT;
464 8 ▶ if (my @children = get_children($self, $command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = get_children($self, $command)) { } 7 1 8
465 7 print {$fh} list_commands($self, \@children);
466 }
467 else {
468 1 print {$fh} "no sub-commands\n";
469 }
470 }
471  
472 21 ▶sub print_help ($self, $target) {
Subroutine print_help: called CC=15 SCAR=27.1
Pod documented
473 21 my $command = fetch_spec_for($self, $target);
474 21 my $enamr = env_namer($self, $command);
475 my $fh =
476 21 ▶ $self->{application}{configuration}{'help-on-stderr'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'application'}{'configuration'}{'help-on-stderr'} ? : 0 21 21
477 ? \*STDERR
478 : \*STDOUT;
479  
480 21 print {$fh} $command->{help}, "\n\n";
481  
482 21 ▶ if (defined(my $description = $command->{description})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $description = $command->{"description"})) 20 1 21
483 20 $description =~ s{\A\s+|\s+\z}{}gmxs; # trim
484 20 $description =~ s{^}{ }gmxs; # add some indentation
485 20 print {$fh} "Description:\n$description\n\n";
486 }
487  
488 13 printf {$fh} "Can be called as: %s\n\n", join ', ',
489 $command->{supports}->@*
490 21 ▶ if $command->{supports};
Branch: 1 branch
BranchTrueFalseTotal
if $command->{"supports"} 13 8 21
491  
492 21 ▶ my $options = $command->{options} // [];
Condition100%2/2
$command->{"options"} // []
l!l
192
MC/DC100%1/1
$command->{"options"} // []
$command->{"options"}
Truth table100%2/2
A
A= $command->{"options"}
Aresult
11
00
493 21 ▶ if ($options->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$options) { } 15 6 21
494 15 print {$fh} "Options:\n";
495 15 my $n = 0; # count the option
496 15 for my $option ($options->@*) {
497 25 ▶ print {$fh} "\n" if $n++;
Branch: 1 branch
BranchTrueFalseTotal
if $n++ 10 15 25
498  
499 25 ▶ printf {$fh} "%15s: %s\n", name_for_option($option),
Condition50%1/2
$option->{"help"} // ""
l!l
025
MC/DC0%0/1
$option->{"help"} // ""
$option->{"help"}
Truth table50%1/2
A
A= $option->{"help"}
Aresult
11
00
500 25 $option->{help} // '';
501  
502 25 ▶ if (exists $option->{getopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"getopt"}) 25 0 25
503 25 my @lines = commandline_help($option->{getopt});
504 25 printf {$fh} "%15s command-line: %s\n", '', shift(@lines);
505 25 printf {$fh} "%15s %s\n", '', $_ for @lines;
506 }
507  
508 25 ▶ if (defined(my $env_name = $enamr->($option))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $env_name = &$enamr($option))) 16 9 25
509 16 printf {$fh} "%15s environment : %s\n", '', $env_name;
510 }
511  
512 8 ▶ printf {$fh} "%15s default : %s\n", '',
Condition50%1/2
$option->{"default"} // "*undef*"
l!l
80
MC/DC0%0/1
$option->{"default"} // "*undef*"
$option->{"default"}
Truth table50%1/2
A
A= $option->{"default"}
Aresult
11
00
513 $option->{default} // '*undef*'
514 25 ▶ if exists $option->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option->{"default"} 8 17 25
515 } ## end for my $option ($options...)
516 15 print {$fh} "\n";
517 } ## end if ($options->@*)
518 else {
519 6 print {$fh} "This command has no options.\n\n";
520 }
521  
522 21 ▶ if (my @children = get_children($self, $command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = get_children($self, $command)) { } 12 9 21
523 12 print {$fh} "Sub commands:\n", list_commands($self, \@children),
524 "\n";
525 }
526 else {
527 9 print {$fh} "no sub-commands\n\n";
528 }
529 }
530  
531 403 ▶sub stock_SpecFromHash ($s, $cmd) {
Subroutine stock_SpecFromHash: called CC=3 SCAR=11.0
Pod documented
532 403 ▶ return $cmd if ref($cmd) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cmd eq "HASH" 13 390 403
533 390 ▶ return $s->{application}{commands}{$cmd} // undef;
Condition100%2/2
$s->{"application"}{"commands"}{$cmd} // undef
l!l
3864
MC/DC100%1/1
$s->{"application"}{"commands"}{$cmd} // undef
$s->{"application"}{"commands"}{$cmd}
Truth table100%2/2
A
A= $s->{"application"}{"commands"}{$cmd}
Aresult
11
00
534 }
535  
536 151 ▶sub stock_SpecFromHashOrModule ($s, $cmd) {
Subroutine stock_SpecFromHashOrModule: called CC=3 SCAR=11.7
Pod documented
537 151 ▶ return $cmd if ref($cmd) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cmd eq "HASH" 0 151 151
538 return $s->{application}{commands}{$cmd}
539 151 ▶ //= $s->{factory}->($cmd, 'spec')->();
Condition66%2/3
$s->{"application"}{"commands"}{$cmd} //= $s->{"factory"}->($cmd, "spec")->()
l!l&&r!l&&!r
14650
MC/DC0%0/2
$s->{"application"}{"commands"}{$cmd} //= $s->{"factory"}->($cmd, "spec")->()
$s->{"application"}{"commands"}{$cmd} $s->{"factory"}->($cmd, "spec")->()
Truth table66%2/3
A //= B
A= $s->{"application"}{"commands"}{$cmd}B= $s->{"factory"}->($cmd, "spec")->()
ABresult
1X1
011
000
540 }
541  
542 554 ▶sub fetch_spec_for ($self, $command) {
Subroutine fetch_spec_for: called CC=2 SCAR=6.9
Pod documented
543 my $fetcher = $self->{application}{configuration}{specfetch}
544 554 ▶ // \&stock_SpecFromHash;
Condition100%2/2
$self->{"application"}{"configuration"}{"specfetch"} // \&App::Easer::V1::stock_SpecFromHash
l!l
151403
MC/DC100%1/1
$self->{"application"}{"configuration"}{"specfetch"} // \&App::Easer::V1::stock_SpecFromHash
$self->{"application"}{"configuration"}{"specfetch"}
Truth table100%2/2
A
A= $self->{"application"}{"configuration"}{"specfetch"}
Aresult
11
00
545 554 return $self->{factory}->($fetcher, 'specfetch')->($self, $command);
546 }
547  
548 103 ▶sub run ($application, $args) {
Subroutine run: called CC=5 SCAR=16.2
Pod documented
549 103 $application = add_auto_commands(load_application($application));
550 my $self = {
551 application => $application,
552 configs => [],
553 factory => generate_factory($application->{factory} // {}),
554 helpers => {
555 'print-commands' => \&print_commands,
556 'print-help' => \&print_help,
557 },
558 103 ▶ trail => [['MAIN', $application->{commands}{MAIN}{name}]],
Condition50%1/2
$application->{"factory"} // {}
l!l
0103
MC/DC0%0/1
$application->{"factory"} // {}
$application->{"factory"}
Truth table50%1/2
A
A= $application->{"factory"}
Aresult
11
00
559 };
560  
561 103 while ('necessary') {
562 188 my $command = $self->{trail}[-1][0];
563 188 ▶ my $spec = fetch_spec_for($self, $command)
Branch: 1 branch
BranchTrueFalseTotal
my $spec = fetch_spec_for($self, $command) or die "no definition for '${command}'\n" 184 4 188
564 or die "no definition for '$command'\n";
565  
566 184 $args = collect_options($self, $spec, $args);
567 184 validate_configuration($self, $spec, $args);
568 184 commit_configuration($self, $spec, $args);
569  
570 184 ▶ my ($subc, $alias) = fetch_subcommand($self, $spec, $args) or last;
Branch: 1 branch
BranchTrueFalseTotal
() = fetch_subcommand($self, $spec, $args) or last 85 93 178
571 85 push $self->{trail}->@*, [$subc, $alias];
572 } ## end while ('necessary')
573  
574 93 ▶ return execute($self, $args) // 0;
Condition50%1/2
execute($self, $args) // 0
l!l
930
MC/DC0%0/1
execute($self, $args) // 0
execute($self, $args)
Truth table50%1/2
A
A= execute($self, $args)
Aresult
11
00
575 } ## end sub run
576  
577 2 ▶sub slurp ($file, $mode = '<:encoding(UTF-8)') {
Subroutine slurp: called CC=3 SCAR=11.1
Pod documented
578 2 ▶ open my $fh, $mode, $file or die "open('$file'): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $file or die "open('${file}'): $!\n" 2 0 2
579 2 local $/;
580 2 return <$fh>;
581 }
582  
583 183 ▶sub sources ($self, $spec, $args) {
Subroutine sources: called CC=4 SCAR=13.9
Pod documented
584 my $s = $spec->{sources}
585 // $self->{application}{configuration}{sources}
586 183 ▶ // \&stock_DefaultSources;
Condition100%5/5
($spec->{"sources"} // $self->{"application"}{"configuration"}{"sources"}) // \&App::Easer::V1::stock_DefaultSources
l!l
5178
$spec->{"sources"} // $self->{"application"}{"configuration"}{"sources"}
l!l&&r!l&&!r
32178
MC/DC100%2/2
($spec->{"sources"} // $self->{"application"}{"configuration"}{"sources"}) // \&App::Easer::V1::stock_DefaultSources
$spec->{"sources"} $self->{"application"}{"configuration"}{"sources"}
Truth table100%3/3
A // B
A= $spec->{"sources"}B= $self->{"application"}{"configuration"}{"sources"}
ABresult
1X1
011
000
587 183 ▶ $s = $self->{factory}->($s, 'sources')->() if 'ARRAY' ne ref $s;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $s 181 2 183
588 183 return $s->@*;
589 } ## end sub sources
590  
591 183 ▶sub stock_CmdLine ($self, $spec, $args) {
Subroutine stock_CmdLine: called CC=8 SCAR=23.2
Pod documented
592 183 my @args = $args->@*;
593 183 ▶ my $goc = $spec->{'getopt-config'}
Condition33%1/3
$spec->{"getopt-config"} // default_getopt_config($self, $spec)
l!l&&r!l&&!r
01830
MC/DC0%0/2
$spec->{"getopt-config"} // default_getopt_config($self, $spec)
$spec->{"getopt-config"} default_getopt_config($self, $spec)
Truth table33%1/3
A // B
A= $spec->{"getopt-config"}B= default_getopt_config($self, $spec)
ABresult
1X1
011
000
594 // default_getopt_config($self, $spec);
595 183 require Getopt::Long;
596 183 Getopt::Long::Configure('default', $goc->@*);
597  
598 183 my %option_for;
599 my @specs = map {
600 238 my $go = $_->{getopt};
601 ref($go) eq 'ARRAY'
602 0 ▶ ? ($go->[0] => sub { $go->[1]->(\%option_for, @_) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
603 238 ▶ : $go;
Branch: 1 branch
BranchTrueFalseTotal
ref $go eq 'ARRAY' ? : 0 238 238
604 }
605 183 ▶ grep { exists $_->{getopt} } ($spec->{options} // [])->@*;
Condition100%2/2
$spec->{"options"} // []
l!l
15033
MC/DC100%1/1
$spec->{"options"} // []
$spec->{"options"}
Truth table100%2/2
A
A= $spec->{"options"}
Aresult
11
00
606 183 ▶ Getopt::Long::GetOptionsFromArray(\@args, \%option_for, @specs)
Branch: 1 branch
BranchTrueFalseTotal
Getopt::Long::GetOptionsFromArray(\@args, \%option_for, @specs) or die "bailing out\n" 183 0 183
607 or die "bailing out\n";
608  
609 # Check if we want to forbid the residual @args to start with a '-'
610 183 my $strict = !$spec->{'allow-residual-options'};
611 183 ▶ if ($strict && @args && $args[0] =~ m{\A -}mxs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict and @args and $args[0] =~ /\A -/msux) 0 183 183
Condition66%4/6
$strict and @args and $args[0] =~ /\A -/msux
!ll&&!rl&&r
791040
$strict and @args
!ll&&!rl&&r
079104
MC/DC0%0/3
$strict and @args and $args[0] =~ /\A -/msux
$strict @args $args[0] =~ /\A -/msux
Truth table50%2/4
A and B and C
A= $strictB= @argsC= $args[0] =~ /\A -/msux
ABCresult
0XX0
10X0
1100
1111
612 0 Getopt::Long::Configure('default', 'gnu_getopt');
613 0 Getopt::Long::GetOptionsFromArray(\@args, {});
614 0 die "bailing out\n";
615 }
616  
617 183 return (\%option_for, \@args);
618 } ## end sub stock_CmdLine
619  
620 3 ▶sub stock_JsonFileFromConfig ($self, $spec, $args) {
Subroutine stock_JsonFileFromConfig: called CC=3 SCAR=11.0
Pod documented
621 3 ▶ my $key = $spec->{'config-option'} // 'config';
Condition50%1/2
$spec->{"config-option"} // "config"
l!l
03
MC/DC0%0/1
$spec->{"config-option"} // "config"
$spec->{"config-option"}
Truth table50%1/2
A
A= $spec->{"config-option"}
Aresult
11
00
622 3 ▶ return {} if !exists($spec->{config}{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"config"}{$key} 2 1 3
623 1 require JSON::PP;
624 1 return JSON::PP::decode_json(slurp($spec->{config}{$key}));
625 } ## end sub stock_JsonFileFromConfig
626  
627 3 ▶sub stock_JsonFiles ($self, $spec, @ignore) {
Subroutine stock_JsonFiles: called CC=2 SCAR=6.9
Pod documented
628 return merger($self, $spec)->(
629 map {
630 1 require JSON::PP;
631 1 JSON::PP::decode_json(slurp($_));
632 }
633 3 ▶ grep { -e $_ } ($spec->{'config-files'} // [])->@*
Condition100%2/2
$spec->{"config-files"} // []
l!l
12
MC/DC100%1/1
$spec->{"config-files"} // []
$spec->{"config-files"}
Truth table100%2/2
A
A= $spec->{"config-files"}
Aresult
11
00
634 );
635 } ## end sub stock_JsonFiles
636  
637 181 ▶sub stock_Default ($self, $spec, @ignore) {
Subroutine stock_Default: called CC=2 SCAR=6.9
Pod documented
638 return {
639 87 map { '//=' . name_for_option($_) => $_->{default} }
640 181 ▶ grep { exists $_->{default} } ($spec->{options} // [])->@*
Condition100%2/2
$spec->{"options"} // []
l!l
14833
MC/DC100%1/1
$spec->{"options"} // []
$spec->{"options"}
Truth table100%2/2
A
A= $spec->{"options"}
Aresult
11
00
641 };
642 } ## end sub stock_Default
643  
644 183 ▶sub stock_Environment ($self, $spec, @ignore) {
Subroutine stock_Environment: called CC=4 SCAR=13.9
Pod documented
645 183 my $enamr = env_namer($self, $spec);
646 return {
647 map {
648 238 my $en = $enamr->($_); # name of environment variable
649 defined($en) && exists($ENV{$en})
650 238 ▶ ? (name_for_option($_) => $ENV{$en}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $en && exists $ENV{$en} ? : 34 204 238
Condition100%5/5
defined $en and exists $ENV{$en}
!ll&&!rl&&r
6014434
$spec->{"options"} // []
l!l
15033
MC/DC100%2/2
defined $en and exists $ENV{$en}
defined $en exists $ENV{$en}
MC/DC100%1/1
$spec->{"options"} // []
$spec->{"options"}
Truth table100%3/3
A and B
A= defined $enB= exists $ENV{$en}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $spec->{"options"}
Aresult
11
00
651 183 } ($spec->{options} // [])->@*
652 };
653 } ## end sub stock_Environment
654  
655 263 ▶sub stock_NamEnv ($self, $cspec, $ospec) {
Subroutine stock_NamEnv: called CC=8 SCAR=20.8
Pod documented
656 263 my $aek = 'auto-environment';
657 my $autoenv = exists $cspec->{$aek} ? $cspec->{$aek}
658 263 ▶ : $self->{application}{configuration}{$aek} // undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $cspec->{$aek} ? : 18 245 263
Condition100%2/2
$self->{"application"}{"configuration"}{$aek} // undef
l!l
18227
MC/DC100%1/1
$self->{"application"}{"configuration"}{$aek} // undef
$self->{"application"}{"configuration"}{$aek}
Truth table100%2/2
A
A= $self->{"application"}{"configuration"}{$aek}
Aresult
11
00
659 my $env = exists $ospec->{environment} ? $ospec->{environment}
660 263 ▶ : $autoenv ? 1 : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $ospec->{'environment'} ? : 176 87 263
$autoenv ? : 18 69 87
661 263 ▶ return $env unless ($env // '') eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless ($env // "") eq 1 227 36 263
Condition100%2/2
$env // ""
l!l
19469
MC/DC100%1/1
$env // ""
$env
Truth table100%2/2
A
A= $env
Aresult
11
00
662 36 ▶ my $appname = $self->{application}{configuration}{name} // '';
Condition50%1/2
$self->{"application"}{"configuration"}{"name"} // ""
l!l
360
MC/DC0%0/1
$self->{"application"}{"configuration"}{"name"} // ""
$self->{"application"}{"configuration"}{"name"}
Truth table50%1/2
A
A= $self->{"application"}{"configuration"}{"name"}
Aresult
11
00
663 36 my $optname = name_for_option($ospec);
664 36 return uc(join '_', $appname, $optname);
665 }
666  
667 181 ▶sub stock_Parent ($self, $spec, @ignore) { $self->{configs}[-1] // {} }
Subroutine stock_Parent: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$self->{"configs"}[-1] // {}
l!l
81100
MC/DC100%1/1
$self->{"configs"}[-1] // {}
$self->{"configs"}[-1]
Truth table100%2/2
A
A= $self->{"configs"}[-1]
Aresult
11
00
668  
669 7 ▶sub stock_commands ($self, $config, $args) {
Subroutine stock_commands: called CC=2 SCAR=7.0
Pod documented
670 7 ▶ die "this command does not support arguments\n" if $args->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 7 7
671 7 my $target = get_descendant($self, $self->{trail}[-2][0], $args);
672 7 print_commands($self, $target);
673 7 return 0;
674 } ## end sub stock_commands
675  
676 2166 ▶sub stock_factory ($executable, $default_subname = '', $opts = {}) {
Subroutine stock_factory: called CC=7 SCAR=19.5
Pod documented
677 90 ▶ state $factory = sub ($executable, $default_subname) {
Subroutine __ANON__: called CC=11 SCAR=24.2
678 my @prefixes =
679 !defined $opts->{prefixes} ? ()
680 : 'ARRAY' eq ref $opts->{prefixes} ? ($opts->{prefixes}->@*)
681 90 ▶ : ($opts->{prefixes});
Branch: 2 branches
BranchTrueFalseTotal
!defined($opts->{'prefixes'}) ? : 81 9 90
'ARRAY' eq ref $opts->{'prefixes'} ? : 0 9 9
682 90 push @prefixes, {'+' => 'App::Easer::V1#stock_'};
683 SEARCH:
684 90 for my $expansion_for (@prefixes) {
685 96 for my $p (keys $expansion_for->%*) {
686 111 ▶ next if $p ne substr $executable, 0, length $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p ne substr($executable, 0, length $p) 37 74 111
687 74 substr $executable, 0, length $p, $expansion_for->{$p};
688 74 last SEARCH;
689 }
690 } ## end SEARCH: for my $expansion_for (...)
691  
692 # if it *still* "starts" with '=', it's "inline" Perl code
693 90 ▶ return eval $executable if $executable =~ s{\A \s* = \s* }{}mxs;
Branch: 1 branch
BranchTrueFalseTotal
if $executable =~ s/\A \s* = \s* //msux 1 89 90
694  
695 89 my ($package, $sname) = split m{\#}mxs, $executable;
696 89 ▶ $sname = $default_subname unless defined $sname && length $sname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sname and length $sname 10 79 89
Condition66%2/3
defined $sname and length $sname
!ll&&!rl&&r
10079
MC/DC50%1/2
defined $sname and length $sname
defined $sname length $sname
Truth table66%2/3
A and B
A= defined $snameB= length $sname
ABresult
0X0
100
111
697  
698 # first try to see if the sub is already available in $package
699 89 ▶ if (my $s = $package->can($sname)) { return $s }
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $package->can($sname)) 83 6 89
700  
701 # otherwise force loading of $package and retry
702 6 (my $path = "$package.pm") =~ s{::}{/}gmxs;
703 6 require $path;
704 4 ▶ if (my $s = $package->can($sname)) { return $s }
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $package->can($sname)) 4 0 4
705  
706 0 die "no '$sname' in '$package'\n";
707 2166 };
708 2166 state $cache = {};
709  
710 2166 my $args;
711 2166 ▶ ($executable, $args) = ($executable->{executable}, $executable)
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $executable 1 2165 2166
712 if 'HASH' eq ref $executable;
713 2166 ▶ $executable = $cache->{$executable . ' ' . $default_subname} //=
Branch: 1 branch
BranchTrueFalseTotal
if "CODE" ne ref $executable 929 1237 2166
Condition100%3/3
$$cache{$executable . ' ' . $default_subname} //= &$factory($executable, $default_subname)
l!l&&r!l&&!r
839882
MC/DC100%2/2
$$cache{$executable . ' ' . $default_subname} //= &$factory($executable, $default_subname)
$$cache{$executable . ' ' . $default_subname} &$factory($executable, $default_subname)
Truth table100%3/3
A //= B
A= $$cache{$executable . ' ' . $default_subname}B= &$factory($executable, $default_subname)
ABresult
1X1
011
000
714 $factory->($executable, $default_subname)
715 if 'CODE' ne ref $executable;
716 2164 ▶ return $executable unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 2163 1 2164
717 1 ▶ return sub { $executable->($args, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
718 } ## end sub stock_factory
719  
720 20 ▶sub stock_help ($self, $config, $args) {
Subroutine stock_help: called CC=1 SCAR=0.0
Pod documented
721 20 print_help($self, get_descendant($self, $self->{trail}[-2][0], $args));
722 20 return 0;
723 } ## end sub stock_help
724  
725 178 ▶sub stock_DefaultSources { [qw< +Default +CmdLine +Environment +Parent >] }
Subroutine stock_DefaultSources: called CC=1 SCAR=0.0
Pod documented
726  
727 3 ▶sub stock_SourcesWithFiles {
Subroutine stock_SourcesWithFiles: called CC=1 SCAR=0.0
Pod documented
728 [
729 3 qw< +Default +CmdLine +Environment +Parent
730 +JsonFileFromConfig +JsonFiles
731 >
732 ]
733 } ## end sub stock_SourcesWithFiles
734  
735 184 ▶sub validate_configuration ($self, $spec, $args) {
Subroutine validate_configuration: called CC=5 SCAR=19.7
Pod documented
736 184 my $from_spec = $spec->{validate};
737 184 my $from_self = $self->{application}{configuration}{validate};
738 184 my $validator;
739 184 ▶ if (defined $from_spec && 'HASH' ne ref $from_spec) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $from_spec and 'HASH' ne ref $from_spec) { } 0 184 184
elsif (defined $from_self and 'HASH' ne ref $from_self) { } 0 184 184
Condition33%1/3
defined $from_spec and "HASH" ne ref $from_spec
!ll&&!rl&&r
18400
MC/DC0%0/2
defined $from_spec and "HASH" ne ref $from_spec
defined $from_spec "HASH" ne ref $from_spec
Truth table33%1/3
A and B
A= defined $from_specB= "HASH" ne ref $from_spec
ABresult
0X0
100
111
740 0 $validator = $self->{factory}->($from_spec, 'validate');
741 }
742 ▶ elsif (defined $from_self && 'HASH' ne ref $from_self) {
Condition33%1/3
defined $from_self and "HASH" ne ref $from_self
!ll&&!rl&&r
18400
MC/DC0%0/2
defined $from_self and "HASH" ne ref $from_self
defined $from_self "HASH" ne ref $from_self
Truth table33%1/3
A and B
A= defined $from_selfB= "HASH" ne ref $from_self
ABresult
0X0
100
111
743 0 $validator = $self->{factory}->($from_self, 'validate');
744 }
745 else { # use stock one
746 184 $validator = \&params_validate;
747 }
748 184 $validator->($self, $spec, $args);
749 } ## end sub validate_configuration
750  
751 exit run(
752 $ENV{APPEASER} // {
753 commands => {
754 MAIN => {
755 name => 'main app',
756 help => 'this is the main app',
757 description => 'Yes, this really is the main app',
758 options => [
759 {
760 name => 'foo',
761 description => 'option foo!',
762 getopt => 'foo|f=s',
763 environment => 'FOO',
764 default => 'bar',
765 },
766 ],
767 0 ▶ execute => sub ($global, $conf, $args) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
768 0 my $foo = $conf->{foo};
769 0 say "Hello, $foo!";
770 0 return 0;
771 },
772 22 ▶ 'default-child' => '', # run execute by default
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 22 22
Condition0%0/2
$ENV{"APPEASER"} // {"commands", {"MAIN", {"name", "main app", "help", "this is the main app", "description", "Yes, this really is the main app", "options", [{"name", "foo", "description", "option foo!", "getopt", "foo|f=s", "environment", "FOO", "default", "bar"}], "execute", sub ($global, $conf, $args) { my $foo = $conf->{"foo"}; say "Hello, $foo!"; return 0; } , "default-child", ""}}}
l!l
00
MC/DC0%0/1
$ENV{"APPEASER"} // {"commands", {"MAIN", {"name", "main app", "help", "this is the main app", "description", "Yes, this really is the main app", "options", [{"name", "foo", "description", "option foo!", "getopt", "foo|f=s", "environment", "FOO", "default", "bar"}], "execute", sub ($global, $conf, $args) { my $foo = $conf->{"foo"}; say "Hello, $foo!"; return 0; } , "default-child", ""}}}
$ENV{"APPEASER"}
Truth table0%0/2
A
A= $ENV{"APPEASER"}
Aresult
11
00
773 },
774 },
775 },
776 [@ARGV]
777 ) unless caller;
778  
779 22 1;
↑ Summary Easer/V2.pm »