lib/CLI/Driver/Action.pm

filter: Statementstmt 65.6% 128 / 195 Branchbran 41.6% 30 / 72 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 59.2% 180 / 304 CC 49 SCAR 48.8
CC
49
Cov
58%
usage
402.6
parse
32.7
do
5.1
CRAP 131.6
↑ Summary lib/CLI/Driver/CommonRole.pm »
1 package CLI::Driver::Action;
2  
3 17 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use CLI::Driver::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use CLI::Driver::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use CLI::Driver::Help;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 with 'CLI::Driver::CommonRole';
15  
16 ###############################
17 ###### PUBLIC ATTRIBUTES ######
18 ###############################
19  
20 17 has name => ( is => 'rw', isa => 'Str' );
21 17 has desc => ( is => 'rw', isa => 'Str' );
22 17 has is_deprecated => ( is => 'rw', isa => 'Bool' );
23 17 has class => ( is => 'rw', isa => 'CLI::Driver::Class' );
24 17 has 'method' => ( is => 'rw', isa => 'CLI::Driver::Method' );
25 17 has 'help' => ( is => 'rw', isa => 'CLI::Driver::Help' );
26 17 has 'use_argv_map' => ( is => 'rw', isa => 'Bool' );
27  
28 ############################
29 ###### PUBLIC METHODS ######
30 ############################
31  
32 17 ▶method parse (HashRef :$href!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine parse: called CC=21 SCAR=34.9
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 240 0 240
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 240 240
if ($slice_length % 2 != 0) 0 240 240
if ($i <= $#_) 240 240 480
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 240 240
exists $_{'href'} ? : 240 0 240
if (exists $_{"href"}) 240 0 240
Ref::Util::XS::is_plain_hashref($href) or _failed_check(22, "HashRef", $href, "exception_class", "Error::TypeTiny::Assertion") 0 240 240
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
24000
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
33  
34 240 ▶ if ( $href->{is_deprecated} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href->{"is_deprecated"}) 0 240 240
35  
36 0 my $string = $href->{is_deprecated};
37 0 my $bool = 0;
38  
39 0 ▶ if ( $string =~ /^true$/i or $string == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^true$/iu or $string == 1) 0 0 0
Condition0%0/3
$string =~ /^true$/iu or $string == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$string =~ /^true$/iu or $string == 1
$string =~ /^true$/iu $string == 1
Truth table0%0/3
A or B
A= $string =~ /^true$/iuB= $string == 1
ABresult
1X1
011
000
40 0 $bool = 1;
41 }
42  
43 0 $self->is_deprecated($bool);
44 }
45  
46 240 ▶ if ( $href->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href->{'class'}) { } 240 0 240
47  
48 240 ▶ my $class = CLI::Driver::Class->new(use_argv_map => $self->use_argv_map ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->use_argv_map ? : 15 225 240
49 240 my $success = $class->parse( href => $href->{class} );
50 240 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 240 240
51 0 return 0;
52 }
53  
54 240 $self->class($class);
55 }
56 else {
57 0 return 0;
58 }
59  
60 240 ▶ if ( $href->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href->{'method'}) { } 240 0 240
61  
62 240 ▶ my $method = CLI::Driver::Method->new(use_argv_map => $self->use_argv_map ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->use_argv_map ? : 15 225 240
63 240 my $success = $method->parse( href => $href->{method} );
64 240 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 240 240
65 0 return 0;
66 }
67  
68 240 $self->method($method);
69 }
70 else {
71 0 return 0;
72 }
73  
74 240 ▶ if ( $href->{desc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href->{"desc"}) 240 0 240
75 240 $self->desc( $href->{desc} );
76 }
77  
78 240 my $help = CLI::Driver::Help->new;
79 240 $help->parse( href => $href->{help} );
80 240 $self->help($help);
81  
82 240 return 1;
83 }
84  
85 17 ▶method usage {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine usage: not called CC=21 SCAR=60.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
86  
87 0 printf "\nusage: %s %s [opts] [-?]\n", basename($0), $self->name;
88 0 ▶ printf "description: %s\n", $self->desc if $self->desc;
Branch: 1 branch
BranchTrueFalseTotal
if $self->desc 0 0 0
89 0 print "\n";
90  
91 0 my $help = $self->help;
92  
93 0 my @opts;
94 0 push @opts, @{ $self->class->attr };
95 0 push @opts, @{ $self->method->args };
96  
97 ##########################################################################
98  
99 0 my %opts;
100 0 foreach my $opt (@opts) {
101  
102 0 ▶ if ( $opt->required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->required) 0 0 0
103 0 $opts{ $opt->cli_arg } = $opt;
104  
105 # and $opt->is_hard) {
106 # $opts{ $opt->cli_arg } = $opt->method_arg;
107 # }
108 }
109 }
110  
111 # print required
112 0 foreach my $arg ( sort { uc($a) cmp uc($b) } keys %opts ) {
113 0 my $opt = $opts{$arg};
114 0 my $val = $opt->method_arg;
115 0 printf "\t-%s <%s>", $arg, $val;
116 0 ▶ print ' (soft)' if $opt->is_soft;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->is_soft 0 0 0
117 0 ▶ print ' (multi value)' if $opt->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->is_array 0 0 0
118 0 print "\n";
119 0 ▶ printf "\t\t%s\n", $help->get_help($arg) if $help->get_help($arg);
Branch: 1 branch
BranchTrueFalseTotal
if $help->get_help($arg) 0 0 0
120 }
121  
122 ##########################################################################
123  
124 0 %opts = ();
125 0 foreach my $opt (@opts) {
126  
127 0 ▶ if ( $opt->is_optional and !$opt->is_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->is_optional and not $opt->is_flag) 0 0 0
Condition0%0/3
$opt->is_optional and not $opt->is_flag
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->is_optional and not $opt->is_flag
$opt->is_optional not $opt->is_flag
Truth table0%0/3
A and not(B)
A= $opt->is_optionalB= not $opt->is_flag
ABresult
0X0
100
111
128 0 $opts{ $opt->cli_arg } = $opt;
129  
130 # and $opt->is_hard) {
131 # $opts{ $opt->cli_arg } = $opt->method_arg;
132 # }
133 }
134 }
135  
136 # print "\n" if keys %opts;
137  
138 # print optional
139 0 foreach my $arg ( sort { uc($a) cmp uc($b) } keys %opts ) {
140 0 my $opt = $opts{$arg};
141 0 my $val = $opt->method_arg;
142 0 printf "\t[ -%s <%s> ]", $arg, $val;
143 0 ▶ print ' (multi value)' if $opt->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->is_array 0 0 0
144 0 print "\n";
145 0 ▶ printf "\t\t%s\n", $help->get_help($arg) if $help->get_help($arg);
Branch: 1 branch
BranchTrueFalseTotal
if $help->get_help($arg) 0 0 0
146 }
147  
148 ##########################################################################
149  
150 0 %opts = ();
151 0 foreach my $opt (@opts) {
152  
153 0 ▶ if ( $opt->is_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->is_flag) 0 0 0
154 0 $opts{ $opt->cli_arg } = $opt;
155  
156 # and $opt->is_hard) {
157 # $opts{ $opt->cli_arg } = $opt->method_arg;
158 # }
159 }
160 }
161  
162 # print "\n" if keys %opts;
163  
164 # print flags
165 0 foreach my $arg ( sort { uc($a) cmp uc($b) } keys %opts ) {
166 0 my $opt = $opts{$arg};
167 0 printf "\t[ --%s ]\n", $arg;
168 0 ▶ printf "\t\t%s\n", $help->get_help($arg) if $help->get_help($arg);
Branch: 1 branch
BranchTrueFalseTotal
if $help->get_help($arg) 0 0 0
169 }
170  
171 ##########################################################################
172 # print examples
173 0 ▶ if ( $help->has_examples ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help->has_examples) 0 0 0
174  
175 0 my $cmd = sprintf "%s %s", basename($0), $self->name;
176  
177 0 print "\n";
178 0 print "Examples:\n";
179 0 foreach my $eg ( @{ $help->examples } ) {
180 0 printf "\t%s %s\n", $cmd, $eg;
181 }
182 }
183  
184 #########################################################################
185  
186 0 print "\n";
187 0 exit;
188 }
189  
190 17 ▶method _new_class {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _new_class: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 30 0 30
191  
192 30 my $class = $self->class;
193 30 my $class_name = $class->name;
194 30 my %attr = $class->get_signature;
195  
196 28 load $class_name;
197 28 ▶ my $obj =
Branch: 1 branch
BranchTrueFalseTotal
$self->use_argv_map ? : 1 27 28
198 $class_name->new( %attr, use_argv_map => $self->use_argv_map ? 1 : 0 );
199  
200 #
201 # validate required class attributes were provided
202 #
203 28 my @soft_req = $class->find_req_attrs( hard => 0, soft => 1 );
204  
205 28 foreach my $opt (@soft_req) {
206  
207 2 my $attr = $opt->method_arg;
208  
209 2 ▶ if ( !defined $obj->$attr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->$attr) 1 1 2
210 1 confess "failed to determine $attr";
211 }
212 }
213
214 27 return $obj;
215 }
216  
217 17 ▶method do {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine do: called CC=5 SCAR=16.3
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 30 0 30
218  
219 #
220 # this creates an instance of the user class defined in the yaml
221 #
222 30 my $obj = $self->_new_class;
223  
224 #
225 # prepare the method args from @ARGV or %ARGV
226 #
227 27 my $method = $self->method;
228 27 my $method_name = $method->name;
229 27 my %sig = $method->get_signature;
230
231 24 ▶ if ( $self->use_argv_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_argv_map) { } 1 23 24
232 1 ▶ if ( keys %ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %ARGV) 0 1 1
233 0 my @argv = %ARGV;
234 0 $self->die("extra args detected: @argv");
235 }
236 }
237 else {
238 23 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 1 22 23
239 1 $self->die("extra args detected: @ARGV");
240 }
241 }
242  
243 #
244 # finally invoke the actual method
245 #
246 23 return $obj->$method_name(%sig);
247 }
248  
249 17 1;
↑ Summary lib/CLI/Driver/CommonRole.pm »