Cmd.pm

filter: Statementstmt 94.9% 188 / 198 Branchbran 67.3% 35 / 52 Conditioncond 53.3% 16 / 30 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 94.2% 49 / 52 Podpod 95.4% 21 / 22 totaltotal 83.3% 311 / 373 CC 44 SCAR 12.2
CC
44
Cov
85%
_command
11.4
should_ignore
6.0
_load_default_plugin
4.1
CRAP 3.4
« Cmd/Subdispatch/DashedStyle.pm ↑ Summary Cmd/Simple.pm »
1 # The "experimental" below is not actually scary. The feature went on to be
2 # de-experimental-ized with no changes and is now on by default in perl v5.24
3 # and later. -- rjbs, 2021-03-14
4 17 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use experimental qw(postderef postderef_qq);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package App::Cmd 0.340;
9  
10 17 ▶use parent 'App::Cmd::ArgProcessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # ABSTRACT: write command line apps with less suffering
12  
13 17 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Module::Pluggable::Object ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 17 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=1.2
18 collectors => {
19 -ignore => \'_setup_ignore',
20 -command => \'_setup_command',
21 0 ▶ -run => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 warn "using -run to run your command is deprecated\n";
23 0 $_[1]->{class}->run; 1
24 },
25 },
26 17 };
27  
28 5 ▶sub _setup_command {
Subroutine _setup_command: called CC=3 SCAR=11.0
29 5 my ($self, $val, $data) = @_;
30 5 my $into = $data->{into};
31  
32 5 ▶ Carp::confess "App::Cmd -command setup requested for already-setup class"
Branch: 1 branch
BranchTrueFalseTotal
if $into->isa("App::Cmd::Command") 0 5 5
33 if $into->isa('App::Cmd::Command');
34  
35 {
36 5 my $base = $self->_default_command_base;
37 5 Class::Load::load_class($base);
38 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 push @{"$into\::ISA"}, $base;
40 }
41  
42 5 $self->_register_command($into);
43  
44 5 for my $plugin ($self->_plugin_plugins) {
45 4 $plugin->import_from_plugin({ into => $into });
46 }
47  
48 5 1;
49 }
50  
51 1 ▶sub _setup_ignore {
Subroutine _setup_ignore: called CC=2 SCAR=7.0
52 1 my ($self, $val, $data) = @_;
53 1 my $into = $data->{into};
54  
55 1 ▶ Carp::confess "App::Cmd -ignore setup requested for already-setup class"
Branch: 1 branch
BranchTrueFalseTotal
if $into->isa("App::Cmd::Command") 0 1 1
56 if $into->isa('App::Cmd::Command');
57  
58 1 $self->_register_ignore($into);
59  
60 1 1;
61 }
62  
63 0 ▶sub _plugin_plugins { return }
Subroutine _plugin_plugins: not called CC=1 SCAR=6.9
64  
65 #pod =head1 SYNOPSIS
66 #pod
67 #pod in F<yourcmd>:
68 #pod
69 #pod use YourApp;
70 #pod YourApp->run;
71 #pod
72 #pod in F<YourApp.pm>:
73 #pod
74 #pod package YourApp;
75 #pod use App::Cmd::Setup -app;
76 #pod 1;
77 #pod
78 #pod in F<YourApp/Command/blort.pm>:
79 #pod
80 #pod package YourApp::Command::blort;
81 #pod use YourApp -command;
82 #pod use strict; use warnings;
83 #pod
84 #pod sub abstract { "blortex algorithm" }
85 #pod
86 #pod sub description { "Long description on blortex algorithm" }
87 #pod
88 #pod sub opt_spec {
89 #pod return (
90 #pod [ "blortex|X", "use the blortex algorithm" ],
91 #pod [ "recheck|r", "recheck all results" ],
92 #pod );
93 #pod }
94 #pod
95 #pod sub validate_args {
96 #pod my ($self, $opt, $args) = @_;
97 #pod
98 #pod # no args allowed but options!
99 #pod $self->usage_error("No args allowed") if @$args;
100 #pod }
101 #pod
102 #pod sub execute {
103 #pod my ($self, $opt, $args) = @_;
104 #pod
105 #pod my $result = $opt->{blortex} ? blortex() : blort();
106 #pod
107 #pod recheck($result) if $opt->{recheck};
108 #pod
109 #pod print $result;
110 #pod }
111 #pod
112 #pod and, finally, at the command line:
113 #pod
114 #pod knight!rjbs$ yourcmd blort --recheck
115 #pod
116 #pod All blorts successful.
117 #pod
118 #pod =head1 DESCRIPTION
119 #pod
120 #pod App::Cmd is intended to make it easy to write complex command-line applications
121 #pod without having to think about most of the annoying things usually involved.
122 #pod
123 #pod For information on how to start using App::Cmd, see L<App::Cmd::Tutorial>.
124 #pod
125 #pod =method new
126 #pod
127 #pod my $cmd = App::Cmd->new(\%arg);
128 #pod
129 #pod This method returns a new App::Cmd object. During initialization, command
130 #pod plugins will be loaded.
131 #pod
132 #pod Valid arguments are:
133 #pod
134 #pod no_commands_plugin - if true, the command list plugin is not added
135 #pod
136 #pod no_help_plugin - if true, the help plugin is not added
137 #pod
138 #pod no_version_plugin - if true, the version plugin is not added
139 #pod
140 #pod show_version_cmd - if true, the version command will be shown in the
141 #pod command list
142 #pod
143 #pod plugin_search_path - The path to search for commands in. Defaults to
144 #pod results of plugin_search_path method
145 #pod
146 #pod If C<no_commands_plugin> is not given, L<App::Cmd::Command::commands> will be
147 #pod required, and it will be registered to handle all of its command names not
148 #pod handled by other plugins.
149 #pod
150 #pod If C<no_help_plugin> is not given, L<App::Cmd::Command::help> will be required,
151 #pod and it will be registered to handle all of its command names not handled by
152 #pod other plugins. B<Note:> "help" is the default command, so if you do not load
153 #pod the default help plugin, you should provide your own or override the
154 #pod C<default_command> method.
155 #pod
156 #pod If C<no_version_plugin> is not given, L<App::Cmd::Command::version> will be
157 #pod required to show the application's version with command C<--version>. By
158 #pod default, the version command is not included in the command list. Pass
159 #pod C<show_version_cmd> to include the version command in the list.
160 #pod
161 #pod =cut
162  
163 27 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
164 27 my ($class, $arg) = @_;
165  
166 27 my $arg0 = $0;
167 27 my $base = File::Basename::basename $arg0;
168  
169 my $self = {
170 arg0 => $base,
171 full_arg0 => $arg0,
172 27 ▶ show_version => $arg->{show_version_cmd} // 0,
Condition100%2/2
$arg->{"show_version_cmd"} // 0
l!l
225
MC/DC100%1/1
$arg->{"show_version_cmd"} // 0
$arg->{"show_version_cmd"}
Truth table100%2/2
A
A= $arg->{"show_version_cmd"}
Aresult
11
00
173 };
174  
175 27 bless $self, $class;
176  
177 27 $self->{command} = $self->_command($arg);
178  
179 27 return $self;
180 }
181  
182 # effectively, returns the command-to-plugin mapping guts of a Cmd
183 # if called on a class or on a Cmd with no mapping, construct a new hashref
184 # suitable for use as the object's mapping
185 198 ▶sub _command {
Subroutine _command: called CC=11 SCAR=24.3
186 198 my ($self, $arg) = @_;
187 198 ▶ return $self->{command} if ref $self and $self->{command};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"command"} 167 31 198
Condition66%2/3
ref $self and $self->{"command"}
!ll&&!rl&&r
031167
MC/DC50%1/2
ref $self and $self->{"command"}
ref $self $self->{"command"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"command"}
ABresult
0X0
100
111
188  
189 # TODO _default_command_base can be wrong if people are not using
190 # ::Setup and have no ::Command :(
191 #
192 # my $want_isa = $self->_default_command_base;
193 # -- kentnl, 2010-12
194 31 my $want_isa = 'App::Cmd::Command';
195  
196 31 my %plugin;
197 31 for my $plugin ($self->_plugins) {
198  
199 105 Class::Load::load_class($plugin);
200  
201 # relies on either the plugin itself registering as ignored
202 # during compile ( use MyApp::Cmd -ignore )
203 # or being explicitly registered elsewhere ( blacklisted )
204 # via $app_cmd->_register_ignore( $class )
205 # -- kentnl, 2011-09
206 105 ▶ next if $self->should_ignore( $plugin );
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_ignore($plugin) 0 105 105
207  
208 105 ▶ die "$plugin is not a " . $want_isa
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->isa($want_isa) 0 105 105
209 unless $plugin->isa($want_isa);
210  
211 105 ▶ next unless $plugin->can("command_names");
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can("command_names") 0 105 105
212  
213 105 foreach my $command (map { lc } $plugin->command_names) {
214 die "two plugins for command $command: $plugin and $plugin{$command}\n"
215 119 ▶ if exists $plugin{$command};
Branch: 1 branch
BranchTrueFalseTotal
if exists $plugin{$command} 0 119 119
216  
217 119 $plugin{$command} = $plugin;
218 }
219 }
220  
221 31 $self->_load_default_plugin($_, $arg, \%plugin) for qw(commands help version);
222  
223 31 ▶ if ($self->allow_any_unambiguous_abbrev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_any_unambiguous_abbrev) 1 30 31
224 # add abbreviations to list of authorized commands
225 1 require Text::Abbrev;
226 1 my %abbrev = Text::Abbrev::abbrev( keys %plugin );
227 1 @plugin{ keys %abbrev } = @plugin{ values %abbrev };
228 }
229  
230 31 return \%plugin;
231 }
232  
233 # ->_plugins won't be called more than once on any given App::Cmd, but since
234 # finding plugins can be a bit expensive, we'll do a lousy cache here.
235 # -- rjbs, 2007-10-09
236 17 my %plugins_for;
237 32 ▶sub _plugins {
Subroutine _plugins: called CC=3 SCAR=11.0
238 32 my ($self) = @_;
239 32 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2660
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
240  
241 32 ▶ return $plugins_for{$class}->@* if $plugins_for{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $plugins_for{$class} 21 11 32
242  
243 11 my $finder = Module::Pluggable::Object->new(
244 search_path => $self->plugin_search_path,
245 $self->_module_pluggable_options,
246 );
247  
248 11 my @plugins = $finder->plugins;
249 11 $plugins_for{$class} = \@plugins;
250  
251 11 return @plugins;
252 }
253  
254 5 ▶sub _register_command {
Subroutine _register_command: called CC=3 SCAR=11.8
255 5 my ($self, $cmd_class) = @_;
256 5 $self->_plugins;
257  
258 5 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
259 push $plugins_for{ $class }->@*, $cmd_class
260 5 ▶ unless grep { $_ eq $cmd_class } $plugins_for{ $class }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $cmd_class;} @{$plugins_for{$class}} 0 5 5
261 }
262  
263 my %ignored_for;
264  
265 105 ▶sub should_ignore {
Subroutine should_ignore: called CC=4 SCAR=17.9
Pod undocumented
266 105 my ($self, $cmd_class) = @_;
267 105 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
10500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
268 105 for ($ignored_for{ $class }->@*) {
269 0 ▶ return 1 if $_ eq $cmd_class;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $cmd_class 0 0 0
270 }
271 105 return;
272 }
273  
274 1 ▶sub _register_ignore {
Subroutine _register_ignore: called CC=3 SCAR=13.3
275 1 my ($self, $cmd_class) = @_;
276 1 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
277 push $ignored_for{ $class }->@*, $cmd_class
278 0 ▶ unless grep { $_ eq $cmd_class } $ignored_for{ $class }->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $cmd_class;} @{$ignored_for{$class}} 1 0 1
279 }
280  
281 11 ▶sub _module_pluggable_options {
Subroutine _module_pluggable_options: called CC=1 SCAR=0.0
282 # my ($self) = @_; # no point in creating these ops, just to toss $self
283 11 return;
284 }
285  
286 # load one of the stock plugins, unless requested to squash; unlike normal
287 # plugin loading, command-to-plugin mapping conflicts are silently ignored
288 93 ▶sub _load_default_plugin {
Subroutine _load_default_plugin: called CC=4 SCAR=14.0
289 93 my ($self, $plugin_name, $arg, $plugin_href) = @_;
290 93 ▶ unless ($arg->{"no_$plugin_name\_plugin"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$arg{"no_${plugin_name}_plugin"}) 85 8 93
291 85 my $plugin = "App::Cmd::Command::$plugin_name";
292 85 Class::Load::load_class($plugin);
293 85 for my $command (map { lc } $plugin->command_names) {
294 205 ▶ $plugin_href->{$command} //= $plugin;
Condition33%1/3
$plugin_href->{$command} //= $plugin
l!l&&r!l&&!r
02050
MC/DC0%0/2
$plugin_href->{$command} //= $plugin
$plugin_href->{$command} $plugin
Truth table33%1/3
A //= B
A= $plugin_href->{$command}B= $plugin
ABresult
1X1
011
000
295 }
296 }
297 }
298  
299 #pod =method run
300 #pod
301 #pod $cmd->run;
302 #pod
303 #pod This method runs the application. If called the class, it will instantiate a
304 #pod new App::Cmd object to run.
305 #pod
306 #pod It determines the requested command (generally by consuming the first
307 #pod command-line argument), finds the plugin to handle that command, parses the
308 #pod remaining arguments according to that plugin's rules, and runs the plugin.
309 #pod
310 #pod It passes the contents of the global argument array (C<@ARGV>) to
311 #pod L</C<prepare_command>>, but C<@ARGV> is not altered by running an App::Cmd.
312 #pod
313 #pod =cut
314  
315 27 ▶sub run {
Subroutine run: called CC=2 SCAR=7.0
Pod documented
316 27 my ($self) = @_;
317  
318 # We should probably use Class::Default.
319 27 ▶ $self = $self->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 27 27
320  
321 # prepare the command we're going to run...
322 27 my @argv = $self->prepare_args();
323 27 my ($cmd, $opt, @args) = $self->prepare_command(@argv);
324  
325 # ...and then run it
326 25 $self->execute_command($cmd, $opt, @args);
327 }
328  
329 #pod =method prepare_args
330 #pod
331 #pod Normally App::Cmd uses C<@ARGV> for its commandline arguments. You can override
332 #pod this method to change that behavior for testing or otherwise.
333 #pod
334 #pod =cut
335  
336 27 ▶sub prepare_args {
Subroutine prepare_args: called CC=2 SCAR=6.9
Pod documented
337 27 my ($self) = @_;
338 27 ▶ return scalar(@ARGV)
Branch: 1 branch
BranchTrueFalseTotal
scalar @ARGV ? : 26 1 27
339 ? (@ARGV)
340 : ($self->default_args->@*);
341 }
342  
343 #pod =method default_args
344 #pod
345 #pod If C<L</prepare_args>> is not changed and there are no arguments in C<@ARGV>,
346 #pod this method is called and should return an arrayref to be used as the arguments
347 #pod to the program. By default, it returns an empty arrayref.
348 #pod
349 #pod =cut
350  
351 17 ▶use constant default_args => [];
Subroutine BEGIN: called CC=1 SCAR=0.0
352  
353 #pod =method abstract
354 #pod
355 #pod sub abstract { "command description" }
356 #pod
357 #pod Defines the command abstract: a short description that will be printed in the
358 #pod main command options list.
359 #pod
360 #pod =method description
361 #pod
362 #pod sub description { "Long description" }
363 #pod
364 #pod Defines a longer command description that will be shown when the user asks for
365 #pod help on a specific command.
366 #pod
367 #pod =method arg0
368 #pod
369 #pod =method full_arg0
370 #pod
371 #pod my $program_name = $app->arg0;
372 #pod
373 #pod my $full_program_name = $app->full_arg0;
374 #pod
375 #pod These methods return the name of the program invoked to run this application.
376 #pod This is determined by inspecting C<$0> when the App::Cmd object is
377 #pod instantiated, so it's probably correct, but doing weird things with App::Cmd
378 #pod could lead to weird values from these methods.
379 #pod
380 #pod If the program was run like this:
381 #pod
382 #pod knight!rjbs$ ~/bin/rpg dice 3d6
383 #pod
384 #pod Then the methods return:
385 #pod
386 #pod arg0 - rpg
387 #pod full_arg0 - /Users/rjbs/bin/rpg
388 #pod
389 #pod These values are captured when the App::Cmd object is created, so it is safe to
390 #pod assign to C<$0> later.
391 #pod
392 #pod =cut
393  
394 2 ▶sub arg0 { $_[0]->{arg0} }
Subroutine arg0: called CC=1 SCAR=0.0
Pod documented
395 2 ▶sub full_arg0 { $_[0]->{full_arg0} }
Subroutine full_arg0: called CC=1 SCAR=0.0
Pod documented
396  
397 #pod =method prepare_command
398 #pod
399 #pod my ($cmd, $opt, @args) = $app->prepare_command(@ARGV);
400 #pod
401 #pod This method will load the plugin for the requested command, use its options to
402 #pod parse the command line arguments, and eventually return everything necessary to
403 #pod actually execute the command.
404 #pod
405 #pod =cut
406  
407 34 ▶sub prepare_command {
Subroutine prepare_command: called CC=2 SCAR=7.2
Pod documented
408 34 my ($self, @args) = @_;
409  
410 # figure out first-level dispatch
411 34 my ($command, $opt, @sub_args) = $self->get_command(@args);
412  
413 # set up the global options (which we just determined)
414 34 $self->set_global_options($opt);
415  
416 # find its plugin or else call default plugin (default default is help)
417 34 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) { } 34 0 34
418 34 $self->_prepare_command($command, $opt, @sub_args);
419 } else {
420 0 $self->_prepare_default_command($opt, @sub_args);
421 }
422 }
423  
424 36 ▶sub _prepare_command {
Subroutine _prepare_command: called CC=2 SCAR=6.9
425 36 my ($self, $command, $opt, @args) = @_;
426 36 ▶ if (my $plugin = $self->plugin_for($command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin = $self->plugin_for($command)) { } 35 1 36
427 35 return $plugin->prepare($self, @args);
428 } else {
429 1 return $self->_bad_command($command, $opt, @args);
430 }
431 }
432  
433 1 ▶sub _prepare_default_command {
Subroutine _prepare_default_command: called CC=1 SCAR=0.0
434 1 my ($self, $opt, @sub_args) = @_;
435 1 $self->_prepare_command($self->default_command, $opt, @sub_args);
436 }
437  
438 1 ▶sub _bad_command {
Subroutine _bad_command: called CC=2 SCAR=7.0
439 1 my ($self, $command, $opt, @args) = @_;
440 1 ▶ print "Unrecognized command: $command.\n\nUsage:\n" if defined($command);
Branch: 1 branch
BranchTrueFalseTotal
if defined $command 1 0 1
441  
442 # This should be class data so that, in Bizarro World, two App::Cmds will not
443 # conflict.
444 1 our $_bad++;
445 1 $self->prepare_command(qw(commands --stderr));
446 }
447  
448 17 ▶END { exit 1 if our $_bad };
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if our $_bad 0 17 17
449  
450 #pod =method default_command
451 #pod
452 #pod This method returns the name of the command to run if none is given on the
453 #pod command line. The default default is "help"
454 #pod
455 #pod =cut
456  
457 1 ▶sub default_command { "help" }
Subroutine default_command: called CC=1 SCAR=0.0
Pod documented
458  
459 #pod =method execute_command
460 #pod
461 #pod $app->execute_command($cmd, \%opt, @args);
462 #pod
463 #pod This method will invoke C<validate_args> and then C<run> on C<$cmd>.
464 #pod
465 #pod =cut
466  
467 25 ▶sub execute_command {
Subroutine execute_command: called CC=1 SCAR=0.0
Pod documented
468 25 my ($self, $cmd, $opt, @args) = @_;
469  
470 25 local our $active_cmd = $cmd;
471  
472 25 $cmd->validate_args($opt, \@args);
473 24 $cmd->execute($opt, \@args);
474 }
475  
476 #pod =method plugin_search_path
477 #pod
478 #pod This method returns the plugin_search_path as set. The default implementation,
479 #pod if called on "YourApp::Cmd" will return "YourApp::Cmd::Command"
480 #pod
481 #pod This is a method because it's fun to override it with, for example:
482 #pod
483 #pod use constant plugin_search_path => __PACKAGE__;
484 #pod
485 #pod =cut
486  
487 25 ▶sub _default_command_base {
Subroutine _default_command_base: called CC=2 SCAR=7.0
488 25 my ($self) = @_;
489 25 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
14110
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
490 25 return "$class\::Command";
491 }
492  
493 10 ▶sub _default_plugin_base {
Subroutine _default_plugin_base: called CC=2 SCAR=7.0
494 10 my ($self) = @_;
495 10 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
820
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
496 10 return "$class\::Plugin";
497 }
498  
499 10 ▶sub plugin_search_path {
Subroutine plugin_search_path: called CC=4 SCAR=13.9
Pod documented
500 10 my ($self) = @_;
501  
502 10 my $dcb = $self->_default_command_base;
503 10 ▶ my $ccb = $dcb eq 'App::Cmd::Command'
Branch: 1 branch
BranchTrueFalseTotal
$dcb eq 'App::Cmd::Command' ? : 3 7 10
504 ? $self->App::Cmd::_default_command_base
505 : $self->_default_command_base;
506  
507 10 my @default = ($ccb, $self->_default_plugin_base);
508  
509 10 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 8 2 10
510 8 ▶ return $self->{plugin_search_path} //= \@default;
Condition50%1/2
$self->{"plugin_search_path"} //= \@default
l!l
08
MC/DC0%0/1
$self->{"plugin_search_path"} //= \@default
$self->{"plugin_search_path"}
Truth table50%1/2
A
A= $self->{"plugin_search_path"}
Aresult
11
00
511 } else {
512 2 return \@default;
513 }
514 }
515  
516 #pod =method allow_any_unambiguous_abbrev
517 #pod
518 #pod If this method returns true (which, by default, it does I<not>), then any
519 #pod unambiguous abbreviation for a registered command name will be allowed as a
520 #pod means to use that command. For example, given the following commands:
521 #pod
522 #pod reticulate
523 #pod reload
524 #pod rasterize
525 #pod
526 #pod Then the user could use C<ret> for C<reticulate> or C<ra> for C<rasterize> and
527 #pod so on.
528 #pod
529 #pod =cut
530  
531 30 ▶sub allow_any_unambiguous_abbrev { return 0 }
Subroutine allow_any_unambiguous_abbrev: called CC=1 SCAR=0.0
Pod documented
532  
533 #pod =method global_options
534 #pod
535 #pod if ($cmd->app->global_options->{verbose}) { ... }
536 #pod
537 #pod This method returns the running application's global options as a hashref. If
538 #pod there are no options specified, an empty hashref is returned.
539 #pod
540 #pod =cut
541  
542 2 ▶sub global_options {
Subroutine global_options: called CC=3 SCAR=13.1
Pod documented
543 2 my $self = shift;
544 2 ▶ return $self->{global_options} //= {} if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 2 0 2
Condition50%1/2
$self->{"global_options"} //= {}
l!l
20
MC/DC0%0/1
$self->{"global_options"} //= {}
$self->{"global_options"}
Truth table50%1/2
A
A= $self->{"global_options"}
Aresult
11
00
545 0 return {};
546 }
547  
548 #pod =method set_global_options
549 #pod
550 #pod $app->set_global_options(\%opt);
551 #pod
552 #pod This method sets the global options.
553 #pod
554 #pod =cut
555  
556 36 ▶sub set_global_options {
Subroutine set_global_options: called CC=1 SCAR=0.0
Pod documented
557 36 my ($self, $opt) = @_;
558 36 return $self->{global_options} = $opt;
559 }
560  
561 #pod =method command_names
562 #pod
563 #pod my @names = $cmd->command_names;
564 #pod
565 #pod This returns the commands names which the App::Cmd object will handle.
566 #pod
567 #pod =cut
568  
569 6 ▶sub command_names {
Subroutine command_names: called CC=1 SCAR=0.0
Pod documented
570 6 my ($self) = @_;
571 6 keys $self->_command->%*;
572 }
573  
574 #pod =method command_groups
575 #pod
576 #pod my @groups = $cmd->commands_groups;
577 #pod
578 #pod This method can be implemented to return a grouped list of command names with
579 #pod optional headers. Each group is given as arrayref and each header as string.
580 #pod If an empty list is returned, the commands plugin will show two groups without
581 #pod headers: the first group is for the "help" and "commands" commands, and all
582 #pod other commands are in the second group.
583 #pod
584 #pod =cut
585  
586 4 ▶sub command_groups { }
Subroutine command_groups: called CC=1 SCAR=0.0
Pod documented
587  
588 #pod =method command_plugins
589 #pod
590 #pod my @plugins = $cmd->command_plugins;
591 #pod
592 #pod This method returns the package names of the plugins that implement the
593 #pod App::Cmd object's commands.
594 #pod
595 #pod =cut
596  
597 8 ▶sub command_plugins {
Subroutine command_plugins: called CC=1 SCAR=0.0
Pod documented
598 8 my ($self) = @_;
599 8 my %seen = map {; $_ => 1 } values $self->_command->%*;
600 8 keys %seen;
601 }
602  
603 #pod =method plugin_for
604 #pod
605 #pod my $plugin = $cmd->plugin_for($command);
606 #pod
607 #pod This method returns the plugin (module) for the given command. If no plugin
608 #pod implements the command, it returns false.
609 #pod
610 #pod =cut
611  
612 80 ▶sub plugin_for {
Subroutine plugin_for: called CC=3 SCAR=11.0
Pod documented
613 80 my ($self, $command) = @_;
614 80 ▶ return unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 80 80
615 80 ▶ return unless exists $self->_command->{ $command };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_command->{$command} 3 77 80
616  
617 77 return $self->_command->{ $command };
618 }
619  
620 #pod =method get_command
621 #pod
622 #pod my ($command_name, $opt, @args) = $app->get_command(@args);
623 #pod
624 #pod Process arguments and into a command name and (optional) global options.
625 #pod
626 #pod =cut
627  
628 36 ▶sub get_command {
Subroutine get_command: called CC=2 SCAR=6.9
Pod documented
629 36 my ($self, @args) = @_;
630  
631 36 my ($opt, $args, %fields)
632 = $self->_process_args(\@args, $self->_global_option_processing_params);
633  
634 # map --help to help command
635 36 ▶ if ($opt->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 2 34 36
636 2 unshift @$args, 'help';
637 2 delete $opt->{help};
638 }
639  
640 36 my ($command, $rest) = $self->_cmd_from_args($args);
641  
642 36 $self->{usage} = $fields{usage};
643  
644 36 return ($command, $opt, @$rest);
645 }
646  
647 30 ▶sub _cmd_from_args {
Subroutine _cmd_from_args: called CC=1 SCAR=0.0
648 30 my ($self, $args) = @_;
649  
650 30 my $command = shift @$args;
651 30 return ($command, $args);
652 }
653  
654 36 ▶sub _global_option_processing_params {
Subroutine _global_option_processing_params: called CC=1 SCAR=0.0
655 36 my ($self, @args) = @_;
656  
657 return (
658 36 $self->usage_desc(@args),
659 $self->global_opt_spec(@args),
660 { getopt_conf => [ $self->_getopt_conf->@*, 'pass_through' ] },
661 );
662 }
663  
664 14 ▶sub _getopt_conf {
Subroutine _getopt_conf: called CC=1 SCAR=0.0
665 14 return [qw/pass_through/];
666 }
667  
668 #pod =method usage
669 #pod
670 #pod print $self->app->usage->text;
671 #pod
672 #pod Returns the usage object for the global options.
673 #pod
674 #pod =cut
675  
676 1 ▶sub usage { $_[0]{usage} };
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
677  
678 #pod =method usage_desc
679 #pod
680 #pod The top level usage line. Looks something like
681 #pod
682 #pod "yourapp <command> [options]"
683 #pod
684 #pod =cut
685  
686 34 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
687 # my ($self) = @_; # no point in creating these ops, just to toss $self
688 34 return "%c <command> %o";
689 }
690  
691 #pod =method global_opt_spec
692 #pod
693 #pod Returns a list with help command unless C<no_help_plugin> has been specified or
694 #pod an empty list. Can be overridden for pre-dispatch option processing. This is
695 #pod useful for flags like --verbose.
696 #pod
697 #pod =cut
698  
699 32 ▶sub global_opt_spec {
Subroutine global_opt_spec: called CC=2 SCAR=6.9
Pod documented
700 32 my ($self) = @_;
701  
702 32 my $cmd = $self->{command};
703 32 my %seen;
704 128 my @help = grep { ! $seen{$_}++ }
705 128 reverse sort map { s/^--?//; $_ }
706 32 grep { $cmd->{$_} eq 'App::Cmd::Command::help' } keys %$cmd;
707  
708 32 ▶ return (@help ? [ join('|', @help) => "show help" ] : ());
Branch: 1 branch
BranchTrueFalseTotal
@help ? : 32 0 32
709 }
710  
711 #pod =method usage_error
712 #pod
713 #pod $self->usage_error("Something's wrong!");
714 #pod
715 #pod Used to die with nice usage output, during C<validate_args>.
716 #pod
717 #pod =cut
718  
719 0 ▶sub usage_error {
Subroutine usage_error: not called CC=1 SCAR=6.9
Pod documented
720 0 my ($self, $error) = @_;
721 0 die "Error: $error\nUsage: " . $self->_usage_text;
722 }
723  
724 1 ▶sub _usage_text {
Subroutine _usage_text: called CC=1 SCAR=0.0
725 1 my ($self) = @_;
726 1 my $text = $self->usage->text;
727 1 $text =~ s/\A(\s+)/!/;
728 1 return $text;
729 }
730  
731 #pod =head1 TODO
732 #pod
733 #pod =for :list
734 #pod * publish and bring in Log::Speak (simple quiet/verbose output)
735 #pod * publish and use our internal enhanced describe_options
736 #pod * publish and use our improved simple input routines
737 #pod
738 #pod =cut
739  
740 17 1;
741  
742 __END__
« Cmd/Subdispatch/DashedStyle.pm ↑ Summary Cmd/Simple.pm »