lib/CLI/Framework/Command.pm

filter: Statementstmt 78.9% 109 / 138 Branchbran 61.3% 27 / 44 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.8% 26 / 31 Podpod 100% 19 / 19 totaltotal 73.6% 182 / 247 CC 28 SCAR 18.0
CC
28
Cov
72%
dispatch
20.3
usage
12.0
_manufacture_subcommands_in_dir_tree
7.1
CRAP 6.0
« lib/CLI/Framework/Command/Help.pm ↑ Summary lib/CLI/Framework/Command/Tree.pm »
1 package CLI::Framework::Command;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use warnings::register;
6  
7 5 our $VERSION = 0.04;
8  
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Getopt::Long::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Exception::Class::TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use CLI::Framework::Exceptions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #FIXME-TODO-CLASS_GENERATION:
17 #our %CLASSES; # remember which classes have been auto-generated
18 #
19 #sub import {
20 # my ($class, %import_args) = @_;
21 #
22 # # If caller has supplied import args, CLIF's "inline form" is being used;
23 # # we need to generate command classes dynamically...
24 # while( my ($cmd_pkg, $cmd_def) = each %import_args ) {
25 #
26 ##FIXME-TODO-CLASS_GENERATION: Create the new classes named in $cmd_pkg, injecting the subs indicated
27 ## (whether explicitly or implicitly) by the contents of $cmd_def...
28 #
29 ## $cmd_obj = __PACKAGE__->new();
30 # }
31 #}
32  
33 ###############################
34 #
35 # OBJECT CONSTRUCTION
36 #
37 ###############################
38  
39 sub manufacture {
40 7 ▶ my ($class, $target_pkg) = @_;
Subroutine manufacture: called CC=2 SCAR=7.0
Pod documented
41  
42 # Manufacture base command...
43 7 eval "require $target_pkg"; # (may or may not be pre-loaded)
44 7 ▶ my $object = $target_pkg->new()
Branch: 1 branch
BranchTrueFalseTotal
my $object = $target_pkg->new or croak("Failed to instantiate command package '${target_pkg}' via new(): $!") 0 7 7
45 or croak "Failed to instantiate command package '$target_pkg' via new(): $!";
46  
47 # Recognize subcommands that were defined in their own package files...
48 7 $object->_manufacture_subcommands_in_dir_tree();
49  
50 # Recognize subcommands that have been loaded via an inline definition...
51 7 $object->_register_preloaded_subcommands();
52  
53 7 return $object;
54 }
55  
56 sub _manufacture_subcommands_in_dir_tree {
57 10 ▶ my ($parent_command_object) = @_;
Subroutine _manufacture_subcommands_in_dir_tree: called CC=7 SCAR=19.5
58  
59 # Check for a subdirectory by the name of the current command containing .pm
60 # files representing subcommands, then manufacture() any that are found...
61  
62 # Look for subdirectory with name of current command...
63 10 my $subcommand_dir = Class::Inspector->resolved_filename( ref $parent_command_object );
64 10 substr( $subcommand_dir, -3, 3 ) = ''; # trim trailing '.pm'
65  
66 10 ▶ if( -d $subcommand_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $subcommand_dir) 2 8 10
67 # Directory with name of current command exists; look inside for .pm
68 # files representing subcommands...
69  
70 2 my $dh;
71 2 ▶ opendir( $dh, $subcommand_dir ) or die "cannot opendir '$dh': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $subcommand_dir or die "cannot opendir '${dh}': $!" 0 2 2
72 2 ▶ while( my $subcommand = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subcommand = readdir $dh)) 8 2 10
73 # Ignore non-module files...
74 8 ▶ next unless substr( $subcommand, -3 ) =~ s/\.pm//; # trim trailing '.pm'
Branch: 1 branch
BranchTrueFalseTotal
unless substr($subcommand, -3) =~ s/\.pm// 5 3 8
75  
76 3 my $subcommand_pkg = (ref $parent_command_object).'::'.$subcommand;
77 3 eval "require $subcommand_pkg";
78 3 ▶ if( $subcommand_pkg->isa(ref $parent_command_object) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcommand_pkg->isa(ref $parent_command_object)) 3 0 3
79 3 ▶ my $subcommand_obj = $subcommand_pkg->new()
Branch: 1 branch
BranchTrueFalseTotal
my $subcommand_obj = $subcommand_pkg->new or croak("Failed to instantiate subcommand", "'${subcommand_pkg}' via method new(): $!") 0 3 3
80 or croak 'Failed to instantiate subcommand',
81 "'$subcommand_pkg' via method new(): $!";
82  
83 3 $parent_command_object->register_subcommand( $subcommand_obj );
84  
85 3 $subcommand_obj->_manufacture_subcommands_in_dir_tree();
86 3 $subcommand_obj->_register_preloaded_subcommands();
87 }
88 # else {
89 # warnings::warn "Found a non-subclass Perl package file in search path: '$subcommand_pkg' -- ignoring..."
90 # if warnings::enabled;
91 # }
92 }
93 }
94 10 return 1;
95 }
96  
97 sub _register_preloaded_subcommands {
98 13 ▶ my ($parent_cmd_obj) = @_;
Subroutine _register_preloaded_subcommands: called CC=6 SCAR=17.9
99  
100 # Find direct subclasses and register them beneath the given parent...
101  
102 # Class::Inspector::subclasses actually finds all *descendants*
103 # (not just direct subclasses)...
104 13 ▶ my $descendants = sub { Class::Inspector->subclasses(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
105 13 my $descendant_names = $descendants->( ref $parent_cmd_obj );
106 13 ▶ return unless ref $descendant_names eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $descendant_names eq "ARRAY" 9 4 13
107  
108 4 for my $descendant_cmd ( @$descendant_names ) {
109 # (skip if already registered)
110 8 ▶ next if $parent_cmd_obj->package_is_registered( $descendant_cmd );
Branch: 1 branch
BranchTrueFalseTotal
if $parent_cmd_obj->package_is_registered($descendant_cmd) 3 5 8
111  
112 # Find the direct parent class(es) of the descendant...
113 5 my @direct_parents;
114 5 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 5 @direct_parents = @{ $descendant_cmd.'::ISA' };
116 }
117 5 for my $direct_parent_of_descendant (@direct_parents) {
118 # If the descendant is a *direct* subclass of the given parent...
119 5 ▶ if( $direct_parent_of_descendant eq ref $parent_cmd_obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direct_parent_of_descendant eq ref $parent_cmd_obj) 3 2 5
120 # ...register child command as subcommand of parent...
121 3 my $child_cmd = $descendant_cmd->new();
122 3 $parent_cmd_obj->register_subcommand( $child_cmd );
123 3 $child_cmd->_register_preloaded_subcommands();
124 }
125 }
126 }
127 4 return 1;
128 }
129  
130 10 ▶sub new { bless { _cache => undef }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
131  
132 5 ▶sub set_cache { $_[0]->{_cache} = $_[1] }
Subroutine set_cache: called CC=1 SCAR=0.0
Pod documented
133 3 ▶sub cache { $_[0]->{_cache} }
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
134  
135 ###############################
136 #
137 # COMMAND DISPATCHING
138 #
139 ###############################
140  
141 0 ▶sub get_default_usage { $_[0]->{_default_usage} }
Subroutine get_default_usage: not called CC=1 SCAR=6.9
Pod documented
142 5 ▶sub set_default_usage { $_[0]->{_default_usage} = $_[1] }
Subroutine set_default_usage: called CC=1 SCAR=0.0
Pod documented
143  
144 sub usage {
145 0 ▶ my ($cmd, $subcommand_name, @subcommand_args) = @_;
Subroutine usage: not called CC=3 SCAR=24.8
Pod documented
146  
147 # Allow subcommand aliases in place of subcommand name...
148 0 $cmd->_canonicalize($subcommand_name);
149  
150 0 my $usage_text;
151 0 ▶ if(my $subcommand = $cmd->registered_subcommand_object($subcommand_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subcommand = $cmd->registered_subcommand_object($subcommand_name)) { } 0 0 0
152 # Get usage from subcommand object...
153 0 $usage_text = $subcommand->usage(@subcommand_args);
154 }
155 else {
156 # Get usage from Command object...
157 0 $usage_text = $cmd->usage_text();
158 }
159 # Finally, fall back to default command usage message...
160 0 ▶ $usage_text ||= $cmd->get_default_usage();
Condition0%0/3
$usage_text ||= $cmd->get_default_usage
l!l&&r!l&&!r
000
MC/DC0%0/2
$usage_text ||= $cmd->get_default_usage
$usage_text $cmd->get_default_usage
Truth table0%0/3
A ||= B
A= $usage_textB= $cmd->get_default_usage
ABresult
1X1
011
000
161 0 return $usage_text;
162 }
163  
164 sub _canonicalize {
165 5 ▶ my ($cmd, $input) = @_;
Subroutine _canonicalize: called CC=4 SCAR=17.2
166  
167 # Translate shorthand aliases for subcommands to full names...
168  
169 5 ▶ return unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 4 1 5
170  
171 1 my %aliases = $cmd->subcommand_alias();
172 1 ▶ return unless %aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless %aliases 1 0 1
173  
174 0 ▶ my $command_name = $aliases{$input} || $input;
Condition0%0/3
$aliases{$input} || $input
l!l&&r!l&&!r
000
MC/DC0%0/2
$aliases{$input} || $input
$aliases{$input} $input
Truth table0%0/3
A || B
A= $aliases{$input}B= $input
ABresult
1X1
011
000
175 0 $_[1] = $command_name;
176 }
177  
178 #
179 # ARGV_Format
180 #
181 # $ app [app-opts] <cmd> [cmd-opts] <the rest>
182 #
183 # params contain: $cmd = <cmd>, $cmd_opts = [cmd-opts], @args = <the rest>
184 #
185 # <the rest> could, in turn, indicate nested subcommands:
186 # { <subcmd> [subcmd-opts] {...} } [subcmd-args]
187 #
188  
189 sub dispatch {
190 5 ▶ my ($cmd, $cmd_opts, @args) = @_;
Subroutine dispatch: called CC=8 SCAR=30.1
Pod documented
191  
192 # --- VALIDATE COMMAND OPTIONS AND ARGS ---
193 5 eval { $cmd->validate($cmd_opts, @args) };
194 5 ▶ if( catch my $e ) { # (command failed validation)
Branch: 1 branch
BranchTrueFalseTotal
if (&catch(my $e)) 0 5 5
195 0 throw_cmd_validation_exception( error => $e );
196 }
197 # Check if a subcommand is being requested...
198 5 my $first_arg = shift @args; # consume potential subcommand name from input
199 5 $cmd->_canonicalize( $first_arg );
200 5 my ($subcmd_opts, $subcmd_usage);
201 5 ▶ if( my $subcommand = $cmd->registered_subcommand_object($first_arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subcommand = $cmd->registered_subcommand_object($first_arg)) { } 0 5 5
202 # A subcommand is being requested; parse its options...
203 0 @ARGV = @args;
204 0 my $format = $cmd->name().' '.$subcommand->name().'%o ...';
205 0 eval { ($subcmd_opts, $subcmd_usage) =
206 describe_options( $format, $subcommand->option_spec() )
207 };
208 0 ▶ if( catch my $e ) { # (subcommand failed options parsing)
Branch: 1 branch
BranchTrueFalseTotal
if (&catch(my $e)) 0 0 0
209 0 ▶ $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow };
Branch: 1 branch
BranchTrueFalseTotal
$e->isa("CLI::Framework::Exception") and do { $e->rethrow } 0 0 0
210 0 throw_cmd_opts_parse_exception( error => $e );
211 }
212 0 $subcommand->set_default_usage( $subcmd_usage->text() );
213  
214 # Reset arg list to reflect only arguments ( options may have been
215 # consumed by describe_options() )...
216 0 @args = @ARGV;
217  
218 # Pass session data to subcommand...
219 0 $subcommand->set_cache( $cmd->cache() );
220  
221 # --- NOTIFY MASTER COMMAND OF SUBCOMMAND DISPATCH ---
222 0 $cmd->notify_of_subcommand_dispatch( $subcommand, $cmd_opts, @args );
223  
224 # Dispatch subcommand with its options and the remaining args...
225 0 $subcommand->dispatch( $subcmd_opts, @args );
226 }
227 else {
228 # If first arg is not a subcommand then put it back in input...
229 5 ▶ unshift @args, $first_arg if defined $first_arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_arg 1 4 5
230  
231 # ...and run the command itself...
232 5 my $output;
233 5 eval { $output = $cmd->run( $cmd_opts, @args ) };
234 5 ▶ if( catch my $e ) { # (error during command execution)
Branch: 1 branch
BranchTrueFalseTotal
if (&catch(my $e)) 0 5 5
235 0 ▶ $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow };
Branch: 1 branch
BranchTrueFalseTotal
$e->isa("CLI::Framework::Exception") and do { $e->rethrow } 0 0 0
236 0 throw_cmd_run_exception( error => $e );
237 }
238 5 return $output;
239 }
240 }
241  
242 ###############################
243 #
244 # COMMAND REGISTRATION
245 #
246 ###############################
247  
248 9 ▶sub registered_subcommand_names { keys %{$_[0]->{_subcommands}} }
Subroutine registered_subcommand_names: called CC=1 SCAR=0.0
Pod documented
249  
250 sub registered_subcommand_object {
251 17 ▶ my ($cmd, $subcommand_name) = @_;
Subroutine registered_subcommand_object: called CC=2 SCAR=6.9
Pod documented
252  
253 17 ▶ return unless $subcommand_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $subcommand_name 4 13 17
254  
255 13 return $cmd->{_subcommands}->{$subcommand_name};
256 }
257  
258 sub register_subcommand {
259 6 ▶ my ($cmd, $subcommand_obj) = @_;
Subroutine register_subcommand: called CC=3 SCAR=11.8
Pod documented
260  
261 6 ▶ return unless $subcommand_obj &&
Branch: 1 branch
BranchTrueFalseTotal
unless $subcommand_obj and $subcommand_obj->isa("CLI::Framework::Command") 0 6 6
Condition33%1/3
$subcommand_obj and $subcommand_obj->isa("CLI::Framework::Command")
!ll&&!rl&&r
006
MC/DC0%0/2
$subcommand_obj and $subcommand_obj->isa("CLI::Framework::Command")
$subcommand_obj $subcommand_obj->isa("CLI::Framework::Command")
Truth table33%1/3
A and B
A= $subcommand_objB= $subcommand_obj->isa("CLI::Framework::Command")
ABresult
0X0
100
111
262 $subcommand_obj->isa("CLI::Framework::Command");
263  
264 6 my $subcommand_name = $subcommand_obj->name();
265 6 $cmd->{_subcommands}->{$subcommand_name} = $subcommand_obj;
266  
267 6 return $subcommand_obj;
268 }
269  
270 sub package_is_registered {
271 8 ▶ my ($cmd, $pkg) = @_;
Subroutine package_is_registered: called CC=1 SCAR=0.0
Pod documented
272 8 my @registered_pkgs = map { ref $_ } values %{ $cmd->{_subcommands} };
273 8 return grep { $pkg eq $_ } @registered_pkgs;
274 }
275  
276 ###############################
277 #
278 # COMMAND SUBCLASS HOOKS
279 #
280 ###############################
281  
282 sub name {
283 14 ▶ my ($cmd) = @_;
Subroutine name: called CC=1 SCAR=0.0
Pod documented
284  
285 # Use base name of package as command name...
286 14 my $pkg = ref $cmd;
287 14 my @pkg_parts = split /::/, $pkg;
288 14 return lc $pkg_parts[-1];
289 }
290  
291 5 ▶sub option_spec { ( ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
292  
293 1 ▶sub subcommand_alias { ( ) }
Subroutine subcommand_alias: called CC=1 SCAR=0.0
Pod documented
294  
295 ▶sub validate { }
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
296  
297 ▶sub notify_of_subcommand_dispatch { }
Subroutine notify_of_subcommand_dispatch: not called CC=1 SCAR=0.0
Pod documented
298  
299 ▶sub usage_text { }
Subroutine usage_text: not called CC=1 SCAR=0.0
Pod documented
300  
301 0 ▶sub run { $_[0]->usage() }
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
302  
303 #-------
304 5 1;
305  
306 __END__
« lib/CLI/Framework/Command/Help.pm ↑ Summary lib/CLI/Framework/Command/Tree.pm »