Easer/V2.pm

filter: Statementstmt 83.0% 588 / 708 Branchbran 62.3% 248 / 398 Conditioncond 57.6% 109 / 189 MC/DCmcdc 33.6% 35 / 104 Subroutinesub 87.4% 125 / 143 Podpod 100% 4 / 4 totaltotal 71.7% 1109 / 1546 CC 298 SCAR 25.0
CC
298
Cov
73%
list_commands_for
90.0
validate
44.4
__commandline_help
30.3
CRAP 12.1
« Easer/V1.pm ↑ Summary Easer/ConfigHash.pm »
1 package App::Easer::V2;
2 27 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 { our $VERSION = '2.016' }
6 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 our @EXPORT_OK = qw< appeaser_api d dd run >;
10  
11 # repeated stuff to ease direct usage and fatpack-like inclusion
12 2 ▶sub appeaser_api { __PACKAGE__ =~ s{.*::}{}rmxs }
Subroutine appeaser_api: called CC=1 SCAR=0.0
Pod documented
13 0 ▶sub d { warn dd(@_) }
Subroutine d: not called CC=1 SCAR=6.9
Pod documented
14  
15 0 ▶sub dd (@stuff) {
Subroutine dd: not called CC=4 SCAR=24.6
Pod documented
16 27 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 require Data::Dumper;
18 0 local $Data::Dumper::Indent = 1;
19 0 local $Data::Dumper::Sortkeys = 1;
20 0 ▶ Data::Dumper::Dumper(
Branch: 2 branches
BranchTrueFalseTotal
@stuff == 0 ? : 0 0 0
ref $stuff[0] || @stuff % 2 ? : 0 0 0
Condition0%0/3
ref $stuff[0] or @stuff % 2
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $stuff[0] or @stuff % 2
ref $stuff[0] @stuff % 2
Truth table0%0/3
A or B
A= ref $stuff[0]B= @stuff % 2
ABresult
1X1
011
000
21 @stuff == 0 ? []
22 : (ref($stuff[0]) || @stuff % 2) ? \@stuff
23 : {@stuff}
24 );
25 } ## end sub dd (@stuff)
26  
27 120 ▶sub run ($app, @args) {
Subroutine run: called CC=3 SCAR=13.1
Pod documented
28 120 my $class = 'App::Easer::V2::Command';
29 120 ▶ my $instance =
Branch: 2 branches
BranchTrueFalseTotal
ref $app eq 'HASH' ? : 120 0 120
ref $app eq 'ARRAY' ? : 0 0 0
30 ref($app) eq 'HASH' ? $class->new($app)
31 : ref($app) eq 'ARRAY' ? $class->instantiate($app->@*)
32 : $class->instantiate($app);
33 120 return $instance->run(@args);
34 } ## end sub run
35  
36 8 ▶sub import ($package, @args) {
Subroutine import: called CC=10 SCAR=24.5
37 8 my $target = caller;
38 8 my @args_for_exporter;
39 8 our %registered;
40  
41 8 my $parent_class = 'App::Easer::V2::Command';
42 8 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 14 8 22
43 14 my $request = shift @args;
44 14 ▶ if ($request eq '-command') {
Branch: 5 branches
BranchTrueFalseTotal
if ($request eq '-command') { } 6 8 14
elsif ($request eq '-inherit') { } 0 8 8
elsif ($request eq '-register') { } 0 8 8
elsif ($request eq '-spec') { } 4 4 8
elsif ($request eq '-parent') { } 0 4 4
45 6 $registered{$target} = 1;
46 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 push @{$target . '::ISA'}, $parent_class;
48 }
49 elsif ($request eq '-inherit') {
50 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 0 push @{$target . '::ISA'}, $parent_class;
52 }
53 elsif ($request eq '-register') {
54 0 $registered{$target} = 1;
55 }
56 elsif ($request eq '-spec') {
57 4 ▶ Carp::croak "no specification provided"
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 4 4
58 unless @args;
59 4 ▶ Carp::croak "invalid specification provided"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[0] eq "HASH" 0 4 4
60 unless ref($args[0]) eq 'HASH';
61 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 27 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 ${$target . '::app_easer_spec'} = shift @args;
64 } ## end elsif ($request eq '-spec')
65 elsif ($request eq '-parent') { # 2024-08-28 EXPERIMENTAL
66 0 ▶ Carp::croak "no parent class provided"
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
67 unless @args;
68 0 $parent_class = shift @args;
69  
70 # make sure it's required
71 0 App::Easer::V2::Command->load_module($parent_class);
72 }
73 4 else { push @args_for_exporter, $request }
74 } ## end while (@args)
75 8 $package->export_to_level(1, $package, @args_for_exporter);
76 } ## end sub import
77  
78 package App::Easer::V2::Command;
79 27 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 27 ▶use List::Util 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 27 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 27 ▶use Scalar::Util qw< weaken >;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 # some stuff can be managed via a hash reference kept in a "slot",
85 # allowing for overriding should be easy either with re-defining the
86 # "slot" method, or overriding the sub-method relying on it. The name of
87 # the slot is the same as the name of the actual package that $self is
88 # blessed into.
89 13984 ▶sub slot ($self) { return $self->{blessed($self)} //= {} }
Subroutine slot: called CC=2 SCAR=7.6
Condition50%1/2
$$self{builtin::blessed($self)} //= {}
l!l
139840
MC/DC0%0/1
$$self{builtin::blessed($self)} //= {}
$$self{builtin::blessed($self)}
Truth table50%1/2
A
A= $$self{builtin::blessed($self)}
Aresult
11
00
90  
91 # This is a poor man's way to easily define attributes in a single line
92 # Corinna will be a blessing eventually
93 12313 ▶sub _rwn ($self, $name, @newval) {
Subroutine _rwn: called CC=2 SCAR=6.9
94 12313 my $vref = \$self->slot->{$name};
95 12313 ▶ $$vref = $newval[0] if @newval;
Branch: 1 branch
BranchTrueFalseTotal
if @newval 2604 9709 12313
96 12313 return $$vref;
97 }
98  
99 5465 ▶sub _rw ($s, @n) { $s->_rwn((caller(1))[3] =~ s{.*::}{}rmxs, @n) }
Subroutine _rw: called CC=1 SCAR=0.0
100  
101 4173 ▶sub _rwa ($self, @n) {
Subroutine _rwa: called CC=2 SCAR=7.1
102 4173 my $aref = $self->_rwn((caller(1))[3] =~ s{.*::}{}rmxs, @n);
103 4173 ▶ Carp::confess() unless defined $aref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aref 0 4173 4173
104 4173 return $aref->@*;
105 }
106  
107 182 ▶sub _rwad ($self, @n) {
Subroutine _rwad: called CC=3 SCAR=12.0
108 182 ▶ my $aref = $self->_rwn((caller(1))[3] =~ s{.*::}{}rmxs, @n) // [];
Condition50%1/2
$self->_rwn((caller 1)[3] =~ s/.*:://mrsux, @n) // []
l!l
1820
MC/DC0%0/1
$self->_rwn((caller 1)[3] =~ s/.*:://mrsux, @n) // []
$self->_rwn((caller 1)[3] =~ s/.*:://mrsux, @n)
Truth table50%1/2
A
A= $self->_rwn((caller 1)[3] =~ s/.*:://mrsux, @n)
Aresult
11
00
109 182 ▶ return wantarray ? $aref->@* : [$aref->@*];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 182 0 182
110 }
111  
112 326 ▶sub _rw_prd ($self, @n) {
Subroutine _rw_prd: called CC=4 SCAR=14.0
113 326 my $slot = $self->slot;
114 326 my $name = (caller(1))[3] =~ s{.*::}{}rmxs;
115 326 ▶ if (@n) {
Branch: 2 branches
BranchTrueFalseTotal
if (@n) { } 0 326 326
elsif (ref(my $ref_to_default = $slot->{$name})) { } 166 160 326
116 0 $slot->{$name} = $n[0];
117 }
118 elsif (ref(my $ref_to_default = $slot->{$name})) {
119 166 my $parent = $self->parent;
120 166 ▶ $slot->{$name} = $parent ? $parent->$name : $$ref_to_default;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 54 112 166
121 }
122 326 return $slot->{$name};
123 }
124  
125 # these "attributes" would point to stuff that is normally "scalar" and
126 # used as specification overall. It can be overridden but probably it's
127 # just easier to stick in a hash inside the slot. We don't want to put
128 # executables here, though - overriding should be the guiding principle
129 # in this case.
130 182 ▶sub aliases ($self, @r) {
Subroutine aliases: called CC=3 SCAR=11.3
131 182 ▶ if (my @aliases = $self->_rwad(@r)) { return @aliases }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@aliases) = $self->_rwad(@r)) 111 71 182
132 71 ▶ if (defined(my $name = $self->_rwn('name'))) { return $name }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $self->_rwn("name"))) 71 0 71
133 0 return;
134 }
135 354 ▶sub allow_residual_options ($self, @r) { $self->_rw(@r) }
Subroutine allow_residual_options: called CC=1 SCAR=0.0
136 235 ▶sub auto_environment ($self, @r) { $self->_rw(@r) }
Subroutine auto_environment: called CC=1 SCAR=0.0
137 257 ▶sub call_name ($self, @r) { $self->_rw(@r) }
Subroutine call_name: called CC=1 SCAR=0.0
138 602 ▶sub children ($self, @r) { $self->_rwa(@r) }
Subroutine children: called CC=1 SCAR=0.0
139 1204 ▶sub children_prefixes ($self, @r) { $self->_rwa(@r) }
Subroutine children_prefixes: called CC=1 SCAR=0.0
140 80 ▶sub default_child ($self, @r) { $self->_rw(@r) }
Subroutine default_child: called CC=1 SCAR=0.0
141 31 ▶sub description ($self, @r) { $self->_rw(@r) }
Subroutine description: called CC=1 SCAR=0.0
142 111 ▶sub environment_prefix ($self, @r) { $self->_rw(@r) }
Subroutine environment_prefix: called CC=1 SCAR=0.0
143 123 ▶sub execution_reason ($self, @r) { $self->_rw(@r) }
Subroutine execution_reason: called CC=1 SCAR=0.0
144 5 ▶sub fallback_to ($self, @r) { $self->_rw(@r) }
Subroutine fallback_to: called CC=1 SCAR=0.0
145 102 ▶sub final_commit_stack ($self, @r) { $self->_rwa(@r) }
Subroutine final_commit_stack: called CC=1 SCAR=0.0
146 602 ▶sub force_auto_children ($self, @r) { $self->_rw(@r) }
Subroutine force_auto_children: called CC=1 SCAR=0.0
147 248 ▶sub hashy_class ($self, @r) { $self->_rw(@r) }
Subroutine hashy_class: called CC=1 SCAR=0.0
148 43 ▶sub help ($self, @r) { $self->_rw(@r) }
Subroutine help: called CC=1 SCAR=0.0
149 40 ▶sub help_channel ($slf, @r) { $slf->_rw(@r) }
Subroutine help_channel: called CC=1 SCAR=0.0
150 91 ▶sub name ($s, @r) { $s->_rw(@r) // ($s->aliases)[0] // '**no name**' }
Subroutine name: called CC=3 SCAR=12.0
Condition60%3/5
($s->_rw(@r) // ($s->aliases)[0]) // "**no name**"
l!l
910
$s->_rw(@r) // ($s->aliases)[0]
l!l&&r!l&&!r
69220
MC/DC0%0/2
($s->_rw(@r) // ($s->aliases)[0]) // "**no name**"
$s->_rw(@r) ($s->aliases)[0]
Truth table66%2/3
A // B
A= $s->_rw(@r)B= ($s->aliases)[0]
ABresult
1X1
011
000
151 34 ▶sub options_help ($s, @r) { $s->_rw(@r) }
Subroutine options_help: called CC=1 SCAR=0.0
152 217 ▶sub params_validate ($self, @r) { $self->_rw(@r) }
Subroutine params_validate: called CC=1 SCAR=0.0
153 1310 ▶sub parent ($self, @r) { $self->_rw(@r) }
Subroutine parent: called CC=1 SCAR=0.0
154 951 ▶sub pre_execute ($self, @r) { $self->_rwa(@r) }
Subroutine pre_execute: called CC=1 SCAR=0.0
155 537 ▶sub residual_args ($self, @r) { $self->_rwa(@r) }
Subroutine residual_args: called CC=1 SCAR=0.0
156 232 ▶sub _last_cmdline ($self, @r) { $self->_rw(@r) }
Subroutine _last_cmdline: called CC=1 SCAR=0.0
157 289 ▶sub _sources ($self, @r) { $self->_rwn(sources => @r) }
Subroutine _sources: called CC=1 SCAR=0.0
158 34 ▶sub usage ($self, @r) { $self->_rw(@r) }
Subroutine usage: called CC=1 SCAR=0.0
159  
160 326 ▶sub config_hash_key ($self, @r) { $self->_rw_prd(@r) }
Subroutine config_hash_key: called CC=1 SCAR=0.0
161  
162 0 ▶sub is_root ($self) { ! defined($self->parent) }
Subroutine is_root: not called CC=1 SCAR=6.9
163 0 ▶sub root ($self) {
Subroutine root: not called CC=3 SCAR=24.8
164 0 my $slot = $self->slot;
165 0 ▶ return $slot->{root} //= do {
Condition0%0/3
$slot->{"root"} //= do { my $retval = $self; while (defined(my $parent = $retval->parent)) { do { $retval = $parent }; } ; $retval }
l!l&&r!l&&!r
000
MC/DC0%0/2
$slot->{"root"} //= do { my $retval = $self; while (defined(my $parent = $retval->parent)) { do { $retval = $parent }; } ; $retval }
$slot->{"root"} do { my $retval = $self; while (defined(my $parent = $retval->parent)) { do { $retval = $parent }; } ; $retval }
Truth table0%0/3
A //= B
A= $slot->{"root"}B= do { my $retval = $self; while (defined(my $parent = $retval->parent)) { do { $retval = $parent }; } ; $retval }
ABresult
1X1
011
000
166 0 my $retval = $self;
167 0 ▶ while (defined(my $parent = $retval->parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $retval->parent)) 0 0 0
168 0 $retval = $parent;
169 }
170 0 $retval;
171 };
172 }
173  
174 276 ▶sub child ($self, @newval) {
Subroutine child: called CC=2 SCAR=7.0
175 276 my $slot = $self->slot;
176 276 ▶ if (@newval) {
Branch: 1 branch
BranchTrueFalseTotal
if (@newval) 276 0 276
177 276 $slot->{child} = $newval[0];
178 276 weaken($slot->{child});
179 }
180 276 return $slot->{child};
181 }
182 0 ▶sub is_leaf ($self) { ! defined($self->child) }
Subroutine is_leaf: not called CC=1 SCAR=6.9
183 0 ▶sub leaf ($self) {
Subroutine leaf: not called CC=3 SCAR=24.8
184 0 my $slot = $self->slot;
185 0 ▶ if (! exists($slot->{leaf})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $slot->{"leaf"}) 0 0 0
186 0 my $retval = $self;
187 0 ▶ while (defined(my $parent = $retval->child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $retval->child)) 0 0 0
188 0 $retval = $parent;
189 }
190 0 $slot->{leaf} = $retval;
191 0 weaken($slot->{leaf});
192 }
193 0 return $slot->{leaf};
194 }
195  
196  
197 # 2024-08-27 expand to allow hashref in addition to arrayref
198 # backwards-compatibility contract is that overriding this function allows
199 # returning the list of sources to use, which might be composed of a single
200 # hashref...
201 615 ▶sub sources ($self, @new) {
Subroutine sources: called CC=10 SCAR=26.7
202 615 my $r;
203 615 my $slot = $self->slot;
204 615 ▶ if (@new) { # setter + getter
Branch: 1 branch
BranchTrueFalseTotal
if (@new) { } 0 615 615
205 0 $r = $slot->{sources} = $new[0];
206 }
207 else { # getter only, set default if *nothing* has been set yet
208 615 state $default_array =
209 [ qw< +CmdLine +Environment +Parent=70 +Default=100 > ];
210 615 state $default_hash = {
211 current => [ qw< +CmdLine +Environment +Default +ParentSlices > ],
212 final => [ ],
213 };
214 615 state $default_hash_v2_008 = {
215 current => [ qw< +CmdLine +Environment +Default +ParentSlices > ],
216 final => [ ],
217 };
218 615 $r = $slot->{sources};
219 $r = $slot->{sources} =
220 615 ▶ ! defined($r) ? Carp::confess()
Branch: 5 branches
BranchTrueFalseTotal
unless ref $r 170 445 615
!defined($r) ? : 0 170 170
$r eq 'default-array' ? : 170 0 170
$r eq 'default-hash' ? : 0 0 0
$r eq 'v2.008' ? : 0 0 0
221 : $r eq 'default-array' ? $default_array
222 : $r eq 'default-hash' ? $default_hash
223 : $r eq 'v2.008' ? $default_hash_v2_008
224 : Carp::confess()
225 unless ref($r); # string-based, get either default
226 }
227 615 ▶ Carp::confess() unless defined($r);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 615 615
228  
229 615 ▶ return $r->@* if ref($r) eq 'ARRAY'; # backwards-compatible behaviour
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "ARRAY" 555 60 615
230 60 ▶ return \$r if ref($r) eq 'HASH'; # new behaviour
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "HASH" 60 0 60
231 0 Carp::confess(); # unsupported condition
232 }
233  
234 # getter only
235 340 ▶sub _sources_for_phase ($self, $phase) {
Subroutine _sources_for_phase: called CC=5 SCAR=16.2
236 340 my @sources = $self->sources; # might call an overridden thing
237  
238 47 return ${$sources[0]}->{$phase}
239 if @sources == 1
240 && ref($sources[0]) eq 'REF'
241 340 ▶ && ref(${$sources[0]}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @sources == 1 and ref $sources[0] eq "REF" and ref ${$sources[0];} eq "HASH" 47 293 340
Condition66%4/6
@sources == 1 and ref $sources[0] eq "REF" and ref ${$sources[0];} eq "HASH"
!ll&&!rl&&r
293047
@sources == 1 and ref $sources[0] eq "REF"
!ll&&!rl&&r
293047
MC/DC33%1/3
@sources == 1 and ref $sources[0] eq "REF" and ref ${$sources[0];} eq "HASH"
@sources == 1 ref $sources[0] eq "REF" ref ${$sources[0];} eq "HASH"
Truth table50%2/4
A and B and C
A= @sources == 1B= ref $sources[0] eq "REF"C= ref ${$sources[0];} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
242  
243 # backwards compatibility means that we only support the "current"
244 # phase and do nothing for other ones.
245 293 ▶ return $phase eq 'current' ? \@sources : ();
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'current' ? : 187 106 293
246 }
247  
248 199 ▶sub supports ($self, $what) {
Subroutine supports: called CC=1 SCAR=0.0
249 199 ▶ any { $_ eq $what } $self->aliases;
Subroutine __ANON__: called CC=1 SCAR=0.0
250 }
251  
252 777 ▶sub options ($self, @r) {
Subroutine options: called CC=1 SCAR=0.0
253 777 return map { $self->resolve_options($_) } $self->_rwa(@r);
254 }
255  
256 1833 ▶sub resolve_options ($self, $spec) {
Subroutine resolve_options: called CC=5 SCAR=16.6
257 1833 ▶ return $spec if ref($spec) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "HASH" 1729 104 1833
258 104 ▶ $spec = [inherit_options => $spec] unless ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec 104 0 104
259 104 ▶ Carp::confess("invalid spec $spec") unless ref($spec) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "ARRAY" 0 104 104
260 104 my ($method_name, @names) = $spec->@*;
261 104 ▶ my $method = $self->can($method_name)
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->can($method_name) or Carp::confess("cannot find method $method_name in $self") 104 0 104
262 or Carp::confess("cannot find method $method_name in $self");
263 104 return $self->$method(@names);
264 } ## end sub resolve_options
265  
266 104 ▶sub inherit_options ($self, @names) {
Subroutine inherit_options: called CC=11 SCAR=24.1
267 104 my %got;
268 map {
269 104 my @options;
270 104 ▶ if ($_ eq '+parent') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq '+parent') { } 57 47 104
271 57 ▶ @options = grep { $_->{transmit} // 0 } $self->parent->options;
Condition100%2/2
$_->{"transmit"} // 0
l!l
26918
MC/DC100%1/1
$_->{"transmit"} // 0
$_->{"transmit"}
Truth table100%2/2
A
A= $_->{"transmit"}
Aresult
11
00
272 }
273 else {
274 47 ▶ my $name_exact = ref($_) ? undef : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 47 47
275 47 my $name_rx = qr{\A(?:$_)\z};
276 47 my $ancestor = $self->parent;
277 47 ▶ while ($ancestor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor) 47 47 94
278 push @options, my @pass = # FIXME something's strange here
279 grep {
280 47 my $name = $self->name_for_option($_);
281 ($_->{transmit} // 0)
282 && (! $got{$name}++) # inherit once only
283 && (
284 (defined($name_exact) && $name eq $name_exact)
285 309 ▶ || (! $_->{transmit_exact} && $name =~ m{$name_rx})
Condition82%14/17
($_->{'transmit'} // 0) && !$got{$name}++ && (defined $name_exact && $name eq $name_exact || !$_->{'transmit_exact'} && $name =~ /$name_rx/u)
!ll&&!rl&&r
4722240
$_->{"transmit"} // 0 and not $got{$name}++
!ll&&!rl&&r
470262
$_->{"transmit"} // 0
l!l
3090
defined $name_exact and $name eq $name_exact or not $_->{"transmit_exact"} and $name =~ /$name_rx/u
l!l&&r!l&&!r
391222
defined $name_exact and $name eq $name_exact
!ll&&!rl&&r
022339
not $_->{"transmit_exact"} and $name =~ /$name_rx/u
!ll&&!rl&&r
32191
MC/DC50%3/6
($_->{'transmit'} // 0) && !$got{$name}++ && (defined $name_exact && $name eq $name_exact || !$_->{'transmit_exact'} && $name =~ /$name_rx/u)
$_->{"transmit"} not $got{$name}++ defined $name_exact $name eq $name_exact not $_->{"transmit_exact"} $name =~ /$name_rx/u
Truth table44%4/9
A and not(B) && C and D or not(E) and F
A= $_->{"transmit"}B= not $got{$name}++C= defined $name_exactD= $name eq $name_exactE= not $_->{"transmit_exact"}F= $name =~ /$name_rx/u
ABCDEFresult
0XXXXX0
10XXXX0
110X0X0
110X100
11100X0
1110100
1111XX1
110X111
1110111
286 );
287 } $ancestor->options;
288 47 $ancestor = $ancestor->parent;
289 } ## end while ($ancestor)
290 } ## end else [ if ($_ eq '+parent') ]
291 104 map { +{transmit => 1, $_->%*, inherited => 1} } @options;
292 } @names;
293 } ## end sub inherit_options
294  
295 396 ▶sub new ($pkg, @args) {
Subroutine new: called CC=4 SCAR=13.9
296 27 ▶ my $pkg_spec = do { no strict 'refs'; ${$pkg . '::app_easer_spec'} };
Subroutine BEGIN: called CC=1 SCAR=0.0
297 396 ▶ my $slot = {
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] ? : 228 168 396
Condition80%4/5
$pkg_spec // {}
l!l
37359
@args and ref $args[0]
!ll&&!rl&&r
1680228
MC/DC100%1/1
$pkg_spec // {}
$pkg_spec
MC/DC50%1/2
@args and ref $args[0]
@args ref $args[0]
Truth table100%2/2
A
A= $pkg_spec
Aresult
11
00
Truth table66%2/3
A and B
A= @argsB= ref $args[0]
ABresult
0X0
100
111
298 aliases => [],
299 allow_residual_options => 0,
300 auto_environment => 0,
301 children => [],
302 children_prefixes => [$pkg . '::Cmd'],
303 config_hash_key => \'merged',
304 default_child => 'help',
305 environment_prefix => '',
306 fallback_to => undef,
307 final_commit_stack => [],
308 force_auto_children => undef,
309 hashy_class => __PACKAGE__,
310 help_channel => '-STDOUT:encoding(UTF-8)',
311 options => [],
312 params_validate => undef,
313 pre_execute => [],
314 residual_args => [],
315 sources => 'default-array', # 2024-08-24 defer
316 ($pkg_spec // {})->%*,
317 (@args && ref $args[0] ? $args[0]->%* : @args),
318 };
319 396 my $self = bless {$pkg => $slot}, $pkg;
320 396 return $self;
321 } ## end sub new
322  
323 1822 ▶sub merge_hashes ($self, @hrefs) { # FIXME this seems way more complicated than needed
Subroutine merge_hashes: called CC=6 SCAR=18.1
324 1822 my (%retval, %is_overridable);
325 1822 for my $href (@hrefs) {
326 4938 for my $src_key (keys $href->%*) {
327 3082 my $dst_key = $src_key;
328 3082 my $this_overridable = 0;
329 $retval{$dst_key} = $href->{$src_key}
330 3082 ▶ if $is_overridable{$dst_key} || !exists($retval{$dst_key});
Branch: 1 branch
BranchTrueFalseTotal
if $is_overridable{$dst_key} or not exists $retval{$dst_key} 2418 664 3082
Condition66%2/3
$is_overridable{$dst_key} or not exists $retval{$dst_key}
l!l&&r!l&&!r
02418664
MC/DC50%1/2
$is_overridable{$dst_key} or not exists $retval{$dst_key}
$is_overridable{$dst_key} not exists $retval{$dst_key}
Truth table66%2/3
A or not(B)
A= $is_overridable{$dst_key}B= not exists $retval{$dst_key}
ABresult
1X1
011
000
331 3082 ▶ $is_overridable{$dst_key} = 0 unless $this_overridable;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_overridable 3082 0 3082
332 } ## end for my $src_key (keys $href...)
333 } ## end for my $href (@hrefs)
334 1822 return \%retval;
335 } ## end sub merge_hashes
336  
337 234 ▶sub _collect ($self, $sources, @args) {
Subroutine _collect: called CC=19 SCAR=29.4
338 234 my @residual_args; # what is left from the @args at the end
339  
340 234 my $slot = $self->slot;
341 234 my $last_priority = 0;
342 234 for my $source ($sources->@*) {
343 911 ▶ my ($src, @opts) = ref($source) eq 'ARRAY' ? $source->@* : $source;
Branch: 1 branch
BranchTrueFalseTotal
ref $source eq 'ARRAY' ? : 5 906 911
344 911 ▶ my $meta = (@opts && ref $opts[0]) ? shift @opts : {};
Branch: 1 branch
BranchTrueFalseTotal
@opts && ref $opts[0] ? : 2 909 911
Condition100%3/3
@opts and ref $opts[0]
!ll&&!rl&&r
90632
MC/DC100%2/2
@opts and ref $opts[0]
@opts ref $opts[0]
Truth table100%3/3
A and B
A= @optsB= ref $opts[0]
ABresult
0X0
100
111
345 911 my $locator = $src;
346 911 ▶ if (! ref($src)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $src) 909 2 911
347 909 ($src, my $priority) = split m{=}mxs, $src;
348 909 ▶ $meta->{priority} = $priority if defined $priority;
Branch: 1 branch
BranchTrueFalseTotal
if defined $priority 459 450 909
349 909 $locator = $src =~ s{\A \+}{source_}rmxs;
350 }
351 911 ▶ my $sub = $self->ref_to_sub($locator)
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->ref_to_sub($locator) or die "unhandled source for $locator\n" 911 0 911
352 or die "unhandled source for $locator\n";
353  
354 911 my ($slice, $residuals) = $sub->($self, \@opts, \@args);
355 911 ▶ push @residual_args, $residuals->@* if defined $residuals;
Branch: 1 branch
BranchTrueFalseTotal
if defined $residuals 217 694 911
356  
357 # whatever happened in the source, it might have changed the
358 # internals and we need to re-load them from the current config
359 911 ▶ my $latest = $self->_rwn('config') // {};
Condition100%2/2
$self->_rwn("config") // {}
l!l
694217
MC/DC100%1/1
$self->_rwn("config") // {}
$self->_rwn("config")
Truth table100%2/2
A
A= $self->_rwn("config")
Aresult
11
00
360 911 ▶ my @sequence = ($latest->{sequence} //= [])->@*; # legacy
Condition100%2/2
$latest->{"sequence"} //= []
l!l
694217
MC/DC100%1/1
$latest->{"sequence"} //= []
$latest->{"sequence"}
Truth table100%2/2
A
A= $latest->{"sequence"}
Aresult
11
00
361 911 ▶ my %all_eslices_at = ($latest->{all_eslices_at} // {})->%*; # v2.8
Condition100%2/2
$latest->{"all_eslices_at"} // {}
l!l
694217
MC/DC100%1/1
$latest->{"all_eslices_at"} // {}
$latest->{"all_eslices_at"}
Truth table100%2/2
A
A= $latest->{"all_eslices_at"}
Aresult
11
00
362 911 ▶ my %command_eslices_at = ($latest->{command_eslices_at} // {})->%*;
Condition100%2/2
$latest->{"command_eslices_at"} // {}
l!l
694217
MC/DC100%1/1
$latest->{"command_eslices_at"} // {}
$latest->{"command_eslices_at"}
Truth table100%2/2
A
A= $latest->{"command_eslices_at"}
Aresult
11
00
363  
364 # only operate if the source returned something to track
365 911 ▶ if ($slice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slice) 908 3 911
366 $last_priority = my $priority
367 908 ▶ = $meta->{priority} //= $last_priority + 10;
Condition100%2/2
$meta->{"priority"} //= $last_priority + 10
l!l
461447
MC/DC100%1/1
$meta->{"priority"} //= $last_priority + 10
$meta->{"priority"}
Truth table100%2/2
A
A= $meta->{"priority"}
Aresult
11
00
368  
369 908 my $eslice = [$priority, $src, \@opts, $locator, $slice];
370  
371 # new way of collecting the aggregated configuration
372 # the merge takes into account priorities across all command
373 # layers, this function encapsulates getting all of them
374 908 ▶ push(($all_eslices_at{$priority} //= [])->@*, $eslice);
Condition100%2/2
$all_eslices_at{$priority} //= []
l!l
30878
MC/DC100%1/1
$all_eslices_at{$priority} //= []
$all_eslices_at{$priority}
Truth table100%2/2
A
A= $all_eslices_at{$priority}
Aresult
11
00
375 908 ▶ push(($command_eslices_at{$priority} //= [])->@*, $eslice);
Condition100%2/2
$command_eslices_at{$priority} //= []
l!l
30878
MC/DC100%1/1
$command_eslices_at{$priority} //= []
$command_eslices_at{$priority}
Truth table100%2/2
A
A= $command_eslices_at{$priority}
Aresult
11
00
376  
377 # older way of collecting the aggregated configuration
378 908 push @sequence, $eslice;
379 908 ▶ for (my $i = $#sequence; $i > 0; --$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 763 218 981
380 763 ▶ last if $sequence[$i - 1][0] <= $sequence[$i][0];
Branch: 1 branch
BranchTrueFalseTotal
if $sequence[$i - 1][0] <= $sequence[$i][0] 690 73 763
381 73 @sequence[$i - 1, $i] = @sequence[$i, $i - 1];
382 }
383 }
384  
385 # whatever happened, re-compute the aggregated configuration in the
386 # new "matrix" way and in the legacy way
387 my $matrix_config = $self->merge_hashes(
388 2472 map { $_->[-1] } # take slice out of eslice
389 2331 map { $all_eslices_at{$_}->@* } # unroll all eslices
390 911 sort { $a <=> $b } # sort by priority
391 keys(%all_eslices_at) # keys is the priority
392 );
393 911 my $legacy_config = $self->merge_hashes(map {$_->[-1]} @sequence);
394  
395 # save configuration at each step, so that each following source
396 # can take advantage of configurations collected so far. This is
397 # important for e.g. sources that load options from files whose
398 # path is provided as an option itself.
399 911 $self->_rwn(
400 config => {
401 merged => $legacy_config,
402 merged_legacy => $legacy_config,
403 'v2.008' => $matrix_config,
404 sequence => \@sequence,
405 all_eslices_at => \%all_eslices_at,
406 command_eslices_at => \%command_eslices_at,
407 }
408 );
409 } ## end for my $source ($self->...)
410 #App::Easer::V2::d(config => $self->_rwn('config'));
411  
412 # return what's left
413 234 return \@residual_args;
414 }
415  
416 217 ▶sub collect ($self, @args) {
Subroutine collect: called CC=2 SCAR=7.1
417 217 ▶ if (my $sources = $self->_sources_for_phase('current')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sources = $self->_sources_for_phase("current")) 217 0 217
418 217 $self->residual_args($self->_collect($sources, @args));
419 }
420 217 return $self;
421 } ## end sub collect
422  
423 # last round of configuration options collection
424 123 ▶sub final_collect ($self) {
Subroutine final_collect: called CC=2 SCAR=6.9
425 123 ▶ if (my $sources = $self->_sources_for_phase('final')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sources = $self->_sources_for_phase("final")) 17 106 123
426 17 $self->_collect($sources);
427 }
428 123 return $self;
429 } ## end sub collect
430  
431 217 ▶sub getopt_config ($self, @n) {
Subroutine getopt_config: called CC=4 SCAR=14.0
432 217 my $value = $self->_rw(@n);
433 217 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 217 0 217
434 217 my @r = qw< gnu_getopt >;
435 217 ▶ push @r, qw< require_order pass_through > if $self->list_children;
Branch: 1 branch
BranchTrueFalseTotal
if $self->list_children 131 86 217
436 217 ▶ push @r, qw< pass_through > if $self->allow_residual_options;
Branch: 1 branch
BranchTrueFalseTotal
if $self->allow_residual_options 0 217 217
437 217 $value = $self->_rw(\@r);
438 } ## end if (!defined $value)
439 217 return $value->@*;
440 } ## end sub getopt_config
441  
442 # This source is not supposed to accept "options", although it might in
443 # the future, e.g. to set a specific getopt_config instead of setting it
444 # as a general parameter. On the other hand, it does focus on processing
445 # $args
446 217 ▶sub source_CmdLine ($self, $ignore, $args) {
Subroutine source_CmdLine: called CC=9 SCAR=23.1
447 217 my @args = $args->@*;
448  
449 217 require Getopt::Long;
450 217 Getopt::Long::Configure('default', $self->getopt_config);
451  
452 217 my (%option_for, @specs, %name_for);
453 217 for my $option ($self->options) {
454 488 ▶ next unless exists($option->{getopt});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option->{"getopt"} 5 483 488
455 483 my $go = $option->{getopt};
456 483 ▶ if (ref($go) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $go eq 'ARRAY') { } 0 483 483
457 0 my ($string, $callback) = $go->@*;
458 0 ▶ push @specs, $string, sub { $callback->(\%option_for, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
459 0 $go = $string;
460 }
461 else {
462 483 push @specs, $go;
463 }
464  
465 483 my ($go_name) = $go =~ m{\A(\w[-\w]*)}mxs;
466 483 my $official_name = $self->name_for_option($option);
467 483 ▶ $name_for{$go_name} = $official_name if $go_name ne $official_name;
Branch: 1 branch
BranchTrueFalseTotal
if $go_name ne $official_name 10 473 483
468 }
469  
470 217 ▶ Getopt::Long::GetOptionsFromArray(\@args, \%option_for, @specs)
Branch: 1 branch
BranchTrueFalseTotal
Getopt::Long::GetOptionsFromArray(\@args, \%option_for, @specs) or die "bailing out\n" 217 0 217
471 or die "bailing out\n";
472  
473 # Check if we want to forbid the residual @args to start with a '-'
474 217 my $strict = !$self->allow_residual_options;
475 217 ▶ die "bailing out (allow_residual_options is false and got <@args>)"
Branch: 1 branch
BranchTrueFalseTotal
if $strict and @args and $args[0] =~ /\A - . /msux 0 217 217
Condition66%4/6
$strict and @args and $args[0] =~ /\A - . /msux
!ll&&!rl&&r
971200
$strict and @args
!ll&&!rl&&r
097120
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
476 if $strict && @args && $args[0] =~ m{\A - . }mxs;
477  
478 # remap names where the official one is different from the getopt one
479 217 $self->_rename_options_inplace(\%option_for, \%name_for);
480  
481 217 $self->_last_cmdline( { option_for => \%option_for, args => \@args });
482  
483 217 return (\%option_for, \@args);
484 } ## end sub source_CmdLine
485  
486 217 ▶sub _rename_options_inplace ($self, $collected, $name_for) {
Subroutine _rename_options_inplace: called CC=4 SCAR=13.9
487 217 my %renamed;
488 217 for my $go_name (sort { $a cmp $b } keys $name_for->%*) {
489 10 ▶ next unless exists $collected->{$go_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $collected->{$go_name} 6 4 10
490 4 my $official_name = $name_for->{$go_name};
491 4 $renamed{$official_name} = delete($collected->{$go_name});
492 }
493 217 $collected->{$_} = $renamed{$_} for keys %renamed;
494 217 return $self;
495 }
496  
497 15 ▶sub source_LastCmdLine ($self, @ignore) {
Subroutine source_LastCmdLine: called CC=2 SCAR=7.2
498 15 ▶ my $last = $self->_last_cmdline or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $last = $self->_last_cmdline or return {} 15 0 15
499 15 return $last->{option_for};
500 }
501  
502 1305 ▶sub name_for_option ($self, $o) {
Subroutine name_for_option: called CC=6 SCAR=26.9
503 1305 ▶ return $o->{name} if defined $o->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"name"} 30 1275 1305
504 return $1
505 1275 ▶ if defined $o->{getopt} && $o->{getopt} =~ m{\A(\w[-\w]*)}mxs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w[-\w]*)/msux 1275 0 1275
Condition33%1/3
defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w[-\w]*)/msux
!ll&&!rl&&r
001275
MC/DC0%0/2
defined $o->{"getopt"} and $o->{"getopt"} =~ /\A(\w[-\w]*)/msux
defined $o->{"getopt"} $o->{"getopt"} =~ /\A(\w[-\w]*)/msux
Truth table33%1/3
A and B
A= defined $o->{"getopt"}B= $o->{"getopt"} =~ /\A(\w[-\w]*)/msux
ABresult
0X0
100
111
506 return lc $o->{environment}
507 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
508 0 return '~~~';
509 } ## end sub name_for_option
510  
511 205 ▶sub source_Default ($self, $opts, @ignore) {
Subroutine source_Default: called CC=2 SCAR=6.9
512 205 my %opts = $opts->@*;
513 205 my $include_inherited = $opts{include_inherited};
514 return {
515 235 map { $self->name_for_option($_) => $_->{default} }
516 393 grep { exists $_->{default} }
517 205 ▶ grep { $include_inherited || !$_->{inherited} } $self->options
Condition100%3/3
$include_inherited || !$_->{'inherited'}
l!l&&r!l&&!r
7332035
MC/DC100%2/2
$include_inherited || !$_->{'inherited'}
$include_inherited !$_->{'inherited'}
Truth table100%3/3
A || not(B)
A= $include_inheritedB= !$_->{'inherited'}
ABresult
1X1
011
000
518 };
519 }
520 15 ▶sub source_FinalDefault ($self, @i) {
Subroutine source_FinalDefault: called CC=1 SCAR=0.0
521 15 return $self->source_Default([ include_inherited => 1]);
522 }
523  
524 3 ▶sub source_FromTrail ($self, $trail, @ignore) {
Subroutine source_FromTrail: called CC=4 SCAR=14.2
525 3 my $conf = $self->config_hash;
526 3 for my $key ($trail->@*) {
527 9 ▶ return {} unless defined $conf->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{$key} 0 9 9
528 9 $conf = $conf->{$key};
529 9 ▶ die "invalid trail $trail->@* for configuration gathering"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf eq "HASH" 0 9 9
530 unless ref($conf) eq 'HASH';
531 } ## end for my $key ($keys->@*)
532 3 return $conf;
533 }
534  
535 463 ▶sub environment_variable_name ($self, $ospec) {
Subroutine environment_variable_name: called CC=7 SCAR=19.5
536 my $env =
537 exists $ospec->{environment} ? $ospec->{environment}
538 463 ▶ : $self->auto_environment ? 1
Branch: 2 branches
BranchTrueFalseTotal
exists $ospec->{'environment'} ? : 228 235 463
$self->auto_environment ? : 9 226 235
539 : undef;
540 463 ▶ return $env unless ($env // '') eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless ($env // "") eq 1 361 102 463
Condition100%2/2
$env // ""
l!l
237226
MC/DC100%1/1
$env // ""
$env
Truth table100%2/2
A
A= $env
Aresult
11
00
541  
542 # get prefixes all the way up to the first command
543 102 my @prefixes;
544 102 ▶ for (my $instance = $self; $instance; $instance = $instance->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) 111 102 213
545 111 ▶ unshift @prefixes, $instance->environment_prefix // '';
Condition50%1/2
$instance->environment_prefix // ""
l!l
1110
MC/DC0%0/1
$instance->environment_prefix // ""
$instance->environment_prefix
Truth table50%1/2
A
A= $instance->environment_prefix
Aresult
11
00
546 }
547  
548 102 my $name = $self->name_for_option($ospec) =~ s{\W+}{_}rgmxs;
549 102 return uc(join '', @prefixes, $name);
550 } ## end sub environment_variable_name
551  
552  
553 209 ▶sub source_Environment ($self, $opts, @ignore) {
Subroutine source_Environment: called CC=4 SCAR=13.9
554 209 my %opts = $opts->@*;
555 209 my $include_inherited = $opts{include_inherited};
556 return {
557 map {
558 414 my $en = $self->environment_variable_name($_);
559 defined($en)
560 && exists($ENV{$en})
561 414 ▶ ? ($self->name_for_option($_) => $ENV{$en})
Branch: 1 branch
BranchTrueFalseTotal
defined $en && exists $ENV{$en} ? : 55 359 414
Condition100%3/3
defined $en and exists $ENV{$en}
!ll&&!rl&&r
19716255
MC/DC100%2/2
defined $en and exists $ENV{$en}
defined $en exists $ENV{$en}
Truth table100%3/3
A and B
A= defined $enB= exists $ENV{$en}
ABresult
0X0
100
111
562 : ();
563 209 ▶ } grep { $include_inherited || !$_->{inherited} } $self->options
Condition100%3/3
$include_inherited || !$_->{'inherited'}
l!l&&r!l&&!r
7334140
MC/DC100%2/2
$include_inherited || !$_->{'inherited'}
$include_inherited !$_->{'inherited'}
Truth table100%3/3
A || not(B)
A= $include_inheritedB= !$_->{'inherited'}
ABresult
1X1
011
000
564 };
565 } ## end sub source_Environment
566 15 ▶sub source_FinalEnvironment ($self, @i) {
Subroutine source_FinalEnvironment: called CC=1 SCAR=0.0
567 15 return $self->source_Environment([ include_inherited => 1 ]);
568 }
569  
570 33 ▶sub source_JsonFileFromConfig ($self, $key, @ignore) {
Subroutine source_JsonFileFromConfig: called CC=3 SCAR=11.0
571 33 ▶ $key = $key->[0] // 'config';
Condition50%1/2
$key->[0] // "config"
l!l
033
MC/DC0%0/1
$key->[0] // "config"
$key->[0]
Truth table50%1/2
A
A= $key->[0]
Aresult
11
00
572 33 ▶ defined(my $filename = $self->config($key)) or return {};
Branch: 1 branch
BranchTrueFalseTotal
defined(my $filename = $self->config($key)) or return {} 10 23 33
573 10 require JSON::PP;
574 10 return JSON::PP::decode_json($self->slurp($filename));
575 } ## end sub source_JsonFileFromConfig
576  
577 10 ▶sub slurp ($self, $file, $mode = '<:encoding(UTF-8)') {
Subroutine slurp: called CC=3 SCAR=11.1
578 10 ▶ open my $fh, $mode, $file or die "open('$file'): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $file or die "open('${file}'): $!\n" 10 0 10
579 10 local $/;
580 10 return <$fh>;
581 }
582  
583 0 ▶sub source_JsonFiles ($self, $candidates, @ignore) {
Subroutine source_JsonFiles: not called CC=1 SCAR=6.9
584 0 require JSON::PP;
585 return $self->merge_hashes(
586 0 map { JSON::PP::decode_json($self->slurp($_)) }
587 0 grep { -e $_ } $candidates->@*
588 );
589 } ## end sub source_JsonFiles
590  
591 223 ▶sub source_Parent ($self, @ignore) {
Subroutine source_Parent: called CC=2 SCAR=6.9
592 223 ▶ my $parent = $self->parent or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->parent or return {} 104 119 223
593 104 return $parent->config_hash(0);
594 }
595  
596 3 ▶sub source_ParentSlices ($self, @ignore) {
Subroutine source_ParentSlices: called CC=7 SCAR=20.1
597 3 ▶ my $parent = $self->parent or return; # no Parent, no Party
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->parent or return 1 2 3
598  
599 1 my $latest = $self->_rwn('config');
600 1 ▶ $self->_rwn(config => ($latest = {})) unless defined $latest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $latest 0 1 1
601 1 ▶ my $all_eslices_at = $latest->{all_eslices_at} //= {};
Condition50%1/2
$latest->{"all_eslices_at"} //= {}
l!l
10
MC/DC0%0/1
$latest->{"all_eslices_at"} //= {}
$latest->{"all_eslices_at"}
Truth table50%1/2
A
A= $latest->{"all_eslices_at"}
Aresult
11
00
602  
603 # get all stuff from parent, keeping priorities.
604 1 ▶ my $pslices_at = $parent->config_hash(1)->{all_eslices_at} // {};
Condition50%1/2
$parent->config_hash(1)->{"all_eslices_at"} // {}
l!l
10
MC/DC0%0/1
$parent->config_hash(1)->{"all_eslices_at"} // {}
$parent->config_hash(1)->{"all_eslices_at"}
Truth table50%1/2
A
A= $parent->config_hash(1)->{"all_eslices_at"}
Aresult
11
00
605 1 for my $priority (keys($pslices_at->%*)) {
606 3 ▶ my $eslices = $all_eslices_at->{$priority} //= [];
Condition50%1/2
$all_eslices_at->{$priority} //= []
l!l
30
MC/DC0%0/1
$all_eslices_at->{$priority} //= []
$all_eslices_at->{$priority}
Truth table50%1/2
A
A= $all_eslices_at->{$priority}
Aresult
11
00
607 3 push $eslices->@*, $pslices_at->{$priority}->@*;
608 }
609  
610 1 return;
611 }
612  
613 # get the assembled config for the command. It supports the optional
614 # additional boolean parameter $blame to get back a more structured
615 # version where it's clear where each option comes from, to allow for
616 # further injection of parameters from elsewhere.
617 273 ▶sub config_hash ($self, $blame = 0) {
Subroutine config_hash: called CC=5 SCAR=16.5
618 273 ▶ my $config = $self->_rwn('config') // {};
Condition50%1/2
$self->_rwn("config") // {}
l!l
2730
MC/DC0%0/1
$self->_rwn("config") // {}
$self->_rwn("config")
Truth table50%1/2
A
A= $self->_rwn("config")
Aresult
11
00
619 273 ▶ return $config if $blame;
Branch: 1 branch
BranchTrueFalseTotal
if $blame 1 272 273
620 272 ▶ return $config->{$self->config_hash_key} // {};
Condition50%1/2
$$config{$self->config_hash_key} // {}
l!l
2720
MC/DC0%0/1
$$config{$self->config_hash_key} // {}
$$config{$self->config_hash_key}
Truth table50%1/2
A
A= $$config{$self->config_hash_key}
Aresult
11
00
621 }
622  
623 # get one or more specific configurtion values
624 59 ▶sub config ($self, @keys) {
Subroutine config: called CC=2 SCAR=8.1
625 59 my $hash = $self->config_hash(0);
626 59 ▶ return $hash->{$keys[0]} if @keys == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @keys == 1 59 0 59
627 0 return $hash->@{@keys};
628 }
629  
630 0 ▶sub set_config ($self, $key, @value) {
Subroutine set_config: not called CC=2 SCAR=17.9
631 0 my $hash = $self->config_hash(0);
632 0 delete $hash->{$key};
633 0 ▶ $hash->{$key} = $value[0] if @value;
Branch: 1 branch
BranchTrueFalseTotal
if @value 0 0 0
634 0 return $self;
635 } ## end sub set_config
636  
637 # totally replace whatever has been collected at this level
638 0 ▶sub set_config_hash ($self, $new, $full = 0) {
Subroutine set_config_hash: not called CC=4 SCAR=30.0
639 0 ▶ if (! $full) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($full) 0 0 0
640 0 ▶ my $previous = $self->_rwn('config') // {};
Condition0%0/2
$self->_rwn("config") // {}
l!l
00
MC/DC0%0/1
$self->_rwn("config") // {}
$self->_rwn("config")
Truth table0%0/2
A
A= $self->_rwn("config")
Aresult
11
00
641 0 my $key = $self->config_hash_key;
642 0 $new = { $previous->%*, merged => $new, override => $new };
643 }
644 0 $self->_rwn(config => $new);
645 0 return $self;
646 }
647  
648 0 ▶sub inject_configs ($self, $data, $priority = 1000) {
Subroutine inject_configs: not called CC=2 SCAR=17.9
649  
650 # we define an on-the-fly source and get it considered through the
651 # regular source-handling mechanism by _collect
652 $self->_collect(
653 [
654 0 ▶ sub ($self, $opts, $args) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
655 0 my $latest = $self->_rwn('config');
656 0 ▶ $self->_rwn(config => ($latest = {})) unless $latest;
Branch: 1 branch
BranchTrueFalseTotal
unless $latest 0 0 0
657 0 ▶ my $queue = $latest->{all_eslices_at}{$priority} //= [];
Condition0%0/2
$latest->{"all_eslices_at"}{$priority} //= []
l!l
00
MC/DC0%0/1
$latest->{"all_eslices_at"}{$priority} //= []
$latest->{"all_eslices_at"}{$priority}
Truth table0%0/2
A
A= $latest->{"all_eslices_at"}{$priority}
Aresult
11
00
658 0 push $queue->@*, [ $priority, injection => [], '', $data ];
659 0 return;
660 },
661 0 ]
662 );
663 }
664  
665 # (intermediate) commit collected options values, called after collect ends
666 217 ▶sub commit ($self, @n) {
Subroutine commit: called CC=3 SCAR=11.0
667 217 my $commit = $self->_rw(@n);
668 217 ▶ return $commit if @n; # setter, don't call the commit callback
Branch: 1 branch
BranchTrueFalseTotal
if @n 0 217 217
669 217 ▶ return unless $commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $commit 198 19 217
670 19 return $self->ref_to_sub($commit)->($self);
671 } ## end sub commit
672  
673 # final commit of collected options values, called after final_collect ends
674 # this method tries to "propagate" the call up to the parent (and the root
675 # eventually) unless told not to do so. This should allow concentrating
676 # some housekeeping operations in the root command while still waiting for
677 # all options to have been collected
678 220 ▶sub final_commit ($self, @n) {
Subroutine final_commit: called CC=7 SCAR=19.7
679 220 ▶ return $self->_rw(@n) if @n; # setter, don't call the callback
Branch: 1 branch
BranchTrueFalseTotal
if @n 0 220 220
680  
681 # we operate down at the slot level because we want to separate the case
682 # where key 'final_commit' is absent (defaulting to propagation up to
683 # the parent) and where it's set but otherwise false (in which case
684 # there is no propagation).
685 220 my $slot = $self->slot;
686  
687 # put "myself" onto the call stack for final_commit
688 220 ▶ my $stack = $slot->{final_commit_stack} //= [];
Condition50%1/2
$slot->{"final_commit_stack"} //= []
l!l
2200
MC/DC0%0/1
$slot->{"final_commit_stack"} //= []
$slot->{"final_commit_stack"}
Truth table50%1/2
A
A= $slot->{"final_commit_stack"}
Aresult
11
00
689 220 push $stack->@*, $self;
690  
691 220 ▶ if (exists($slot->{final_commit})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $slot->{"final_commit"}) 5 215 220
692 5 my $commit = $slot->{final_commit};
693  
694 # if $commit is false (but present, because it exists) then we
695 # stop and do not propagate to the parent
696 5 ▶ return unless $commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $commit 0 5 5
697  
698 # otherwise, we call it and its return value will tell us whether to
699 # propagate to the parent too or stop here
700 5 my $propagate_to_parent = $self->ref_to_sub($commit)->($self);
701 5 ▶ return unless $propagate_to_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $propagate_to_parent 0 5 5
702 }
703  
704 # here we try to propagate to the parent... if it exists
705 220 my $parent = $self->parent;
706 220 ▶ return unless $parent; # we're root, no parent, no propagation up
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 123 97 220
707  
708 97 $parent->final_commit_stack([$stack->@*]);
709 97 return $parent->final_commit;
710 } ## end sub commit
711  
712 # validate collected options values, called after commit ends.
713 217 ▶sub validate ($self, @n) {
Subroutine validate: called CC=9 SCAR=37.9
714  
715 # Support the "accessor" interface for using a validation sub
716 217 my $validator = $self->_rw(@n);
717 217 ▶ return $validator if @n;
Branch: 1 branch
BranchTrueFalseTotal
if @n 0 217 217
718  
719 # If set, it MUST be a validation sub reference. Otherwise, try the
720 # params_validate/Params::Validate path.
721 217 ▶ if ($validator) {
Branch: 2 branches
BranchTrueFalseTotal
if ($validator) { } 0 217 217
elsif (my $params_validate = $self->params_validate) { } 0 217 217
722 0 ▶ die "validator can only be a CODE reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $validator eq "CODE" 0 0 0
723 unless ref $validator eq 'CODE';
724 0 $validator->($self);
725 }
726 elsif (my $params_validate = $self->params_validate) {
727 0 require Params::Validate;
728 0 ▶ if (my $config_validator = $params_validate->{config} // undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_validator = $params_validate->{"config"} // undef) 0 0 0
Condition0%0/2
$params_validate->{"config"} // undef
l!l
00
MC/DC0%0/1
$params_validate->{"config"} // undef
$params_validate->{"config"}
Truth table0%0/2
A
A= $params_validate->{"config"}
Aresult
11
00
729 0 my @array = $self->config_hash;
730 0 &Params::Validate::validate(\@array, $config_validator);
731 }
732 0 ▶ if (my $args_validator = $params_validate->{args} // undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args_validator = $params_validate->{"args"} // undef) 0 0 0
Condition0%0/2
$params_validate->{"args"} // undef
l!l
00
MC/DC0%0/1
$params_validate->{"args"} // undef
$params_validate->{"args"}
Truth table0%0/2
A
A= $params_validate->{"args"}
Aresult
11
00
733 0 my @array = $self->residual_args;
734 0 &Params::Validate::validate_pos(\@array, $args_validator->@*);
735 }
736 }
737 else {} # no validation needed
738  
739 217 return $self;
740 } ## end sub validate ($self)
741  
742 127 ▶sub find_matching_child ($self, $command) {
Subroutine find_matching_child: called CC=4 SCAR=13.9
743 127 ▶ return unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 0 127 127
744 127 for my $candidate ($self->list_children) {
745 199 my ($child) = $self->inflate_children($candidate);
746 199 ▶ return $child if $child->supports($command);
Branch: 1 branch
BranchTrueFalseTotal
if $child->supports($command) 122 77 199
747 }
748 5 return;
749 } ## end sub find_matching_child
750  
751 40 ▶sub _inflate_default_child ($self) {
Subroutine _inflate_default_child: called CC=4 SCAR=14.2
752 40 ▶ defined(my $default = $self->default_child)
Branch: 1 branch
BranchTrueFalseTotal
defined(my $default = $self->default_child) or die "undefined default child\n" 40 0 40
753 or die "undefined default child\n";
754 40 ▶ return undef if $default eq '-self';
Branch: 1 branch
BranchTrueFalseTotal
if $default eq "-self" 36 4 40
755 4 ▶ my $child = $self->find_matching_child($default)
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->find_matching_child($default) or die "no child matching the default $default\n" 4 0 4
756 or die "no child matching the default $default\n";
757 4 return $child;
758 } ## end sub inflate_default_child ($self)
759  
760 # look for a child to hand execution over. Returns an child instance or
761 # undef (which means that the $self is in charge of executing
762 # something). This implements the most sensible default, deviations will
763 # have to be coded explicitly.
764 # Return values:
765 # - (undef, '-leaf') if no child exists
766 # - ($instance, @args) if a child is found with $args[0]
767 # - ($instance, '-default') if the default child is returned
768 # - (undef, '-fallback') in case $self is the fallback
769 # - ($instance, '-fallback', @args) in case the fallback is returned
770 217 ▶sub find_child ($self) {
Subroutine find_child: called CC=9 SCAR=24.4
771 217 ▶ my @candidates = $self->list_children or return (undef, '-leaf');
Branch: 1 branch
BranchTrueFalseTotal
my(@candidates) = $self->list_children or return undef, "-leaf" 131 86 217
772 131 my @residuals = $self->residual_args;
773 131 ▶ if (@residuals) {
Branch: 2 branches
BranchTrueFalseTotal
if (@residuals) { } 91 40 131
elsif (defined(my $default = $self->default_child)) { } 40 0 40
774 91 ▶ if (my $child = $self->find_matching_child($residuals[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $self->find_matching_child($residuals[0])) 86 5 91
775 86 return ($child, @residuals);
776 } # otherwise... see what the fallback is about
777 }
778 elsif (defined(my $default = $self->default_child)) {
779 40 return ($self->_inflate_default_child, '-default');
780 }
781  
782 # try the fallback...
783 5 my $fallback = $self->fallback;
784 5 ▶ if (defined $fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fallback) 1 4 5
785 1 ▶ return (undef, '-fallback') if $fallback eq '-self';
Branch: 1 branch
BranchTrueFalseTotal
if $fallback eq "-self" 1 0 1
786 0 ▶ return ($self->_inflate_default_child, '-default')
Branch: 1 branch
BranchTrueFalseTotal
if $fallback eq "-default" 0 0 0
787 if $fallback eq '-default';
788 0 ▶ if (my $child = $self->find_matching_child($fallback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $self->find_matching_child($fallback)) 0 0 0
789 0 return ($child, -fallback => @residuals);
790 }
791 } ## end if (defined $fallback)
792  
793 # no fallback at this point... it's an error, build a message and die!
794 # FIXME this can be improved
795 4 die "cannot find sub-command '$residuals[0]'\n";
796 } ## end sub find_child ($self)
797  
798 # get the list of children. This only gives back a list of "hints" that
799 # can be turned into instances via inflate_children. In this case, it's
800 # module names
801 602 ▶sub list_children ($self) {
Subroutine list_children: called CC=8 SCAR=24.9
802 602 my @children = $self->children;
803  
804 # handle auto-loading of children from modules in @INC via prefixes
805 602 require File::Spec;
806 my @expanded_inc = map {
807 602 my ($v, $dirs) = File::Spec->splitpath($_, 'no-file');
808 4303 [$v, File::Spec->splitdir($dirs)];
809 } @INC;
810 602 my %seen;
811 my @autoloaded_children = map {
812 602 my @parts = split m{::}mxs, $_ . 'x';
813 602 substr(my $bprefix = pop @parts, -1, 1, '');
814 map {
815 602 my ($v, @dirs) = $_->@*;
816 4303 my $dirs = File::Spec->catdir(@dirs, @parts);
817 4303 ▶ if (opendir my $dh, File::Spec->catpath($v, $dirs, '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, 'File::Spec'->catpath($v, $dirs, '')) { } 0 4303 4303
818 0 grep { !$seen{$_}++ }
819 map {
820 0 substr(my $lastpart = $_, -3, 3, '');
821 0 join '::', @parts, $lastpart;
822 } grep {
823 0 my $path = File::Spec->catpath($v, $dirs, $_);
824 0 ▶ (-e $path && !-d $path)
Condition0%0/9
-e $path && !(-d $path) && substr($_, 0, length $bprefix) eq $bprefix && substr($_, -3, 3) eq '.pm'
!ll&&!rl&&r
000
-e $path and not -d $path and substr($_, 0, length $bprefix) eq $bprefix
!ll&&!rl&&r
000
-e $path and not -d $path
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
825 && substr($_, 0, length($bprefix)) eq $bprefix
826 && substr($_, -3, 3) eq '.pm'
827 0 } sort { $a cmp $b } readdir $dh;
828 } ## end if (opendir my $dh, File::Spec...)
829 4303 else { () }
830 } @expanded_inc;
831 } $self->children_prefixes;
832 push @autoloaded_children, map {
833 602 my $prefix = $_;
834 602 my $prefix_length = length($prefix);
835 44 grep { !$seen{$_}++ }
836 grep {
837 602 ▶ (substr($_, 0, length $prefix) eq $prefix)
Condition66%2/3
substr($_, 0, length $prefix) eq $prefix && (index($_, ':', $prefix_length) == -1)
!ll&&!rl&&r
379044
MC/DC50%1/2
substr($_, 0, length $prefix) eq $prefix && (index($_, ':', $prefix_length) == -1)
substr($_, 0, length $prefix) eq $prefix (index($_, ':', $prefix_length) == -1)
Truth table66%2/3
A && B
A= substr($_, 0, length $prefix) eq $prefixB= (index($_, ':', $prefix_length) == -1)
ABresult
0X0
100
111
838 && (index($_, ':', $prefix_length) < 0);
839 } keys %App::Easer::V2::registered;
840 } $self->children_prefixes;
841  
842 # auto-loaded children are appended with consistent sorting
843 0 push @children, sort { $a cmp $b } @autoloaded_children;
844  
845 602 ▶ push @children, $self->auto_children
Branch: 1 branch
BranchTrueFalseTotal
if $self->force_auto_children // @children 408 194 602
Condition66%2/3
$self->force_auto_children // @children
l!l&&r!l&&!r
1044980
MC/DC0%0/2
$self->force_auto_children // @children
$self->force_auto_children @children
Truth table66%2/3
A // B
A= $self->force_auto_childrenB= @children
ABresult
1X1
011
000
846 if $self->force_auto_children // @children;
847 602 return @children;
848 } ## end sub list_children ($self)
849  
850 1232 ▶sub _auto_child ($self, $name, $inflate = 0) {
Subroutine _auto_child: called CC=3 SCAR=11.0
851 1232 my $child = __PACKAGE__ . '::' . ucfirst(lc($name));
852 1232 ▶ ($child) = $self->inflate_children($child) if $inflate;
Branch: 1 branch
BranchTrueFalseTotal
if $inflate 8 1224 1232
853 1232 return $child;
854 }
855  
856 # returns either class names or inflated objects
857 408 ▶sub auto_children ($self, $inflate = 0) {
Subroutine auto_children: called CC=2 SCAR=6.9
858 408 map { $self->_auto_child($_, $inflate) } qw< help commands tree >;
859 }
860  
861 1 ▶sub auto_commands ($self) { return $self->_auto_child('commands', 1) }
Subroutine auto_commands: called CC=1 SCAR=0.0
862  
863 7 ▶sub auto_help ($self) { return $self->_auto_child('help', 1) }
Subroutine auto_help: called CC=1 SCAR=0.0
864  
865 0 ▶sub auto_tree ($self) { return $self->_auto_child('tree', 1) }
Subroutine auto_tree: not called CC=1 SCAR=6.9
866  
867 5 ▶sub run_help ($self, $mode = 'help') { $self->auto_help->run($mode) }
Subroutine run_help: called CC=2 SCAR=6.9
868  
869 1 ▶sub full_help_text ($s, @as) { $s->auto_help->collect_help_for($s, @as) }
Subroutine full_help_text: called CC=1 SCAR=0.0
870  
871 0 ▶sub load_module ($sop, $module) {
Subroutine load_module: not called CC=2 SCAR=17.9
872 0 my $file = "$module.pm" =~ s{::}{/}grmxs;
873 0 ▶ eval { require $file } or Carp::confess("module<$module>: $EVAL_ERROR");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file } } or Carp::confess("module<$module>: $App::Easer::V2::Command::EVAL_ERROR") 0 0 0
874 0 return $module;
875 }
876  
877 # Gets a specification like "Foo::Bar::baz" and returns a reference to
878 # sub "baz" in "Foo::Bar". If no package name is set, returns a
879 # reference to a sub in the package of $self. FIXME document properly
880 1006 ▶sub ref_to_sub ($self, $spec) {
Subroutine ref_to_sub: called CC=7 SCAR=19.8
881 1006 ▶ Carp::confess("undefined specification in ref_to_sub")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 0 1006 1006
882 unless defined $spec;
883 1006 ▶ return $spec if ref($spec) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "CODE" 97 909 1006
884 909 ▶ my ($class, $function) =
Branch: 1 branch
BranchTrueFalseTotal
ref $spec eq 'ARRAY' ? : 0 909 909
885 ref($spec) eq 'ARRAY'
886 ? $spec->@*
887 : $spec =~ m{\A (?: (.*) :: )? (.*) \z}mxs;
888 909 ▶ return $self->can($function) unless length($class // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($class // "") 908 1 909
Condition100%2/2
$class // ""
l!l
1908
MC/DC100%1/1
$class // ""
$class
Truth table100%2/2
A
A= $class
Aresult
11
00
889 1 ▶ $self->load_module($class) unless $class->can($function);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($function) 0 1 1
890 1 return $class->can($function);
891 } ## end sub ref_to_sub
892  
893 276 ▶sub instantiate ($sop, $class, @args) {
Subroutine instantiate: called CC=2 SCAR=7.2
894 276 ▶ $sop->load_module($class) unless $class->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 276 276
895 276 return $class->new(@args);
896 }
897  
898 276 ▶sub _reparent ($self, $child) {
Subroutine _reparent: called CC=6 SCAR=19.7
899 276 $child->parent($self);
900 276 $self->child($child); # saves a weak reference to $child
901  
902 # 2024-08-27 propagate sources configurations
903 276 ▶ if (! ref($child->_sources)) { # still default, my need to set it
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $child->_sources) 275 1 276
904 275 my ($first, @rest) = $self->sources;
905 275 ▶ if (ref($first) eq 'REF') { # new approach, propagate
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first eq "REF") 13 262 275
906 13 my $ssources = $$first;
907 13 $child->_sources(my $csources = { $ssources->%* });
908 13 ▶ if (my $next = $ssources->{next}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $ssources->{"next"}) 0 13 13
909 0 ▶ my @csources =
Branch: 2 branches
BranchTrueFalseTotal
ref $next eq 'ARRAY' ? : 0 0 0
ref $next eq 'CODE' ? : 0 0 0
910 ref($next) eq 'ARRAY' ? $next->@*
911 : ref($next) eq 'CODE' ? $next->($child)
912 : Carp::confess(); # no clue
913 0 $csources->{current} = \@csources;
914 }
915 }
916 }
917  
918 # propagate pre-execute callbacks down the line
919 276 $child->pre_execute_schedule($self->pre_execute);
920  
921 276 return $child;
922 }
923  
924 # transform one or more children "hints" into instances.
925 248 ▶sub inflate_children ($self, @hints) {
Subroutine inflate_children: called CC=4 SCAR=13.9
926 248 my $hashy = $self->hashy_class;
927 map {
928 276 my $child = $_;
929 276 ▶ if (!blessed($child)) { # actually inflate it
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($child)) 276 0 276
930 276 ▶ $child =
Branch: 2 branches
BranchTrueFalseTotal
ref $child eq 'ARRAY' ? : 42 234 276
ref $child eq 'HASH' ? : 85 149 234
931 ref($child) eq 'ARRAY' ? $self->instantiate($child->@*)
932 : ref($child) eq 'HASH' ? $self->instantiate($hashy, $child)
933 : $self->instantiate($child);
934 } ## end if (!blessed($child))
935 276 $self->_reparent($child); # returns $child
936 248 } grep { defined $_ } @hints;
937 } ## end sub inflate_children
938  
939 # fallback mechanism when finding a child, relies on fallback_to.
940 5 ▶sub fallback ($self) {
Subroutine fallback: called CC=4 SCAR=19.7
941 5 my $fto = $self->fallback_to;
942 5 ▶ return $fto if !defined($fto) || $fto !~ m{\A(?: 0 | [1-9]\d* )\z};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fto and $fto =~ /\A(?: 0 | [1-9]\d* )\z/u 5 0 5
Condition66%2/3
defined $fto and $fto =~ /\A(?: 0 | [1-9]\d* )\z/u
!ll&&!rl&&r
410
MC/DC0%0/2
defined $fto and $fto =~ /\A(?: 0 | [1-9]\d* )\z/u
defined $fto $fto =~ /\A(?: 0 | [1-9]\d* )\z/u
Truth table66%2/3
A and B
A= defined $ftoB= $fto =~ /\A(?: 0 | [1-9]\d* )\z/u
ABresult
0X0
100
111
943 0 my @children = $self->list_children;
944 0 ▶ return $children[$fto] if $fto <= $#children;
Branch: 1 branch
BranchTrueFalseTotal
if $fto <= $#children 0 0 0
945 0 return undef;
946 } ## end sub fallback ($self)
947  
948 # execute what's set as the execute sub in the slot
949 71 ▶sub execute ($self) {
Subroutine execute: called CC=3 SCAR=11.7
950 71 ▶ my $spec = $self->_rw or die "nothing to search for execution\n";
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $self->_rw or die "nothing to search for execution\n" 71 0 71
951 71 ▶ my $sub = $self->ref_to_sub($spec) or die "nothing to execute\n";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->ref_to_sub($spec) or die "nothing to execute\n" 71 0 71
952 71 return $sub->($self);
953 }
954  
955 276 ▶sub pre_execute_schedule ($self, @specs) {
Subroutine pre_execute_schedule: called CC=3 SCAR=15.1
956 276 ▶ if (my $spec = $self->_rw) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spec = $self->_rw) 0 276 276
957 0 ▶ my $sub = $self->ref_to_sub($spec) or die "nothing for pre_execute_schedule\n";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->ref_to_sub($spec) or die "nothing for pre_execute_schedule\n" 0 0 0
958 0 return $sub->($self, @specs);
959 }
960  
961 # default approach is to append to the current ones
962 276 $self->pre_execute([$self->pre_execute, @specs]);
963 276 return $self;
964 }
965  
966 123 ▶sub pre_execute_run ($self) {
Subroutine pre_execute_run: called CC=5 SCAR=25.9
967 123 ▶ if (my $spec = $self->_rw) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spec = $self->_rw) 0 123 123
968 0 ▶ my $sub = $self->ref_to_sub($spec) or die "nothing to pre-execute\n";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->ref_to_sub($spec) or die "nothing to pre-execute\n" 0 0 0
969 0 return $sub->($self);
970 }
971  
972 # default is to run 'em all
973 123 for my $spec ($self->pre_execute) {
974 0 ▶ my $sub = $self->ref_to_sub($spec) or die "nothing to pre-execute\n";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->ref_to_sub($spec) or die "nothing to pre-execute\n" 0 0 0
975 0 $sub->($self);
976 }
977 123 return $self;
978 }
979  
980 217 ▶sub run ($self, $name, @args) {
Subroutine run: called CC=2 SCAR=6.9
981 217 $self->call_name($name);
982 217 $self->collect(@args);
983 217 $self->commit;
984 217 $self->validate;
985 217 my ($child, @child_args) = $self->find_child;
986 213 ▶ return $child->run(@child_args) if defined $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child 90 123 213
987  
988 # we're the executors
989 123 $self->execution_reason($child_args[0]);
990 123 $self->final_collect; # no @args passed in this collection
991 123 $self->final_commit;
992 123 $self->pre_execute_run;
993 123 return $self->execute;
994 } ## end sub run
995  
996 package App::Easer::V2::Command::Commands;
997 27 push our @ISA, 'App::Easer::V2::Command';
998 35 ▶sub aliases { 'commands' }
Subroutine aliases: called CC=1 SCAR=0.0
999 14 ▶sub allow_residual_options { 0 }
Subroutine allow_residual_options: called CC=1 SCAR=0.0
1000 1 ▶sub description { 'Print list of supported sub-commands' }
Subroutine description: called CC=1 SCAR=0.0
1001 20 ▶sub help { 'list sub-commands' }
Subroutine help: called CC=1 SCAR=0.0
1002 0 ▶sub name { 'commands' }
Subroutine name: not called CC=1 SCAR=6.9
1003  
1004 80 ▶sub target ($self) {
Subroutine target: called CC=4 SCAR=14.0
1005 80 my ($subc, @rest) = $self->residual_args;
1006 80 ▶ die "this command does not support many arguments\n" if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 80 80
1007 80 my $target = $self->parent;
1008 80 ▶ $target = $target->find_matching_child($subc) if defined $subc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $subc 32 48 80
1009 80 ▶ die "cannot find sub-command '$subc'\n" unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 80 80
1010 80 return $target;
1011 } ## end sub target ($self)
1012  
1013 41 ▶sub list_commands_for ($self, $target = undef) {
Subroutine list_commands_for: called CC=8 SCAR=21.0
1014 41 ▶ $target //= $self->target;
Condition33%1/3
$target //= $self->target
l!l&&r!l&&!r
4100
MC/DC0%0/2
$target //= $self->target
$target $self->target
Truth table33%1/3
A //= B
A= $targetB= $self->target
ABresult
1X1
011
000
1015 41 my @lines;
1016 41 for my $command ($target->inflate_children($target->list_children)) {
1017 69 ▶ my $help = $command->help // '(**missing help**)';
Condition100%2/2
$command->help // "(**missing help**)"
l!l
672
MC/DC100%1/1
$command->help // "(**missing help**)"
$command->help
Truth table100%2/2
A
A= $command->help
Aresult
11
00
1018 69 my @aliases = $command->aliases;
1019 69 ▶ next unless @aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless @aliases 0 69 69
1020 69 push @lines, sprintf '%15s: %s', shift(@aliases), $help;
1021 69 ▶ push @lines, sprintf '%15s (also as: %s)', '', join ', ', @aliases
Branch: 1 branch
BranchTrueFalseTotal
if @aliases 21 48 69
1022 if @aliases;
1023 } ## end for my $command ($target...)
1024 41 ▶ return unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 22 19 41
1025 19 return join "\n", @lines;
1026 } ## end sub list_commands_for
1027  
1028 40 ▶sub _build_printout_facility ($self) {
Subroutine _build_printout_facility: called CC=12 SCAR=33.6
1029 40 my $channel = $self->target->help_channel;
1030 40 my $refch = ref $channel;
1031  
1032 40 ▶ return $channel if $refch eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $refch eq "CODE" 0 40 40
1033  
1034 40 my $fh;
1035 40 ▶ if ($refch eq 'GLOB') {
Branch: 3 branches
BranchTrueFalseTotal
if ($refch eq 'GLOB') { } 0 40 40
elsif ($refch eq 'SCALAR') { } 0 40 40
elsif ($refch) { } 0 40 40
1036 0 $fh = $channel;
1037 }
1038 elsif ($refch eq 'SCALAR') {
1039 0 ▶ open $fh, '>', $channel or die "open(): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $channel or die "open(): $!\n" 0 0 0
1040 }
1041 elsif ($refch) {
1042 0 die 'invalid channel';
1043 }
1044 else {
1045 40 ($channel, my $binmode) = split m{:}mxs, $channel, 2;
1046 40 ▶ if ($channel eq '-' || lc($channel) eq '-stdout') {
Branch: 2 branches
BranchTrueFalseTotal
if ($channel eq '-' or lc $channel eq '-stdout') { } 40 0 40
elsif (lc $channel eq '-stderr') { } 0 0 0
Condition33%1/3
$channel eq "-" or lc $channel eq "-stdout"
l!l&&r!l&&!r
0400
MC/DC0%0/2
$channel eq "-" or lc $channel eq "-stdout"
$channel eq "-" lc $channel eq "-stdout"
Truth table33%1/3
A or B
A= $channel eq "-"B= lc $channel eq "-stdout"
ABresult
1X1
011
000
1047 40 $fh = \*STDOUT;
1048 }
1049 elsif (lc($channel) eq '-stderr') {
1050 0 $fh = \*STDERR;
1051 }
1052 else {
1053 0 ▶ open $fh, '>', $channel or die "open('$channel'): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $channel or die "open('${channel}'): $!\n" 0 0 0
1054 }
1055 40 ▶ binmode $fh, $binmode if length($binmode // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($binmode // "") 40 0 40
Condition50%1/2
$binmode // ""
l!l
400
MC/DC0%0/1
$binmode // ""
$binmode
Truth table50%1/2
A
A= $binmode
Aresult
11
00
1056 }
1057  
1058 40 ▶ return sub ($cmd, @stuff) {
Subroutine __ANON__: called CC=1 SCAR=0.0
1059 40 print {$fh} @stuff;
1060 40 return $cmd;
1061 }
1062 40 }
1063  
1064 40 ▶sub printout ($self, @stuff) {
Subroutine printout: called CC=2 SCAR=7.1
1065 40 my $pof = $self->_rw;
1066 40 ▶ $self->_rw($pof = $self->_build_printout_facility) unless $pof;
Branch: 1 branch
BranchTrueFalseTotal
unless $pof 40 0 40
1067 40 $pof->($self, @stuff);
1068 }
1069  
1070 7 ▶sub execute ($self) {
Subroutine execute: called CC=3 SCAR=11.2
1071 7 my $target = $self->target;
1072 7 ▶ my $name = $target->call_name // $target->name;
Condition33%1/3
$target->call_name // $target->name
l!l&&r!l&&!r
700
MC/DC0%0/2
$target->call_name // $target->name
$target->call_name $target->name
Truth table33%1/3
A // B
A= $target->call_nameB= $target->name
ABresult
1X1
011
000
1073 7 ▶ if (defined(my $commands = $self->list_commands_for($target))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $commands = $self->list_commands_for($target))) { } 6 1 7
1074 6 $self->printout("sub-commands for $name\n", $commands, "\n");
1075 }
1076 else {
1077 1 $self->printout("no sub-commands for $name\n");
1078 }
1079 } ## end sub execute ($self)
1080  
1081 package App::Easer::V2::Command::Help;
1082 27 push our @ISA, 'App::Easer::V2::Command::Commands';
1083 27 our @aliases = qw< help usage >;
1084 64 ▶sub aliases { @aliases }
Subroutine aliases: called CC=1 SCAR=0.0
1085 66 ▶sub allow_residual_options { 0 }
Subroutine allow_residual_options: called CC=1 SCAR=0.0
1086 1 ▶sub description { 'Print help for (sub)command' }
Subroutine description: called CC=1 SCAR=0.0
1087 20 ▶sub help { 'print a help command' }
Subroutine help: called CC=1 SCAR=0.0
1088 0 ▶sub name { 'help' }
Subroutine name: not called CC=1 SCAR=6.9
1089  
1090 49 ▶sub __commandline_help ($getopt) {
Subroutine __commandline_help: called CC=27 SCAR=34.1
1091 49 my @retval;
1092  
1093 49 my ($mode, $type, $desttype, $min, $max, $default);
1094 49 ▶ if (substr($getopt, -1, 1) eq '!') {
Branch: 5 branches
BranchTrueFalseTotal
if (substr($getopt, -1, 1) eq '!') { } 15 34 49
elsif ($getopt =~ s/:\+ ([\@%])? \z//msux) { } 1 33 34
elsif (substr($getopt, -1, 1) eq '+') { } 1 32 33
elsif ($getopt =~ s/( [:=]) # 1 mode ([siof]) # 2 type ([\@%])? # 3 desttype (?: \{ (\d*)? # 4 min ,? (\d*)? # 5 max \} )? \z//msux) { } 18 14 32
elsif ($getopt =~ s/: (\d+) ([\@%])? \z//msux) { } 1 13 14
1095 15 $type = 'bool-negatable';
1096 15 substr $getopt, -1, 1, '';
1097 15 push @retval, 'boolean (can be negated)';
1098 }
1099 elsif ($getopt =~ s<:\+ ([@%])? \z><>mxs) {
1100 1 $mode = 'optional';
1101 1 $type = 'i';
1102 1 $default = 'increment';
1103 1 $desttype = $1;
1104 1 my $line = "integer, value is optional, defaults to incrementing current value";
1105 1 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 0 1 1
Condition33%1/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
100
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
1106 1 push @retval, $line;
1107 } ## end elsif ($getopt =~ s<:+ ([@%])? \z><>mxs)
1108 elsif (substr($getopt, -1, 1) eq '+') {
1109 1 $mode = 'increment';
1110 1 substr $getopt, -1, 1, '';
1111 1 push @retval,
1112 'incremental integer (adds 1 every time it is provided)';
1113 } ## end elsif (substr($getopt, -1...))
1114 elsif (
1115 $getopt =~ s<(
1116 [:=]) # 1 mode
1117 ([siof]) # 2 type
1118 ([@%])? # 3 desttype
1119 (?:
1120 \{
1121 (\d*)? # 4 min
1122 ,?
1123 (\d*)? # 5 max
1124 \}
1125 )? \z><>mxs
1126 )
1127 {
1128 18 ▶ $mode = $1 eq '=' ? 'required' : 'optional';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '=' ? : 17 1 18
1129 18 $type = $2;
1130 18 $desttype = $3;
1131 18 $min = $4;
1132 18 $max = $5;
1133 18 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 0 18 18
1134 0 ▶ $mode = $min ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$min ? : 0 0 0
1135 }
1136 $type = {
1137 s => 'string',
1138 i => 'integer',
1139 o => 'perl-extended-integer',
1140 f => 'float',
1141 18 }->{$type};
1142 18 my $line = "$type, value is $mode";
1143 18 ▶ $line .= ", at least $min times" if defined($min) && $min > 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $min > 1 0 18 18
Condition33%1/3
defined $min and $min > 1
!ll&&!rl&&r
1800
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
1144 18 ▶ $line .= ", no more than $max times"
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and length $max 0 18 18
Condition33%1/3
defined $max and length $max
!ll&&!rl&&r
1800
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
1145 if defined($max) && length($max);
1146 18 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 2 16 18
Condition66%2/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
1602
MC/DC50%1/2
defined $desttype and $desttype eq "\@"
defined $desttype $desttype eq "\@"
Truth table66%2/3
A and B
A= defined $desttypeB= $desttype eq "\@"
ABresult
0X0
100
111
1147 18 push @retval, $line;
1148 } ## end elsif ($getopt =~ s<( ) )
1149 elsif ($getopt =~ s<: (\d+) ([@%])? \z><>mxs) {
1150 1 $mode = 'optional';
1151 1 $type = 'i';
1152 1 $default = $1;
1153 1 $desttype = $2;
1154 1 my $line = "integer, value is optional, defaults to $default";
1155 1 ▶ $line .= ", list valued" if defined($desttype) && $desttype eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if defined $desttype and $desttype eq "\@" 0 1 1
Condition33%1/3
defined $desttype and $desttype eq "\@"
!ll&&!rl&&r
100
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
1156 1 push @retval, $line;
1157 } ## end elsif ($getopt =~ s<: (\d+) ([@%])? \z><>mxs)
1158 else { # boolean, non-negatable
1159 13 $type = 'bool';
1160 13 push @retval, 'boolean';
1161 }
1162  
1163 49 my @alternatives = split /\|/, $getopt;
1164 49 ▶ if ($type eq 'bool-negatable') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'bool-negatable') { } 15 34 49
elsif ($type eq 'bool' or $mode eq 'increment') { } 14 20 34
elsif ($mode eq 'optional') { } 3 17 20
1165 push @retval, map {
1166 15 ▶ if (length($_) == 1) { "-$_" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 13 16 29
1167 16 else { "--$_ | --no-$_" }
1168 } @alternatives;
1169 } ## end if ($type eq 'bool')
1170 ▶ elsif ($type eq 'bool' || $mode eq 'increment') {
Condition100%3/3
$type eq "bool" or $mode eq "increment"
l!l&&r!l&&!r
13120
MC/DC100%2/2
$type eq "bool" or $mode eq "increment"
$type eq "bool" $mode eq "increment"
Truth table100%3/3
A or B
A= $type eq "bool"B= $mode eq "increment"
ABresult
1X1
011
000
1171 push @retval, map {
1172 14 ▶ if (length($_) == 1) { "-$_" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 1 14 15
1173 14 else { "--$_" }
1174 } @alternatives;
1175 } ## end if ($type eq 'bool')
1176 elsif ($mode eq 'optional') {
1177 push @retval, map {
1178 0 ▶ if (length($_) == 1) { "-$_ [<value>]" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 0 3 3
1179 3 else { "--$_ [<value>]" }
1180 } @alternatives;
1181 } ## end elsif ($mode eq 'optional')
1182 else {
1183 push @retval, map {
1184 17 ▶ if (length($_) == 1) { "-$_ <value>" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 13 17 30
1185 17 else { "--$_ <value>" }
1186 } @alternatives;
1187 } ## end else [ if ($type eq 'bool') ]
1188  
1189 49 return @retval;
1190 } ## end sub __commandline_help ($getopt)
1191  
1192 33 ▶sub execute ($self) {
Subroutine execute: called CC=1 SCAR=0.0
1193 33 $self->printout($self->collect_help_for($self->target, $self->call_name));
1194 33 return 0;
1195 }
1196  
1197 49 ▶sub real_environment_variable_name ($self, $target, $opt) {
Subroutine real_environment_variable_name: called CC=5 SCAR=16.8
1198 49 my $name = $target->name_for_option($opt);
1199 49 ▶ while ($target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target) 58 0 58
1200 return $target->environment_variable_name($opt)
1201 58 ▶ if $opt && ! $opt->{inherited};
Branch: 1 branch
BranchTrueFalseTotal
if $opt and not $opt->{"inherited"} 49 9 58
Condition66%2/3
$opt and not $opt->{"inherited"}
!ll&&!rl&&r
0949
MC/DC50%1/2
$opt and not $opt->{"inherited"}
$opt not $opt->{"inherited"}
Truth table66%2/3
A and not(B)
A= $optB= not $opt->{"inherited"}
ABresult
0X0
100
111
1202  
1203 # here, either the option is not available at this level (which is
1204 # in any case not possible, as inheritance is always from the direct
1205 # parent) or it's inherited. Move on to the parent to look for the
1206 # real source of info.
1207 9 ▶ $target = $target->parent // die 'weird inheritance, uh?!?';
Condition50%1/2
$target->parent // die("weird inheritance, uh?!?")
l!l
90
MC/DC0%0/1
$target->parent // die("weird inheritance, uh?!?")
$target->parent
Truth table50%1/2
A
A= $target->parent
Aresult
11
00
1208 ($opt) = # search the parent's options, very inefficient but still...
1209 9 grep { $target->name_for_option($_) eq $name } $target->options;
1210 }
1211 # we should never arrive here for real options
1212 0 return;
1213 }
1214  
1215 34 ▶sub collect_help_for ($self, $target, $mode = 'help') {
Subroutine collect_help_for: called CC=24 SCAR=32.5
1216 34 my @stuff;
1217  
1218 26 ▶ my $trim_and_prefix = sub ($text, $prefix = ' ') {
Subroutine __ANON__: called CC=2 SCAR=6.9
1219 26 $text =~ s{\A\s+|\s+\z}{}gmxs; # trim
1220 26 $text =~ s{^}{$prefix}gmxs; # add some indentation
1221 26 return $text;
1222 34 };
1223  
1224 34 ▶ push @stuff, ($target->help // 'no concise help yet'), "\n\n";
Condition100%2/2
$target->help // "no concise help yet"
l!l
295
MC/DC100%1/1
$target->help // "no concise help yet"
$target->help
Truth table100%2/2
A
A= $target->help
Aresult
11
00
1225  
1226 34 ▶ if ($mode eq 'help' && defined(my $description = $target->description)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help" and defined(my $description = $target->description)) 26 8 34
Condition66%2/3
$mode eq "help" and defined(my $description = $target->description)
!ll&&!rl&&r
0826
MC/DC50%1/2
$mode eq "help" and defined(my $description = $target->description)
$mode eq "help" defined(my $description = $target->description)
Truth table66%2/3
A and B
A= $mode eq "help"B= defined(my $description = $target->description)
ABresult
0X0
100
111
1227 26 push @stuff, "Description:\n", $trim_and_prefix->($description), "\n\n";
1228 }
1229  
1230 34 ▶ if (defined(my $usage = $target->usage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $usage = $target->usage)) 0 34 34
1231 0 push @stuff, "Usage:\n", $trim_and_prefix->($usage), "\n\n";
1232 }
1233  
1234 # Print this only for sub-commands, not for the root
1235 34 ▶ push @stuff, sprintf "Can be called as: %s\n\n", join ', ',
Branch: 1 branch
BranchTrueFalseTotal
if $target->parent 18 16 34
1236 $target->aliases
1237 if $target->parent;
1238  
1239 34 my @options = $target->options;
1240 34 my $options_help = $target->options_help;
1241 34 ▶ if (@options || defined($options_help)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options or defined $options_help) { } 28 6 34
Condition66%2/3
@options or defined $options_help
l!l&&r!l&&!r
2806
MC/DC50%1/2
@options or defined $options_help
@options defined $options_help
Truth table66%2/3
A or B
A= @optionsB= defined $options_help
ABresult
1X1
011
000
1242 28 push @stuff, "Options:\n";
1243  
1244 28 ▶ $options_help //= {};
Condition50%1/2
$options_help //= {}
l!l
028
MC/DC0%0/1
$options_help //= {}
$options_help
Truth table50%1/2
A
A= $options_help
Aresult
11
00
1245 28 ▶ if (! ref($options_help)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $options_help) { } 0 28 28
1246 0 push @stuff, $trim_and_prefix->($options_help), "\n\n";
1247 }
1248 else {
1249 28 ▶ my $preamble = $options_help->{preamble} // undef;
Condition50%1/2
$options_help->{"preamble"} // undef
l!l
028
MC/DC0%0/1
$options_help->{"preamble"} // undef
$options_help->{"preamble"}
Truth table50%1/2
A
A= $options_help->{"preamble"}
Aresult
11
00
1250 28 ▶ push @stuff, $trim_and_prefix->($preamble), "\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $preamble 0 28 28
1251 if defined($preamble);
1252  
1253 28 my $n = 0; # count the option
1254 28 for my $opt (@options) {
1255 49 ▶ push @stuff, "\n" if $n++; # from second line on
Branch: 1 branch
BranchTrueFalseTotal
if $n++ 21 28 49
1256  
1257 push @stuff, sprintf "%15s: %s\n", $target->name_for_option($opt),
1258 49 ▶ $opt->{help} // '';
Condition100%2/2
$opt->{"help"} // ""
l!l
742
MC/DC100%1/1
$opt->{"help"} // ""
$opt->{"help"}
Truth table100%2/2
A
A= $opt->{"help"}
Aresult
11
00
1259  
1260 49 ▶ if (exists $opt->{getopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"getopt"}) 49 0 49
1261 49 my @lines = __commandline_help($opt->{getopt});
1262 49 push @stuff, sprintf "%15s command-line: %s\n", '',
1263 shift(@lines);
1264 push @stuff,
1265 49 map { sprintf "%15s %s\n", '', $_ } @lines;
1266 } ## end if (exists $opt->{getopt...})
1267  
1268 49 ▶ if (defined(my $env = $self->real_environment_variable_name($target, $opt))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $env = $self->real_environment_variable_name($target, $opt))) 20 29 49
1269 20 push @stuff, sprintf "%15s environment: %s\n", '', $env;
1270 }
1271  
1272 49 ▶ if (exists($opt->{default})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"default"}) 17 32 49
1273 17 my $default = $opt->{default};
1274 my $print = ! defined($default) ? '*undef*'
1275 : ! ref($default) ? $default
1276 17 ▶ : do { require JSON::PP; JSON::PP::encode_json($default) };
Branch: 2 branches
BranchTrueFalseTotal
!defined($default) ? : 0 17 17
!ref($default) ? : 16 1 17
1277 17 push @stuff, sprintf "%15s default: %s\n", '', $print;
1278 }
1279 } ## end for my $opt (@options)
1280  
1281 28 ▶ my $postamble = $options_help->{postamble} // undef;
Condition50%1/2
$options_help->{"postamble"} // undef
l!l
028
MC/DC0%0/1
$options_help->{"postamble"} // undef
$options_help->{"postamble"}
Truth table50%1/2
A
A= $options_help->{"postamble"}
Aresult
11
00
1282 28 ▶ push @stuff, "\n", $trim_and_prefix->($postamble), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $postamble 0 28 28
1283 if defined($postamble);
1284 }
1285  
1286 28 push @stuff, "\n";
1287 } ## end if (my @options = $target...)
1288 else {
1289 6 push @stuff, "This command has no option\n";
1290 }
1291  
1292 34 ▶ if (defined(my $commands = $self->list_commands_for($target))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $commands = $self->list_commands_for($target))) { } 13 21 34
1293 13 push @stuff, "Sub-commands:\n", $commands, "\n";
1294 }
1295 else {
1296 21 push @stuff, "No sub-commands\n";
1297 }
1298  
1299 34 return join '', @stuff;
1300 } ## end sub execute ($self)
1301  
1302 package App::Easer::V2::Command::Tree;
1303 27 push our @ISA, 'App::Easer::V2::Command::Commands';
1304 27 ▶sub aliases { 'tree' }
Subroutine aliases: called CC=1 SCAR=0.0
1305 1 ▶sub description { 'Print tree of supported sub-commands' }
Subroutine description: called CC=1 SCAR=0.0
1306 20 ▶sub help { 'print sub-commands in a tree' }
Subroutine help: called CC=1 SCAR=0.0
1307 0 ▶sub name { 'tree' }
Subroutine name: not called CC=1 SCAR=6.9
1308  
1309 1 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
1310 return (
1311 {
1312 1 getopt => 'include_auto|include-auto|I!',
1313 default => 0,
1314 environment => 1,
1315 },
1316 );
1317 } ## end sub options
1318  
1319 0 ▶sub list_commands_for ($self, $target) {
Subroutine list_commands_for: not called CC=9 SCAR=45.0
1320 0 ▶ my $exclude_auto = $self->config('include_auto') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->config('include_auto') ? : 0 0 0
1321 0 my @lines;
1322 0 for my $command ($target->inflate_children($target->list_children)) {
1323 0 ▶ my ($name) = $command->aliases or next;
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $command->aliases or next 0 0 0
1324 next
1325 0 ▶ if $name =~ m{\A(?: help | commands | tree)\z}mxs && $exclude_auto;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A(?: help | commands | tree)\z/msux and $exclude_auto 0 0 0
Condition0%0/3
$name =~ /\A(?: help | commands | tree)\z/msux and $exclude_auto
!ll&&!rl&&r
000
MC/DC0%0/2
$name =~ /\A(?: help | commands | tree)\z/msux and $exclude_auto
$name =~ /\A(?: help | commands | tree)\z/msux $exclude_auto
Truth table0%0/3
A and B
A= $name =~ /\A(?: help | commands | tree)\z/msuxB= $exclude_auto
ABresult
0X0
100
111
1326 0 ▶ my $help = $command->help // '(**missing help**)';
Condition0%0/2
$command->help // "(**missing help**)"
l!l
00
MC/DC0%0/1
$command->help // "(**missing help**)"
$command->help
Truth table0%0/2
A
A= $command->help
Aresult
11
00
1327 0 push @lines, sprintf '- %s (%s)', $name, $help;
1328 0 ▶ if (defined(my $subtree = $self->list_commands_for($command))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subtree = $self->list_commands_for($command))) 0 0 0
1329 0 push @lines, $subtree =~ s{^}{ }rgmxs;
1330 }
1331 } ## end for my $command ($target...)
1332 0 ▶ return unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 0 0
1333 0 return join "\n", @lines;
1334 } ## end sub list_commands_for
1335  
1336 27 1;
« Easer/V1.pm ↑ Summary Easer/ConfigHash.pm »