Simple.pm

filter: Statementstmt 68.7% 317 / 461 Branchbran 47.3% 89 / 188 Conditioncond 32.1% 62 / 193 MC/DCmcdc 13.3% 16 / 120 Subroutinesub 77.4% 48 / 62 Podpod 68.7% 11 / 16 totaltotal 52.2% 543 / 1040 CC 172 SCAR 46.5
CC
172
Cov
56%
init_logger
348.5
new
165.4
main
156.0
CRAP 104.5
↑ Summary Simple/Scaffold.pm »
1 package CLI::Simple;
2 # a Simple, Fast & Easy way to create scripts
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use CLI::Simple::Constants qw(:booleans :chars :log-levels @VALID_OPTIONS @DEFAULT_HELP_SECTIONS :color-config);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use CLI::Simple::Utils qw(normalize_options slurp dmp choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use CLI::Simple::DumpSpec qw(_cmd_dump_spec);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use CLI::Simple::Migrate qw(_cmd_migrate);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use CLI::Simple::Scaffold qw(_cmd_scaffold);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use CLI::Simple::Helpers qw(_is_class_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use CLI::Simple::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use FindBin qw($RealBin $RealScript);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use List::Util qw(none pairs any);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 our $VERSION = '2.2.5';
27  
28 7 our $GETOPT_EXIT_ON_ERROR = $TRUE;
29 7 our $GETOPT_STATUS;
30 7 our $GETOPT_ERROR_MESSAGE;
31  
32 7 my %GENERATED_ACCESSOR; # tracks accessors mk_accessors itself created, per class
33  
34 7 __PACKAGE__->follow_best_practice;
35 7 __PACKAGE__->mk_accessors(
36 qw(
37 _validate_command
38 _command
39 _command_args
40 _commands
41 _program
42 _abbreviations
43 )
44 );
45  
46 7 our $USE_LOGGER = $FALSE;
47 7 our $AUTO_DEFAULT = $FALSE;
48 7 our $AUTO_HELP = $FALSE;
49 7 our $PAGER = $TRUE;
50  
51 7 our %INTERNAL_COMMANDS = (
52 '-generate-completion' => \&_cmd_generate_completion,
53 '-migrate' => \&_cmd_migrate,
54 '-dump-spec' => \&_cmd_dump_spec,
55 '-scaffold' => \&_cmd_scaffold,
56 );
57  
58 7 our @EXPORT_OK = qw($AUTO_HELP $AUTO_DEFAULT $PAGER $USE_LOGGER);
59  
60 7 ▶use parent qw(Exporter Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 # CLI::Simple 2.0.0 additions
63  
64 7 our $MANIFEST; # package-level, per-consumer class
65  
66 7 ▶caller or exit __PACKAGE__->main();
Branch: 1 branch
BranchTrueFalseTotal
caller or exit __PACKAGE__->main 7 0 7
67  
68 ########################################################################
69 10 ▶sub import {
Subroutine import: called CC=7 SCAR=32.4
70 ########################################################################
71 10 my ( $class, @args ) = @_;
72  
73 10 ▶ if ( any { $_ eq ':roles' } @args ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq ":roles"; } , @args)) 0 10 10
74 0 my $caller = caller;
75  
76 0 ▶ if ( !${^COMPILING} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (${^COMPILING}) 0 0 0
77 0 ( my $dist = $caller ) =~ s/::/-/gxsm;
78 0 my $yaml_file = lc($dist) . '.yml';
79  
80 0 require File::ShareDir;
81 0 my $path = eval { File::ShareDir::dist_file( $dist, $yaml_file ) };
82 0 ▶ undef $path if $path && !-e $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path and not -e $path 0 0 0
Condition0%0/3
$path and not -e $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path and not -e $path
$path not -e $path
Truth table0%0/3
A and not(B)
A= $pathB= not -e $path
ABresult
0X0
100
111
83  
84 0 ▶ $class->_load_manifest( $caller, $path ) if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
85 }
86 }
87  
88 # preserve existing Exporter behaviour
89 10 ▶ $class->export_to_level( 1, $class, grep { $_ ne ':roles' && !/[.]ya?ml\z/xsm } @args );
Condition33%1/3
$_ ne ':roles' && !/[.]ya?ml\z/msx
!ll&&!rl&&r
004
MC/DC0%0/2
$_ ne ':roles' && !/[.]ya?ml\z/msx
$_ ne ':roles' !/[.]ya?ml\z/msx
Truth table33%1/3
A && not(B)
A= $_ ne ':roles'B= !/[.]ya?ml\z/msx
ABresult
0X0
100
111
90  
91 10 return;
92 }
93  
94 ########################################################################
95 4 ▶sub _load_manifest {
Subroutine _load_manifest: called CC=6 SCAR=18.0
96 ########################################################################
97 4 my ( $class, $target, $yaml_file ) = @_;
98  
99 4 require YAML::Tiny;
100  
101 4 ▶ my $manifest = YAML::Tiny::LoadFile($yaml_file)
Branch: 1 branch
BranchTrueFalseTotal
my $manifest = YAML::Tiny::LoadFile($yaml_file) or die "ERROR: could not load manifest: $yaml_file\n" 4 0 4
102 or die "ERROR: could not load manifest: $yaml_file\n";
103  
104 4 ▶ if ( my $commands = $manifest->{commands} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $commands = $manifest->{"commands"}) 4 0 4
105  
106 # derive unique roles from command values
107 4 my %seen;
108 4 ▶ my @roles = grep { !$seen{$_}++ && _is_class_name($_) } values %{$commands};
Condition66%2/3
!$seen{$_}++ && _is_class_name($_)
!ll&&!rl&&r
205
MC/DC50%1/2
!$seen{$_}++ && _is_class_name($_)
!$seen{$_}++ _is_class_name($_)
Truth table66%2/3
not(A) && B
A= !$seen{$_}++B= _is_class_name($_)
ABresult
0X0
100
111
109  
110 4 require Role::Tiny;
111 4 Role::Tiny->apply_roles_to_package( $target, @roles );
112  
113 # build dispatch table: 'code-review' => \&cmd_code_review
114 4 my %dispatch;
115 4 for my $cmd ( keys %{$commands} ) {
116 7 my $value = $commands->{$cmd};
117  
118 7 ▶ my $method = choose {
Subroutine __ANON__: called CC=2 SCAR=7.1
119 7 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless _is_class_name($value) 0 7 7
120 if !_is_class_name($value);
121  
122 # derive method from command key, not role class name
123 # create-config -> cmd_create_config
124 # install -> cmd_install
125 7 ( my $m = "cmd_$cmd" ) =~ s/-/_/gxsm;
126 7 return $m;
127 7 };
128  
129 7 ▶ die sprintf "ERROR: %s does not implement %s\n", $value, $method
Branch: 1 branch
BranchTrueFalseTotal
unless $target->can($method) 1 6 7
130 if !$target->can($method);
131  
132 6 $dispatch{$cmd} = $target->can($method);
133 }
134  
135 3 $manifest->{_dispatch} = \%dispatch;
136 }
137  
138 # store on the target class
139 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
140 3 ${"${target}::_CLI_MANIFEST"} = $manifest;
141  
142 3 return;
143 }
144  
145 ########################################################################
146 3 ▶sub _manifest {
Subroutine _manifest: called CC=1 SCAR=0.0
147 ########################################################################
148 3 my ($class) = @_;
149  
150 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
151 3 return ${"${class}::_CLI_MANIFEST"};
152 }
153  
154 ########################################################################
155 0 ▶sub main {
Subroutine main: not called CC=12 SCAR=50.5
Pod documented
156 ########################################################################
157 0 my ($class) = @_;
158  
159 0 my $manifest = $class->_manifest;
160  
161 my $cli = $class->new(
162 option_specs => $manifest ? ( $manifest->{options} // [] ) : [],
163 alias => $manifest ? ( $manifest->{alias} // {} ) : {},
164 default_options => $manifest ? ( $manifest->{default_options} // {} ) : {},
165 extra_options => $manifest ? ( $manifest->{extra_options} // [] ) : [],
166 commands => $manifest ? $manifest->{_dispatch} : { default => \&usage },
167 0 ▶ abbreviations => $manifest ? ( $manifest->{abbreviations} // $FALSE ) : $FALSE,
Branch: 6 branches
BranchTrueFalseTotal
$manifest ? : 0 0 0
$manifest ? : 0 0 0
$manifest ? : 0 0 0
$manifest ? : 0 0 0
$manifest ? : 0 0 0
$manifest ? : 0 0 0
Condition0%0/11
$manifest->{"options"} // []
l!l
00
$manifest->{"alias"} // {}
l!l
00
$manifest->{"default_options"} // {}
l!l
00
$manifest->{"extra_options"} // []
l!l
00
$manifest->{"abbreviations"} // $CLI::Simple::FALSE
l!l&&r!l&&!r
000
MC/DC0%0/1
$manifest->{"options"} // []
$manifest->{"options"}
MC/DC0%0/1
$manifest->{"alias"} // {}
$manifest->{"alias"}
MC/DC0%0/1
$manifest->{"default_options"} // {}
$manifest->{"default_options"}
MC/DC0%0/1
$manifest->{"extra_options"} // []
$manifest->{"extra_options"}
MC/DC0%0/2
$manifest->{"abbreviations"} // $CLI::Simple::FALSE
$manifest->{"abbreviations"} $CLI::Simple::FALSE
Truth table0%0/2
A
A= $manifest->{"options"}
Aresult
11
00
Truth table0%0/2
A
A= $manifest->{"alias"}
Aresult
11
00
Truth table0%0/2
A
A= $manifest->{"default_options"}
Aresult
11
00
Truth table0%0/2
A
A= $manifest->{"extra_options"}
Aresult
11
00
Truth table0%0/3
A // B
A= $manifest->{"abbreviations"}B= $CLI::Simple::FALSE
ABresult
1X1
011
000
168 );
169  
170 0 return $cli->run;
171 }
172  
173 ########################################################################
174 35 ▶sub _use_logger {
Subroutine _use_logger: called CC=1 SCAR=0.0
175 ########################################################################
176 35 return $USE_LOGGER;
177 }
178  
179 ########################################################################
180 0 ▶sub use_log4perl {
Subroutine use_log4perl: not called CC=12 SCAR=49.1
Pod documented
181 ########################################################################
182 0 my ( $self, %args ) = @_;
183  
184 0 my @valid_options = qw(
185 log_level
186 level
187 loglevel
188 log-level
189 config
190 color
191 debug_color
192 info_color
193 warn_color
194 error_color
195 trace_color
196 fatal_color
197 );
198  
199 0 foreach my $o ( keys %args ) {
200 die "ERROR: unknown argument ($o)\n"
201 0 ▶ if none { $o eq $_ } @valid_options;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $o eq $_; } , @valid_options) 0 0 0
202 }
203  
204 0 ▶ eval { require Log::Log4perl; 1; }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Log::Log4perl; 1 } } or die "use_log4perl() requires Log::Log4perl, which is not installed.\nIt is an opt-in feature, so CLI::Simple does not depend on it.\nIf your app calls use_log4perl(), add 'Log::Log4perl' to your requires/cpanfile.\n" 0 0 0
205 or die "use_log4perl() requires Log::Log4perl, which is not installed.\n"
206 . "It is an opt-in feature, so CLI::Simple does not depend on it.\n"
207 . "If your app calls use_log4perl(), add 'Log::Log4perl' to your requires/cpanfile.\n";
208  
209 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
210  
211 die "ERROR: color and config are mutually exclusive - color uses CLI::Simple's built-in colorized config\n"
212 0 ▶ if $args{color} && $args{config};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"color"} and $args{"config"} 0 0 0
Condition0%0/3
$args{"color"} and $args{"config"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"color"} and $args{"config"}
$args{"color"} $args{"config"}
Truth table0%0/3
A and B
A= $args{"color"}B= $args{"config"}
ABresult
0X0
100
111
213  
214 0 my ($level) = grep {defined} @args{qw(log_level log-level loglevel level)};
215 0 ▶ $level //= 'error';
Condition0%0/2
$level //= "error"
l!l
00
MC/DC0%0/1
$level //= "error"
$level
Truth table0%0/2
A
A= $level
Aresult
11
00
216  
217 0 my $log4perl_conf = $args{config};
218  
219 0 ▶ if ( $args{color} && !$log4perl_conf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"color"} and not $log4perl_conf) 0 0 0
Condition0%0/3
$args{"color"} and not $log4perl_conf
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"color"} and not $log4perl_conf
$args{"color"} not $log4perl_conf
Truth table0%0/3
A and not(B)
A= $args{"color"}B= not $log4perl_conf
ABresult
0X0
100
111
220 0 $log4perl_conf = $self->_set_color_config(%args);
221 }
222  
223 {
224 0 $USE_LOGGER = $TRUE;
225  
226 7 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 ▶ *{"${class}::get_log4perl_conf"} = sub { return $log4perl_conf };
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 0 ▶ *{"${class}::set_log4perl_conf"} = sub { $log4perl_conf = $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 ▶ *{"${class}::get_log4perl_level"} = sub { return $level };
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 }
231  
232 0 ▶ if ( !$self->can('set_logger') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("set_logger")) 0 0 0
233 0 $self->mk_accessors('logger');
234 0 $GENERATED_ACCESSOR{$class}{logger} = $TRUE;
235 }
236  
237 0 ▶ if ( !$self->can('set_log_level') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("set_log_level")) 0 0 0
238 0 $self->mk_accessors('log_level');
239 0 $GENERATED_ACCESSOR{$class}{log_level} = $TRUE;
240 }
241  
242 0 return $self;
243 }
244  
245 ########################################################################
246 21 ▶sub new {
Subroutine new: called CC=71 SCAR=51.1
Pod documented
247 ########################################################################
248 21 my ( $class, @params ) = @_;
249  
250 21 ▶ my %args = ref $params[0] ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
ref $params[0] ? : 0 21 21
251  
252 21 foreach my $o ( keys %args ) {
253 die "ERROR: unknown option '$o'\n"
254 47 ▶ if none { $o eq $_ } @VALID_OPTIONS;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $o eq $_; } , @CLI::Simple::VALID_OPTIONS) 1 46 47
255 }
256  
257 my (
258 $default_options, $option_specs, $commands, $extra_options, $abbreviations,
259 $error_handler, $alias, $validate_command, $help_sections
260 )
261 = @args{
262 20 qw(default_options option_specs commands extra_options abbreviations error_handler alias validate_command help_sections)};
263  
264 20 ▶ $validate_command //= $TRUE;
Condition33%1/3
$validate_command //= $CLI::Simple::TRUE
l!l&&r!l&&!r
0200
MC/DC0%0/2
$validate_command //= $CLI::Simple::TRUE
$validate_command $CLI::Simple::TRUE
Truth table33%1/3
A //= B
A= $validate_commandB= $CLI::Simple::TRUE
ABresult
1X1
011
000
265  
266 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 20 my $stash = \%{ $class . $DOUBLE_COLON };
269  
270 20 local *alias = *alias; ## no critic ProhibitLocalVars
271  
272 7 ▶ use vars qw($DEFAULT_OPTIONS $EXTRA_OPTIONS $OPTION_SPECS $COMMANDS $LOGGING);
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 20 ▶ *DEFAULT_OPTIONS = $stash->{DEFAULT_OPTIONS} // $EMPTY;
Condition66%2/3
$stash->{"DEFAULT_OPTIONS"} // $CLI::Simple::EMPTY
l!l&&r!l&&!r
1604
MC/DC50%1/2
$stash->{"DEFAULT_OPTIONS"} // $CLI::Simple::EMPTY
$stash->{"DEFAULT_OPTIONS"} $CLI::Simple::EMPTY
Truth table66%2/3
A // B
A= $stash->{"DEFAULT_OPTIONS"}B= $CLI::Simple::EMPTY
ABresult
1X1
011
000
275 20 ▶ *EXTRA_OPTIONS = $stash->{EXTRA_OPTIONS} // $EMPTY;
Condition66%2/3
$stash->{"EXTRA_OPTIONS"} // $CLI::Simple::EMPTY
l!l&&r!l&&!r
1604
MC/DC50%1/2
$stash->{"EXTRA_OPTIONS"} // $CLI::Simple::EMPTY
$stash->{"EXTRA_OPTIONS"} $CLI::Simple::EMPTY
Truth table66%2/3
A // B
A= $stash->{"EXTRA_OPTIONS"}B= $CLI::Simple::EMPTY
ABresult
1X1
011
000
276 20 ▶ *OPTION_SPECS = $stash->{OPTION_SPECS} // $EMPTY;
Condition66%2/3
$stash->{"OPTION_SPECS"} // $CLI::Simple::EMPTY
l!l&&r!l&&!r
1604
MC/DC50%1/2
$stash->{"OPTION_SPECS"} // $CLI::Simple::EMPTY
$stash->{"OPTION_SPECS"} $CLI::Simple::EMPTY
Truth table66%2/3
A // B
A= $stash->{"OPTION_SPECS"}B= $CLI::Simple::EMPTY
ABresult
1X1
011
000
277 20 ▶ *COMMANDS = $stash->{COMMANDS} // $EMPTY;
Condition66%2/3
$stash->{"COMMANDS"} // $CLI::Simple::EMPTY
l!l&&r!l&&!r
1604
MC/DC50%1/2
$stash->{"COMMANDS"} // $CLI::Simple::EMPTY
$stash->{"COMMANDS"} $CLI::Simple::EMPTY
Truth table66%2/3
A // B
A= $stash->{"COMMANDS"}B= $CLI::Simple::EMPTY
ABresult
1X1
011
000
278  
279 20 ▶ $default_options //= $DEFAULT_OPTIONS;
Condition33%1/3
$default_options //= $CLI::Simple::DEFAULT_OPTIONS
l!l&&r!l&&!r
0020
MC/DC0%0/2
$default_options //= $CLI::Simple::DEFAULT_OPTIONS
$default_options $CLI::Simple::DEFAULT_OPTIONS
Truth table33%1/3
A //= B
A= $default_optionsB= $CLI::Simple::DEFAULT_OPTIONS
ABresult
1X1
011
000
280 20 ▶ $extra_options //= $EXTRA_OPTIONS;
Condition33%1/3
$extra_options //= $CLI::Simple::EXTRA_OPTIONS
l!l&&r!l&&!r
0020
MC/DC0%0/2
$extra_options //= $CLI::Simple::EXTRA_OPTIONS
$extra_options $CLI::Simple::EXTRA_OPTIONS
Truth table33%1/3
A //= B
A= $extra_optionsB= $CLI::Simple::EXTRA_OPTIONS
ABresult
1X1
011
000
281 20 ▶ $option_specs //= $OPTION_SPECS;
Condition66%2/3
$option_specs //= $CLI::Simple::OPTION_SPECS
l!l&&r!l&&!r
1703
MC/DC50%1/2
$option_specs //= $CLI::Simple::OPTION_SPECS
$option_specs $CLI::Simple::OPTION_SPECS
Truth table66%2/3
A //= B
A= $option_specsB= $CLI::Simple::OPTION_SPECS
ABresult
1X1
011
000
282 20 ▶ $commands //= $COMMANDS;
Condition33%1/3
$commands //= $CLI::Simple::COMMANDS
l!l&&r!l&&!r
2000
MC/DC0%0/2
$commands //= $CLI::Simple::COMMANDS
$commands $CLI::Simple::COMMANDS
Truth table33%1/3
A //= B
A= $commandsB= $CLI::Simple::COMMANDS
ABresult
1X1
011
000
283  
284 20 ▶ $option_specs //= [];
Condition100%2/2
$option_specs //= []
l!l
173
MC/DC100%1/1
$option_specs //= []
$option_specs
Truth table100%2/2
A
A= $option_specs
Aresult
11
00
285  
286 20 ▶ croak sprintf "ERROR: 'commands' is required\nusage: %s->new( option_specs => specs, commands => commands);\n", __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless $option_specs and $commands 0 20 20
Condition33%1/3
$option_specs and $commands
!ll&&!rl&&r
0020
MC/DC0%0/2
$option_specs and $commands
$option_specs $commands
Truth table33%1/3
A and B
A= $option_specsB= $commands
ABresult
0X0
100
111
287 if !$option_specs || !$commands;
288  
289 20 ▶ $default_options //= {};
Condition50%1/2
$default_options //= {}
l!l
020
MC/DC0%0/1
$default_options //= {}
$default_options
Truth table50%1/2
A
A= $default_options
Aresult
11
00
290  
291 20 my $options = { %{$default_options} };
292  
293 20 ▶ if ( $class->_use_logger && none { $_ eq 'log-level' } @{$option_specs} ) {
Subroutine __ANON__: not called CC=1 SCAR=2.2
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_use_logger and &none(sub { $_ eq "log-level"; } , @{$option_specs;})) 0 20 20
Condition33%1/3
$class->_use_logger and &none(sub { $_ eq "log-level"; } , @{$option_specs;})
!ll&&!rl&&r
2000
MC/DC0%0/2
$class->_use_logger and &none(sub { $_ eq "log-level"; } , @{$option_specs;})
$class->_use_logger &none(sub { $_ eq "log-level"; } , @{$option_specs;})
Truth table33%1/3
A and B
A= $class->_use_loggerB= &none(sub { $_ eq "log-level"; } , @{$option_specs;})
ABresult
0X0
100
111
294 0 push @{$option_specs}, 'log-level=s';
295 }
296  
297 # if we have an option alias, make sure the option alias spec is set too
298 20 ▶ if ( $alias && ref $alias && $alias->{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias and ref $alias and $alias->{"options"}) 7 13 20
Condition66%4/6
$alias and ref $alias and $alias->{"options"}
!ll&&!rl&&r
1307
$alias and ref $alias
!ll&&!rl&&r
1307
MC/DC33%1/3
$alias and ref $alias and $alias->{"options"}
$alias ref $alias $alias->{"options"}
Truth table50%2/4
A and B and C
A= $aliasB= ref $aliasC= $alias->{"options"}
ABCresult
0XX0
10X0
1100
1111
299 7 foreach my $p ( pairs %{ $alias->{options} } ) {
300 7 my ( $aka, $name ) = @{$p};
301  
302 # Does an option named $aka already exist (with or without a spec)?
303 7 ▶ my $aka_exists = any {/^\Q$aka\E(?:[^\w-].*)?$/xsm} @{$option_specs};
Subroutine __ANON__: called CC=1 SCAR=0.0
304  
305 7 ▶ if ( !$aka_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aka_exists) 2 5 7
306  
307 # Find the canonical spec for $name
308 2 my ($spec) = grep {/^\Q$name\E(?:[^\w-].*)?$/xsm} @{$option_specs};
309 2 ▶ croak sprintf 'ERROR: no such option defined: %s', $name if !$spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 2 2
310  
311 # Pull just the specifier part (e.g., '=s', ':i', '!', '+', etc.)
312 2 my ($spec_part) = $spec =~ /^\Q$name\E(?:[|].)?([^\w-].*)?$/xsm;
313  
314 2 ▶ $spec_part ||= q{};
Condition50%1/2
$spec_part ||= ""
l!l
20
MC/DC0%0/1
$spec_part ||= ""
$spec_part
Truth table50%1/2
A
A= $spec_part
Aresult
11
00
315  
316 2 push @{$option_specs}, $aka . $spec_part;
317 }
318 }
319 }
320  
321 20 ▶ if ( @ARGV && $ARGV[0] =~ /^-[[:alpha:]]/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^-[[:alpha:]]/msx) 0 20 20
Condition66%2/3
@ARGV and $ARGV[0] =~ /^-[[:alpha:]]/msx
!ll&&!rl&&r
6140
MC/DC0%0/2
@ARGV and $ARGV[0] =~ /^-[[:alpha:]]/msx
@ARGV $ARGV[0] =~ /^-[[:alpha:]]/msx
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] =~ /^-[[:alpha:]]/msx
ABresult
0X0
100
111
322 0 ▶ if ( my $handler = $INTERNAL_COMMANDS{ $ARGV[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $CLI::Simple::INTERNAL_COMMANDS{$ARGV[0]}) 0 0 0
323 0 exit $handler->( $class, $commands, $option_specs );
324 }
325 }
326  
327 20 ▶ $GETOPT_STATUS = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
328 20 ▶ local $SIG{__WARN__} = sub { $GETOPT_ERROR_MESSAGE = shift; return; };
Subroutine __ANON__: called CC=1 SCAR=0.0
329  
330 20 return GetOptions( $options, @{$option_specs} );
331 }
332 20 ->();
333  
334 20 normalize_options($options);
335  
336 20 my %cli_options;
337  
338 my @accessors
339 20 ▶ = ( @{ $extra_options || [] }, map { ( split /[^\w\-]/xsm )[0] } @{$option_specs} );
Condition50%1/2
$extra_options || []
l!l
020
MC/DC0%0/1
$extra_options || []
$extra_options
Truth table50%1/2
A
A= $extra_options
Aresult
11
00
340  
341 20 foreach (@accessors) {
342 28 ▶ die "ERROR: accessor must be a scalar\n" # somebody did @extra_options = [qw( a b c )] ???
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 0 28 28
343 if ref $_;
344  
345 28 s/\-/_/xsmg;
346  
347 # can() can't tell "a human hand-wrote this method" apart from
348 # "mk_accessors already generated this on an earlier ->new() call
349 # for this same class" -- both look identical. Only the latter is
350 # fine (constructing multiple instances of the same class within
351 # one process is completely normal); only the former is the
352 # genuine naming collision this check exists to catch. Track what
353 # this class has already generated, per class, so a repeat
354 # construction doesn't get mistaken for a hand-written conflict.
355 28 ▶ if ( !$GENERATED_ACCESSOR{$class}{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($GENERATED_ACCESSOR{$class}{$_}) 7 21 28
356 7 ▶ die "ERROR: accessor for $_ already exists!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $class->can('get_' . $_) or $class->can('set_' . $_) 0 7 7
Condition33%1/3
$class->can('get_' . $_) or $class->can('set_' . $_)
l!l&&r!l&&!r
007
MC/DC0%0/2
$class->can('get_' . $_) or $class->can('set_' . $_)
$class->can('get_' . $_) $class->can('set_' . $_)
Truth table33%1/3
A or B
A= $class->can('get_' . $_)B= $class->can('set_' . $_)
ABresult
1X1
011
000
357 if $class->can( 'get_' . $_ ) || $class->can( 'set_' . $_ );
358  
359 7 $class->mk_accessors($_);
360  
361 7 $GENERATED_ACCESSOR{$class}{$_} = $TRUE;
362 }
363  
364 28 $cli_options{$_} = $options->{$_};
365 }
366  
367 20 ▶ if ( !$error_handler && !$GETOPT_STATUS ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $error_handler || $CLI::Simple::GETOPT_STATUS) { } 2 18 20
elsif (not $CLI::Simple::GETOPT_STATUS) { } 0 18 18
Condition66%2/3
$error_handler || $CLI::Simple::GETOPT_STATUS
l!l&&r!l&&!r
0182
MC/DC50%1/2
$error_handler || $CLI::Simple::GETOPT_STATUS
$error_handler $CLI::Simple::GETOPT_STATUS
Truth table66%2/3
A || B
A= $error_handlerB= $CLI::Simple::GETOPT_STATUS
ABresult
1X1
011
000
368 2 print {*STDERR} $GETOPT_ERROR_MESSAGE;
369 2 ▶ if ($GETOPT_EXIT_ON_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLI::Simple::GETOPT_EXIT_ON_ERROR) 2 0 2
370 2 _leave($FAILURE);
371 }
372 }
373 elsif ( !$GETOPT_STATUS ) {
374 0 ▶ if ( !$error_handler->($GETOPT_ERROR_MESSAGE) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$error_handler($CLI::Simple::GETOPT_ERROR_MESSAGE)) 0 0 0
375 0 _leave($FAILURE);
376 }
377 }
378  
379 18 ▶ croak "ERROR: alias must be a hash ref with keys 'options' or 'commands'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $alias and not ref $alias 0 18 18
Condition66%2/3
$alias and not ref $alias
!ll&&!rl&&r
1170
MC/DC0%0/2
$alias and not ref $alias
$alias not ref $alias
Truth table66%2/3
A and not(B)
A= $aliasB= not ref $alias
ABresult
0X0
100
111
380 if $alias && !ref $alias;
381  
382 18 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 7 11 18
383 7 ▶ if ( $alias->{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias->{"options"}) 7 0 7
384 7 foreach my $p ( pairs %{ $alias->{options} } ) {
385 7 my ( $aka, $name ) = @{$p};
386 7 $aka =~ s/[-]/_/gxsm;
387 7 $name =~ s/[-]/_/gxsm;
388  
389 7 ▶ if ( defined $cli_options{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cli_options{$name}) { } 6 1 7
elsif (defined $cli_options{$aka}) { } 1 0 1
390 6 $cli_options{$aka} = $cli_options{$name};
391 }
392 elsif ( defined $cli_options{$aka} ) {
393 1 $cli_options{$name} = $cli_options{$aka};
394 }
395 }
396 }
397  
398 # command aliases are a convenience so someone doesn't have to add
399 # to $commands manually
400 7 ▶ if ( $alias->{commands} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias->{"commands"}) 1 6 7
401 1 foreach my $p ( pairs %{ $alias->{commands} } ) {
402 croak sprintf "ERROR: no command: %s\n"
403 1 ▶ if !$commands->{ $p->[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless $$commands{$p->[1]} 0 1 1
404  
405 1 $commands->{ $p->[0] } = $commands->{ $p->[1] };
406 }
407 }
408 }
409  
410 18 my $self = $class->SUPER::new( \%cli_options );
411  
412 18 ▶ if ( !$self->can('get_help_sections') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->can('get_help_sections')) { } 7 11 18
413 7 $self->mk_accessors('help_sections');
414 7 $GENERATED_ACCESSOR{$class}{help_sections} = $TRUE;
415 }
416 else {
417 11 ▶ $help_sections //= $self->get_help_sections;
Condition33%1/3
$help_sections //= $self->get_help_sections
l!l&&r!l&&!r
0011
MC/DC0%0/2
$help_sections //= $self->get_help_sections
$help_sections $self->get_help_sections
Truth table33%1/3
A //= B
A= $help_sectionsB= $self->get_help_sections
ABresult
1X1
011
000
418 }
419  
420 18 ▶ $help_sections //= [@DEFAULT_HELP_SECTIONS];
Condition50%1/2
$help_sections //= [@CLI::Simple::DEFAULT_HELP_SECTIONS]
l!l
018
MC/DC0%0/1
$help_sections //= [@CLI::Simple::DEFAULT_HELP_SECTIONS]
$help_sections
Truth table50%1/2
A
A= $help_sections
Aresult
11
00
421  
422 18 $self->set_help_sections($help_sections);
423  
424 # AUTO_DEFAULT uses the only command as the default
425 18 ▶ if ( $AUTO_DEFAULT && scalar keys %{$commands} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLI::Simple::AUTO_DEFAULT and scalar keys %{$commands;} == 1) 2 16 18
Condition100%3/3
$CLI::Simple::AUTO_DEFAULT and scalar keys %{$commands;} == 1
!ll&&!rl&&r
1512
MC/DC100%2/2
$CLI::Simple::AUTO_DEFAULT and scalar keys %{$commands;} == 1
$CLI::Simple::AUTO_DEFAULT scalar keys %{$commands;} == 1
Truth table100%3/3
A and B
A= $CLI::Simple::AUTO_DEFAULTB= scalar keys %{$commands;} == 1
ABresult
0X0
100
111
426 2 my ($command) = keys %{$commands};
427  
428 2 ▶ if ( @ARGV && $ARGV[0] ne $command ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] ne $command) { } 1 1 2
elsif (not @ARGV) { } 1 0 1
Condition66%2/3
@ARGV and $ARGV[0] ne $command
!ll&&!rl&&r
101
MC/DC50%1/2
@ARGV and $ARGV[0] ne $command
@ARGV $ARGV[0] ne $command
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] ne $command
ABresult
0X0
100
111
429 1 unshift @ARGV, $command;
430 }
431 elsif ( !@ARGV ) {
432 1 unshift @ARGV, $command;
433 }
434 }
435  
436 18 ▶ my $command = shift @ARGV // $EMPTY;
Condition66%2/3
shift @ARGV // $CLI::Simple::EMPTY
l!l&&r!l&&!r
1305
MC/DC50%1/2
shift @ARGV // $CLI::Simple::EMPTY
shift @ARGV $CLI::Simple::EMPTY
Truth table66%2/3
A // B
A= shift @ARGVB= $CLI::Simple::EMPTY
ABresult
1X1
011
000
437  
438 18 ▶ if ( !$command ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 5 13 18
439 5 ▶ if ( $commands->{default} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($commands->{'default'}) { } 0 5 5
elsif ($CLI::Simple::AUTO_HELP) { } 1 4 5
440 0 ▶ $command = ref $commands->{default} ? 'default' : $commands->{default};
Branch: 1 branch
BranchTrueFalseTotal
ref $commands->{'default'} ? : 0 0 0
441 }
442 elsif ($AUTO_HELP) {
443 1 $command = 'help';
444 }
445 }
446  
447 18 $self->set__validate_command($validate_command);
448  
449 18 $self->set__command($command);
450  
451 18 $self->set__command_args( [@ARGV] );
452  
453 18 $self->set__commands($commands);
454  
455 18 $self->set__program("$RealBin/$RealScript");
456  
457 18 ▶ if ( $command eq 'help' || ( $self->can('get_help') && $self->get_help ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "help" or $self->can("get_help") and $self->get_help) 1 17 18
Condition50%3/6
$command eq "help" or $self->can("get_help") and $self->get_help
l!l&&r!l&&!r
1017
$self->can("get_help") and $self->get_help
!ll&&!rl&&r
1700
MC/DC33%1/3
$command eq "help" or $self->can("get_help") and $self->get_help
$command eq "help" $self->can("get_help") $self->get_help
Truth table50%2/4
A or B and C
A= $command eq "help"B= $self->can("get_help")C= $self->get_help
ABCresult
1XX1
0111
00X0
0100
458 # custom help function?
459  
460 1 my $help = $commands->{help};
461  
462 1 ▶ if ( !$help ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($help) 1 0 1
463 1 $self->usage;
464 }
465  
466 0 ▶ if ( ref $help && reftype($help) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $help and builtin::reftype($help) eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $help and builtin::reftype($help) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $help and builtin::reftype($help) eq "ARRAY"
ref $help builtin::reftype($help) eq "ARRAY"
Truth table0%0/3
A and B
A= ref $helpB= builtin::reftype($help) eq "ARRAY"
ABresult
0X0
100
111
467 0 $help->[0]->( $self, $TRUE );
468 }
469 else {
470 0 $help->( $self, $TRUE );
471 }
472 }
473  
474 17 ▶ $self->set__abbreviations( $abbreviations // $FALSE );
Condition66%2/3
$abbreviations // $CLI::Simple::FALSE
l!l&&r!l&&!r
2015
MC/DC50%1/2
$abbreviations // $CLI::Simple::FALSE
$abbreviations $CLI::Simple::FALSE
Truth table66%2/3
A // B
A= $abbreviationsB= $CLI::Simple::FALSE
ABresult
1X1
011
000
475  
476 17 $self->validate_command;
477  
478 15 $self->init_logger;
479  
480 15 ▶ $self->can('init') && $self->init();
Branch: 1 branch
BranchTrueFalseTotal
$self->can("init") and $self->init 0 15 15
481  
482 15 return $self;
483 }
484  
485 ########################################################################
486 4 ▶sub _pod_has_section {
Subroutine _pod_has_section: called CC=4 SCAR=13.9
487 ########################################################################
488 4 my ( $filename, $section ) = @_;
489  
490 4 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or return $CLI::Simple::FALSE 4 0 4
491 or return $FALSE;
492  
493 4 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 163 1 164
494 163 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A=head1\s+\Q$section\E\s*\z/msx 3 160 163
495 if $line =~ /\A=head1\s+\Q$section\E\s*\z/xsm;
496 }
497  
498 1 return $FALSE;
499 }
500  
501 ########################################################################
502 5 ▶sub _get_help_sections {
Subroutine _get_help_sections: called CC=5 SCAR=16.1
503 ########################################################################
504 5 my ( $self, $input ) = @_;
505  
506 5 my $sections = $self->get_help_sections;
507  
508 return $sections
509 5 ▶ if @{$sections} != @DEFAULT_HELP_SECTIONS;
Branch: 1 branch
BranchTrueFalseTotal
if @{$sections;} != @CLI::Simple::DEFAULT_HELP_SECTIONS 1 4 5
510  
511 4 for my $idx ( 0 .. $#DEFAULT_HELP_SECTIONS ) {
512 20 ▶ return $sections
Branch: 1 branch
BranchTrueFalseTotal
if $sections->[$idx] ne $CLI::Simple::DEFAULT_HELP_SECTIONS[$idx] 0 20 20
513 if $sections->[$idx] ne $DEFAULT_HELP_SECTIONS[$idx];
514 }
515  
516 4 ▶ return [ _pod_has_section( $input, 'USAGE' )
Branch: 1 branch
BranchTrueFalseTotal
_pod_has_section($input, 'USAGE') ? : 3 1 4
517 ? 'USAGE'
518 : 'SYNOPSIS',
519 'DESCRIPTION/Commands', 'DESCRIPTION/Options', 'OPTIONS', ];
520 }
521  
522 ########################################################################
523 7 ▶sub _leave {
Subroutine _leave: called CC=2 SCAR=7.1
524 ########################################################################
525 7 my (@args) = @_;
526  
527 7 ▶ my ($exit_code) = ref $args[0] ? $args[1] : $args[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 7 7
528  
529 7 exit $exit_code;
530 }
531  
532 ########################################################################
533 0 ▶sub _set_color_config {
Subroutine _set_color_config: not called CC=7 SCAR=40.3
534 ########################################################################
535 0 my ( $self, %args ) = @_;
536  
537 return sprintf $LOG4PERL_CONF,
538 $args{debug_color} // $LOG4PERL_COLOR_DEBUG,
539 $args{info_color} // $LOG4PERL_COLOR_INFO,
540 $args{warn_color} // $LOG4PERL_COLOR_WARN,
541 $args{error_color} // $LOG4PERL_COLOR_ERROR,
542 $args{fatal_color} // $LOG4PERL_COLOR_FATAL,
543 0 ▶ $args{trace_color} // $LOG4PERL_COLOR_TRACE;
Condition0%0/18
$args{"debug_color"} // $CLI::Simple::LOG4PERL_COLOR_DEBUG
l!l&&r!l&&!r
000
$args{"info_color"} // $CLI::Simple::LOG4PERL_COLOR_INFO
l!l&&r!l&&!r
000
$args{"warn_color"} // $CLI::Simple::LOG4PERL_COLOR_WARN
l!l&&r!l&&!r
000
$args{"error_color"} // $CLI::Simple::LOG4PERL_COLOR_ERROR
l!l&&r!l&&!r
000
$args{"fatal_color"} // $CLI::Simple::LOG4PERL_COLOR_FATAL
l!l&&r!l&&!r
000
$args{"trace_color"} // $CLI::Simple::LOG4PERL_COLOR_TRACE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"debug_color"} // $CLI::Simple::LOG4PERL_COLOR_DEBUG
$args{"debug_color"} $CLI::Simple::LOG4PERL_COLOR_DEBUG
MC/DC0%0/2
$args{"info_color"} // $CLI::Simple::LOG4PERL_COLOR_INFO
$args{"info_color"} $CLI::Simple::LOG4PERL_COLOR_INFO
MC/DC0%0/2
$args{"warn_color"} // $CLI::Simple::LOG4PERL_COLOR_WARN
$args{"warn_color"} $CLI::Simple::LOG4PERL_COLOR_WARN
MC/DC0%0/2
$args{"error_color"} // $CLI::Simple::LOG4PERL_COLOR_ERROR
$args{"error_color"} $CLI::Simple::LOG4PERL_COLOR_ERROR
MC/DC0%0/2
$args{"fatal_color"} // $CLI::Simple::LOG4PERL_COLOR_FATAL
$args{"fatal_color"} $CLI::Simple::LOG4PERL_COLOR_FATAL
MC/DC0%0/2
$args{"trace_color"} // $CLI::Simple::LOG4PERL_COLOR_TRACE
$args{"trace_color"} $CLI::Simple::LOG4PERL_COLOR_TRACE
Truth table0%0/3
A // B
A= $args{"debug_color"}B= $CLI::Simple::LOG4PERL_COLOR_DEBUG
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args{"info_color"}B= $CLI::Simple::LOG4PERL_COLOR_INFO
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args{"warn_color"}B= $CLI::Simple::LOG4PERL_COLOR_WARN
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args{"error_color"}B= $CLI::Simple::LOG4PERL_COLOR_ERROR
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args{"fatal_color"}B= $CLI::Simple::LOG4PERL_COLOR_FATAL
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $args{"trace_color"}B= $CLI::Simple::LOG4PERL_COLOR_TRACE
ABresult
1X1
011
000
544 }
545  
546 ########################################################################
547 15 ▶sub init_logger {
Subroutine init_logger: called CC=23 SCAR=58.5
Pod undocumented
548 ########################################################################
549 15 my ($self) = @_;
550  
551 15 ▶ if ( $self->_use_logger && $self->can('get_log4perl_conf') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_use_logger and $self->can("get_log4perl_conf")) 0 15 15
Condition33%1/3
$self->_use_logger and $self->can("get_log4perl_conf")
!ll&&!rl&&r
1500
MC/DC0%0/2
$self->_use_logger and $self->can("get_log4perl_conf")
$self->_use_logger $self->can("get_log4perl_conf")
Truth table33%1/3
A and B
A= $self->_use_loggerB= $self->can("get_log4perl_conf")
ABresult
0X0
100
111
552  
553 0 my $config = $self->get_log4perl_conf;
554  
555 0 ▶ my $color = $self->can('get_color') && defined $self->get_color ? $self->get_color : $config && $config eq $LOG4PERL_CONF;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('get_color') && defined $self->get_color ? : 0 0 0
Condition0%0/6
$self->can("get_color") and defined $self->get_color
!ll&&!rl&&r
000
$config and $config eq $CLI::Simple::LOG4PERL_CONF
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("get_color") and defined $self->get_color
$self->can("get_color") defined $self->get_color
MC/DC0%0/2
$config and $config eq $CLI::Simple::LOG4PERL_CONF
$config $config eq $CLI::Simple::LOG4PERL_CONF
Truth table0%0/3
A and B
A= $self->can("get_color")B= defined $self->get_color
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $configB= $config eq $CLI::Simple::LOG4PERL_CONF
ABresult
0X0
100
111
556  
557 0 ▶ $color = $color && eval { require Term::ANSIColor; 1; };
Condition0%0/3
$color && eval { do { require Term::ANSIColor; 1 } }
!ll&&!rl&&r
000
MC/DC0%0/2
$color && eval { do { require Term::ANSIColor; 1 } }
$color eval { do { require Term::ANSIColor; 1 } }
Truth table0%0/3
A && B
A= $colorB= eval { do { require Term::ANSIColor; 1 } }
ABresult
0X0
100
111
558  
559 # color on, but not done through use_log4perl?
560 0 ▶ if ( $color && !$config ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($color and not $config) { } 0 0 0
elsif (not $color and $config and $config =~ /screencoloredlevels/imsx) { } 0 0 0
Condition0%0/3
$color and not $config
!ll&&!rl&&r
000
MC/DC0%0/2
$color and not $config
$color not $config
Truth table0%0/3
A and not(B)
A= $colorB= not $config
ABresult
0X0
100
111
561 0 $config = $self->_set_color_config;
562 }
563 ▶ elsif ( !$color && $config && $config =~ /screencoloredlevels/xsmi ) {
Condition0%0/6
not $color and $config and $config =~ /screencoloredlevels/imsx
!ll&&!rl&&r
000
not $color and $config
!ll&&!rl&&r
000
MC/DC0%0/3
not $color and $config and $config =~ /screencoloredlevels/imsx
not $color $config $config =~ /screencoloredlevels/imsx
Truth table0%0/4
not(A) and B and C
A= not $colorB= $configC= $config =~ /screencoloredlevels/imsx
ABCresult
0XX0
10X0
1100
1111
564 0 $config = q{};
565 }
566  
567 0 $self->set_log4perl_conf($config);
568  
569 0 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) { } 0 0 0
570 0 ▶ Log::Log4perl->init( ref $config ? $config : \$config );
Branch: 1 branch
BranchTrueFalseTotal
ref $config ? : 0 0 0
571 }
572 else {
573 0 Log::Log4perl->import(':easy');
574 0 Log::Log4perl->easy_init( $LOG_LEVELS{error} );
575 }
576  
577 0 my $logger = Log::Log4perl->get_logger(q{});
578  
579 0 $self->set_logger($logger);
580  
581 0 ▶ my $level = $self->get_log_level // $self->get_log4perl_level;
Condition0%0/3
$self->get_log_level // $self->get_log4perl_level
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_log_level // $self->get_log4perl_level
$self->get_log_level $self->get_log4perl_level
Truth table0%0/3
A // B
A= $self->get_log_levelB= $self->get_log4perl_level
ABresult
1X1
011
000
582  
583 0 $self->set_log_level($level);
584  
585 0 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 0 0 0
586 0 $logger->level( $LOG_LEVELS{$level} );
587 }
588 }
589  
590 15 my $commands = $self->commands;
591 15 my $command = $self->command;
592  
593 return $self
594 15 ▶ if !$command || !$commands->{$command} || !ref( $commands->{command} ) || reftype( $commands->{$command} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if not $command && $commands->{$command} && ref $commands->{"command"} or builtin::reftype($commands->{$command}) ne "ARRAY" 15 0 15
Condition55%5/9
not $command && $commands->{$command} && ref $commands->{"command"} or builtin::reftype($commands->{$command}) ne "ARRAY"
l!l&&r!l&&!r
1500
$command && $commands->{$command} && ref $commands->{'command'}
!ll&&!rl&&r
4110
$command and $commands->{$command}
!ll&&!rl&&r
4011
MC/DC0%0/4
not $command && $commands->{$command} && ref $commands->{"command"} or builtin::reftype($commands->{$command}) ne "ARRAY"
$command $commands->{$command} ref $commands->{'command'} builtin::reftype($commands->{$command}) ne "ARRAY"
Truth table40%2/5
not(A and B && C) or D
A= $commandB= $commands->{$command}C= ref $commands->{'command'}D= builtin::reftype($commands->{$command}) ne "ARRAY"
ABCDresult
0XXX1
10XX1
110X1
11111
11100
595  
596 0 my ( $sub, $log_level ) = @{ $commands->{$command} };
597  
598 0 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_logger 0 0 0
599 if !$self->get_logger;
600  
601 0 ▶ $log_level = $self->get_log_level // $log_level;
Condition0%0/3
$self->get_log_level // $log_level
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_log_level // $log_level
$self->get_log_level $log_level
Truth table0%0/3
A // B
A= $self->get_log_levelB= $log_level
ABresult
1X1
011
000
602  
603 0 ▶ $self->get_logger->level( $LOG_LEVELS{$log_level} // $LOG_LEVELS{info} );
Condition0%0/3
$CLI::Simple::LOG_LEVELS{$log_level} // $CLI::Simple::LOG_LEVELS{"info"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$CLI::Simple::LOG_LEVELS{$log_level} // $CLI::Simple::LOG_LEVELS{"info"}
$CLI::Simple::LOG_LEVELS{$log_level} $CLI::Simple::LOG_LEVELS{"info"}
Truth table0%0/3
A // B
A= $CLI::Simple::LOG_LEVELS{$log_level}B= $CLI::Simple::LOG_LEVELS{"info"}
ABresult
1X1
011
000
604  
605 0 return $self;
606 }
607  
608 ########################################################################
609 0 ▶sub get_kv_args {
Subroutine get_kv_args: not called CC=2 SCAR=17.9
Pod undocumented
610 ########################################################################
611 0 my ($self) = @_;
612  
613 0 my @arg_list = @{ $self->get__command_args };
614  
615 0 my %args;
616  
617 0 foreach (@arg_list) {
618 0 my ( $k, $v ) = split /=/xsm;
619 0 $args{$k} = $v;
620 }
621  
622 0 return %args;
623 }
624  
625 ########################################################################
626 0 ▶sub set_args {
Subroutine set_args: not called CC=1 SCAR=6.9
Pod documented
627 ########################################################################
628 0 my ( $self, $args ) = @_;
629  
630 0 $self->set__command_args($args);
631  
632 0 return $args;
633 }
634  
635 ########################################################################
636 4 ▶sub get_args {
Subroutine get_args: called CC=5 SCAR=16.1
Pod documented
637 ########################################################################
638 4 my ( $self, @vars ) = @_;
639  
640 4 my $command_args = $self->get__command_args;
641  
642 4 ▶ return wantarray ? @{$command_args} : @{$command_args}
Branch: 2 branches
BranchTrueFalseTotal
unless @vars 2 2 4
wantarray ? : 2 0 2
643 if !@vars;
644  
645 2 ▶ @vars = map { $_ ? $_ : '<undef>' } @vars;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 5 1 6
646  
647 2 my %args;
648 2 @args{@vars} = @{$command_args}[ 0 .. $#vars ];
649  
650 2 delete $args{'<undef>'};
651  
652 2 ▶ return wantarray ? %args : \%args;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
653 }
654  
655 ########################################################################
656 0 ▶sub default_command {
Subroutine default_command: not called CC=1 SCAR=6.9
Pod undocumented
657 ########################################################################
658 0 goto &usage;
659 }
660  
661 ########################################################################
662 5 ▶sub usage {
Subroutine usage: called CC=5 SCAR=18.0
Pod documented
663 ########################################################################
664 5 my ($self) = @_;
665  
666 5 require Pod::Usage;
667  
668 5 ▶ my $wrapper = $ENV{MODULINO_WRAPPER} // q{};
Condition50%1/2
$ENV{"MODULINO_WRAPPER"} // ""
l!l
05
MC/DC0%0/1
$ENV{"MODULINO_WRAPPER"} // ""
$ENV{"MODULINO_WRAPPER"}
Truth table50%1/2
A
A= $ENV{"MODULINO_WRAPPER"}
Aresult
11
00
669  
670 5 ▶ my $input = $wrapper eq 'cli-simple' ? $INC{'CLI/Simple/Shell.pm'} : $self->get__program;
Branch: 1 branch
BranchTrueFalseTotal
$wrapper eq 'cli-simple' ? : 0 5 5
671  
672 5 ▶ if ( $PAGER && IO::Interactive::is_interactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLI::Simple::PAGER and IO::Interactive::is_interactive()) 0 5 5
Condition66%2/3
$CLI::Simple::PAGER and IO::Interactive::is_interactive()
!ll&&!rl&&r
410
MC/DC0%0/2
$CLI::Simple::PAGER and IO::Interactive::is_interactive()
$CLI::Simple::PAGER IO::Interactive::is_interactive()
Truth table66%2/3
A and B
A= $CLI::Simple::PAGERB= IO::Interactive::is_interactive()
ABresult
0X0
100
111
673 0 eval {
674 0 require IO::Pager;
675 0 IO::Pager->new(*STDOUT);
676 };
677 }
678  
679 5 my $sections = $self->_get_help_sections($input);
680  
681 5 Pod::Usage::pod2usage(
682 -noperldoc => 1,
683 -exitval => 'NOEXIT',
684 -input => $input,
685 -verbose => 99,
686 -sections => $sections,
687 );
688  
689 5 return _leave($FAILURE);
690 }
691  
692 ########################################################################
693 0 ▶sub example {
Subroutine example: not called CC=1 SCAR=6.9
Pod undocumented
694 ########################################################################
695 0 require File::ShareDir;
696  
697 0 print {*STDOUT} slurp File::ShareDir::dist_file( 'CLI-Simple', 'MyScript.pm' );
698  
699 0 return 0;
700 }
701  
702 ########################################################################
703 38 ▶sub command {
Subroutine command: called CC=2 SCAR=7.6
Pod documented
704 ########################################################################
705 38 my ( $self, $command ) = @_;
706  
707 38 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) 0 38 38
708 0 $self->set__command($command);
709 }
710  
711 38 return $self->get__command;
712 }
713  
714 ########################################################################
715 0 ▶sub command_args {
Subroutine command_args: not called CC=3 SCAR=24.8
Pod documented
716 ########################################################################
717 0 my ( $self, @args ) = @_;
718  
719 0 ▶ return $self->get__command_args
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
720 if !@args;
721  
722 0 ▶ if ( ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0]) 0 0 0
723 0 $self->set__command_args( $args[0] );
724 0 return;
725 }
726  
727 0 $self->set__command_args( [@args] );
728  
729 0 return;
730 }
731  
732 ########################################################################
733 34 ▶sub commands {
Subroutine commands: called CC=4 SCAR=18.7
Pod documented
734 ########################################################################
735 34 my ( $self, $command, $handler ) = @_;
736  
737 34 my $commands = $self->get__commands;
738  
739 34 ▶ if ( $command && $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command and $handler) 0 34 34
Condition33%1/3
$command and $handler
!ll&&!rl&&r
3400
MC/DC0%0/2
$command and $handler
$command $handler
Truth table33%1/3
A and B
A= $commandB= $handler
ABresult
0X0
100
111
740  
741 0 ▶ croak "ERROR: usage: commands([command, subref])\n"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($handler) ne "CODE" 0 0 0
742 if reftype($handler) ne 'CODE';
743  
744 0 $commands->{$command} = $handler;
745 }
746  
747 34 return $commands;
748 }
749  
750 ########################################################################
751 6 ▶sub program {
Subroutine program: called CC=1 SCAR=0.0
Pod undocumented
752 ########################################################################
753 6 my ($self) = @_;
754  
755 6 return $self->get__program;
756 }
757  
758 ########################################################################
759 17 ▶sub validate_command {
Subroutine validate_command: called CC=7 SCAR=19.5
Pod documented
760 ########################################################################
761 17 my ($self) = @_;
762  
763 17 my $command = $self->command;
764  
765 return
766 17 ▶ if !$command || !$self->get__validate_command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command and $self->get__validate_command 4 13 17
Condition66%2/3
$command and $self->get__validate_command
!ll&&!rl&&r
4013
MC/DC50%1/2
$command and $self->get__validate_command
$command $self->get__validate_command
Truth table66%2/3
A and B
A= $commandB= $self->get__validate_command
ABresult
0X0
100
111
767  
768 13 my $commands = $self->commands;
769  
770 return $command
771 13 ▶ if defined $commands->{$command};
Branch: 1 branch
BranchTrueFalseTotal
if defined $commands->{$command} 10 3 13
772  
773 3 ▶ croak sprintf "Unknown command: %s\n", $command
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get__abbreviations 1 2 3
774 if !$self->get__abbreviations;
775  
776 2 my $abbreviation = $command;
777  
778 2 my @matches = grep {/^$abbreviation/xsm} keys %{$commands};
779  
780 2 ▶ croak sprintf "Unknown command: %s\n", $command
Branch: 1 branch
BranchTrueFalseTotal
unless @matches 0 2 2
781 if !@matches;
782  
783 2 ▶ if ( @matches == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches == 1) 1 1 2
784 1 $command = $matches[0]; # Unique match - accept
785 1 $self->set__command($command);
786 1 return $command;
787 }
788  
789 1 croak sprintf "Ambiguous command '$abbreviation'; could match: %s\n", join q{,}, @matches;
790 }
791  
792 ########################################################################
793 0 ▶sub _cmd_generate_completion {
Subroutine _cmd_generate_completion: not called CC=4 SCAR=27.5
794 ########################################################################
795 0 my ( $class, $commands, $option_specs ) = @_;
796  
797 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
798 0 my $stash = \%{ $class . '::' };
799  
800 0 my $cmd_list = join q{ }, sort grep { !/^-/xsm } keys %{$commands};
801  
802 0 ▶ my $program = $ENV{MODULINO_WRAPPER} // do {
Condition0%0/3
$ENV{"MODULINO_WRAPPER"} // do { (my $name = $class) =~ s/::/-/gmsx; lc $name }
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"MODULINO_WRAPPER"} // do { (my $name = $class) =~ s/::/-/gmsx; lc $name }
$ENV{"MODULINO_WRAPPER"} do { (my $name = $class) =~ s/::/-/gmsx; lc $name }
Truth table0%0/3
A // B
A= $ENV{"MODULINO_WRAPPER"}B= do { (my $name = $class) =~ s/::/-/gmsx; lc $name }
ABresult
1X1
011
000
803 0 ( my $name = $class ) =~ s/::/-/gxsm;
804 0 lc $name;
805 };
806  
807 0 my @flags;
808 my @value_opts;
809  
810 0 for my $spec ( @{$option_specs} ) {
811 0 my ($name) = $spec =~ /\A([\w-]+)/xsm;
812 0 $name =~ s/_/-/gxsm;
813  
814 0 ▶ if ( $spec =~ /[=:]/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /[=:]/msx) { } 0 0 0
815 0 push @value_opts, "--$name";
816 }
817 else {
818 0 push @flags, "--$name";
819 }
820 }
821  
822 0 my $flags = join q{ }, sort @flags;
823 0 my $value_opts = join q{ }, sort @value_opts;
824  
825 0 printf {*STDOUT} <<'END_COMPLETION', $program, $cmd_list, $value_opts, $flags, $program, $program;
826 _%s() {
827 local cur prev words cword
828 _init_completion || return
829  
830 local commands="%s"
831 local value_opts="%s"
832 local flags="%s"
833  
834 if [[ $cword -eq 1 ]]; then
835 if [[ "$cur" == --* ]]; then
836 COMPREPLY=( $(compgen -W "$flags $value_opts" -- "$cur") )
837 else
838 COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
839 fi
840 return
841 fi
842  
843 case $prev in
844 $value_opts)
845 COMPREPLY=( $(compgen -f -- "$cur") )
846 return ;;
847 esac
848  
849 COMPREPLY=( $(compgen -W "$flags $value_opts" -- "$cur") )
850 }
851  
852 complete -F _%s %s
853 END_COMPLETION
854  
855 0 return $SUCCESS;
856 }
857  
858 ########################################################################
859 6 ▶sub run {
Subroutine run: called CC=8 SCAR=28.7
Pod documented
860 ########################################################################
861 6 my ($self) = @_;
862  
863 6 my $command = $self->command;
864  
865 ######################################################################
866 # a blank command means that we did not have a default && AUTO_HELP
867 # is off scripter has deliberately decided to allow running of
868 # init() phase w/o a run phase
869 ######################################################################
870  
871 6 ▶ return $SUCCESS
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 6 6
872 if !$command;
873  
874 6 my $commands = $self->commands;
875  
876 6 my $program = $self->program;
877  
878 6 my $handler = $commands->{$command};
879  
880 6 ▶ if ( $handler && !ref $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler and not ref $handler) 0 6 6
Condition33%1/3
$handler and not ref $handler
!ll&&!rl&&r
060
MC/DC0%0/2
$handler and not ref $handler
$handler not ref $handler
Truth table33%1/3
A and not(B)
A= $handlerB= not ref $handler
ABresult
0X0
100
111
881 0 $handler = $commands->{$handler}; # default?
882 }
883  
884 6 ▶ die "ERROR: no such command '$command' has been registered.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 6 6
885 if !$handler;
886  
887 6 ▶ return $handler->($self)
Branch: 1 branch
BranchTrueFalseTotal
if ref $handler ne "ARRAY" 6 0 6
888 if ref $handler ne 'ARRAY';
889  
890 0 my ( $sub, $log_level ) = @{$handler};
891  
892 0 ▶ croak "ERROR: invalid specification for $command\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $sub or builtin::reftype($sub) ne "CODE" 0 0 0
Condition0%0/3
not ref $sub or builtin::reftype($sub) ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $sub or builtin::reftype($sub) ne "CODE"
not ref $sub builtin::reftype($sub) ne "CODE"
Truth table0%0/3
not(A) or B
A= not ref $subB= builtin::reftype($sub) ne "CODE"
ABresult
1X1
011
000
893 if !ref $sub || reftype($sub) ne 'CODE';
894  
895 0 return $sub->($self);
896 }
897  
898 7 1;
899  
900 ## no critic (RequirePodSections)
901  
902 __END__
↑ Summary Simple/Scaffold.pm »