lib/CLI/Driver/Action.pm

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