CLI.pm

filter: Statementstmt 96.8% 92 / 95 Branchbran 86.6% 26 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 90.0% 18 / 20 Podpod 40.0% 4 / 10 totaltotal 87.2% 144 / 165 CC 18 SCAR 10.7
CC
18
Cov
92%
get_cmd
6.0
error_cmd
4.0
cmd_map
4.0
CRAP 2.9
« CLI/Helper.pm ↑ Summary CLI/Command/Commands.pm »
1 package App::CLI;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.52';
9  
10 =encoding utf8
11  
12 =head1 NAME
13  
14 App::CLI - Dispatcher module for command line interface programs
15  
16 =head1 SYNOPSIS
17  
18 package MyApp;
19 use base 'App::CLI'; # the DISPATCHER of your App
20 # it's not necessary to put the dispatcher
21 # on the top level of your App
22  
23 package main;
24  
25 MyApp->dispatch; # call the dispatcher where you want
26  
27  
28 package MyApp::List;
29 use base qw(App::CLI::Command); # any (SUB)COMMAND of your App
30  
31 use constant options => (
32 "h|help" => "help",
33 "verbose" => "verbose",
34 'n|name=s' => 'name',
35 );
36  
37 use constant subcommands => qw(User Nickname type); # if you want subcommands
38 # automatically dispatch to subcommands
39 # when invoke $ myapp list [user|nickname|--type]
40 # note 'type' is not capitalized
41 # it is a deprecated subcommand
42  
43 sub run {
44 my ($self, @args) = @_;
45  
46 print "verbose" if $self->{verbose};
47 my $name = $self->{name}; # get arg following long option --name
48  
49 if ($self->{help}) {
50 # if $ myapp list --help or $ myapp list -h
51 # only output PODs
52 } else {
53 # do something when invoking $ myapp list
54 # without subcommand and --help
55 }
56 }
57  
58  
59 package MyApp::List::User;
60 use base qw(App::CLI::Command);
61 use constant options => (
62 "h|help" => "help",
63 );
64  
65 sub run {
66 my ($self,@args) = @_;
67 # code for listing user
68 }
69  
70  
71 pakcage MyApp::List::Nickname;
72 use base qw(App::CLI::Command);
73 use constant options => (
74 "sort=s" => "sort",
75 );
76  
77 sub run {
78 my ($self,@args) = @_;
79 # code for listing nickname
80 }
81  
82  
83 package MyApp::List::type; # old genre of subcommand could not cascade infinitely
84 use base qw(MyApp::List); # should inherit its parent's command
85  
86 sub run {
87 my ($self, @args);
88 # run to here when invoking $ myapp list --type
89 }
90  
91  
92 package MyApp::Help;
93 use base 'App::CLI::Command::Help';
94  
95 use constant options => (
96 'verbose' => 'verbose',
97 );
98  
99 sub run {
100 my ($self, @arg) = @_;
101 # do something
102 $self->SUPER(@_); # App::CLI::Command::Help would output POD of each command
103 }
104  
105 =head1 DESCRIPTION
106  
107 C<App::CLI> dispatches CLI (command line interface) based commands
108 into command classes. It also supports subcommand and per-command
109 options.
110  
111 =head2 Methods
112  
113 =cut
114  
115 2 ▶use App::CLI::Helper;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 2 ▶use constant alias => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 ▶use constant global_options => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
120 2 ▶use constant options => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 30 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
123 30 my ( $class, @args ) = @_;
124 30 my $self = bless {@args}, $class;
125 30 $self->{'app_argv'} = undef;
126  
127 30 return $self;
128 }
129  
130 45 ▶sub app_argv {
Subroutine app_argv: called CC=2 SCAR=6.9
Pod undocumented
131 45 my $self = shift;
132  
133 45 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 42 3 45
134 42 $self->{'app_argv'} = shift;
135 }
136  
137 45 return $self->{'app_argv'};
138 }
139  
140 22 ▶sub prepare {
Subroutine prepare: called CC=2 SCAR=6.9
Pod undocumented
141 22 my $self = shift;
142 22 my $data = {};
143  
144 22 $self->get_opt(
145 [qw(no_ignore_case bundling pass_through)],
146 opt_map( $data, $self->global_options )
147 );
148  
149 22 my $command_name = shift @ARGV;
150 22 my $cmd = $self->get_cmd( $command_name, @_, $data );
151  
152 20 ▶ while ( $cmd->cascadable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->cascadable) 6 20 26
153 6 $cmd = $cmd->cascading;
154 }
155  
156 20 $self->get_opt( [qw(no_ignore_case bundling)],
157 opt_map( $cmd, $cmd->command_options ) );
158  
159 20 $cmd = $cmd->subcommand;
160  
161 20 return $cmd;
162 }
163  
164 =head3 get_opt([@config], %opt_map)
165  
166 Give options map, processed by L<Getopt::Long::Parser>.
167  
168 =cut
169  
170 42 ▶sub get_opt {
Subroutine get_opt: called CC=2 SCAR=7.2
Pod documented
171 42 my ( $self, $config, @app_options ) = @_;
172 42 my $p = Getopt::Long::Parser->new;
173 42 $p->configure(@$config);
174 42 my $err = '';
175 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 my $msg = shift;
177 0 $err .= "$msg";
178 42 };
179 42 my @current_argv = @ARGV;
180 42 $self->app_argv( \@current_argv );
181 42 ▶ die $self->error_opt($err) unless $p->getoptions(@app_options);
Branch: 1 branch
BranchTrueFalseTotal
unless $p->getoptions(@app_options) 0 42 42
182 }
183  
184 42 ▶sub opt_map {
Subroutine opt_map: called CC=2 SCAR=7.1
Pod undocumented
185 42 my ( $self, %opt ) = @_;
186 return
187 42 ▶ map { $_ => ref( $opt{$_} ) ? $opt{$_} : \$self->{ $opt{$_} } } keys %opt;
Branch: 1 branch
BranchTrueFalseTotal
ref $opt{$_} ? : 0 55 55
188 }
189  
190 =head3 dispatch(@args)
191  
192 Interface of dispatcher
193  
194 =cut
195  
196 22 ▶sub dispatch {
Subroutine dispatch: called CC=3 SCAR=11.0
Pod documented
197 22 my ( $self, @args ) = @_;
198 22 ▶ $self = $self->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 9 13 22
199  
200 22 ▶ $self->app($self) if $self->can('app');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("app") 22 0 22
201  
202 22 my $cmd = $self->prepare(@args);
203 20 $cmd->run_command(@ARGV);
204 }
205  
206 =head3 cmd_map($cmd)
207  
208 Find the name of the package implementing the requested command.
209  
210 The command is first searched for in C<alias>. If the alias exists and points
211 to a package name starting with the C<+> sign, then that package name (minus
212 the C<+> sign) is returned. This makes it possible to map commands to arbitrary
213 packages.
214  
215 Otherwise, the package is searched for in the result of calling C<commands>,
216 and a package name is constructed by upper-casing the first character of the
217 command name, and appending it to the package name of the app itself.
218  
219 If both of these fail, and the command does not map to any package name,
220 C<undef> is returned instead.
221  
222 =cut
223  
224 24 ▶sub cmd_map {
Subroutine cmd_map: called CC=4 SCAR=13.9
Pod documented
225 24 my ( $self, $cmd ) = @_;
226  
227 24 my %alias = $self->alias;
228  
229 24 ▶ if ( exists $alias{$cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $alias{$cmd}) 4 20 24
230 4 $cmd = $alias{$cmd};
231  
232 # Alias points to package name, return immediately
233 4 ▶ return $cmd if $cmd =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ s/^\+// 3 1 4
234 }
235  
236 21 ($cmd) = grep { $_ eq $cmd } $self->commands;
237  
238 # No such command
239 21 ▶ return unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 2 19 21
240  
241 19 my $base = ref $self;
242 19 return join '::', $base, ucfirst $cmd;
243 }
244  
245 4 ▶sub error_cmd {
Subroutine error_cmd: called CC=4 SCAR=13.9
Pod undocumented
246 4 my ( $self, $pkg ) = @_;
247  
248 4 my $cmd;
249 4 ▶ if ( defined($pkg) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pkg) { } 2 2 4
elsif (do { $self->app_argv }->[0]) { } 1 1 2
250 2 ▶ $cmd = ref($pkg) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
251 }
252 2 elsif ( ${ $self->app_argv }[0] ) {
253 1 $cmd = ${ $self->app_argv }[0];
254 }
255 else {
256 1 $cmd = '<empty command>';
257 }
258  
259 4 return "Command '$cmd' not recognized, try $0 --help.\n";
260 }
261  
262 0 ▶sub error_opt { $_[1] }
Subroutine error_opt: not called CC=1 SCAR=6.9
Pod undocumented
263  
264 =head3 get_cmd($cmd, @arg)
265  
266 Return subcommand of first level via C<$ARGV[0]>.
267  
268 =cut
269  
270 26 ▶sub get_cmd {
Subroutine get_cmd: called CC=6 SCAR=18.0
Pod documented
271 26 my ( $self, $cmd, $data ) = @_;
272 26 ▶ die $self->error_cmd($cmd) unless $cmd && $cmd eq lc($cmd);
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd and $cmd eq lc $cmd 2 24 26
Condition66%2/3
$cmd and $cmd eq lc $cmd
!ll&&!rl&&r
2024
MC/DC50%1/2
$cmd and $cmd eq lc $cmd
$cmd $cmd eq lc $cmd
Truth table66%2/3
A and B
A= $cmdB= $cmd eq lc $cmd
ABresult
0X0
100
111
273  
274 24 my $base = ref $self;
275 24 my $pkg = $self->cmd_map($cmd);
276  
277 24 ▶ die $self->error_cmd($cmd) unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 2 22 24
278  
279 22 load_class $pkg;
280  
281 22 ▶ die $self->error_cmd($cmd) unless $pkg->can('run');
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->can("run") 0 22 22
282  
283 22 ▶ my @arg = defined $data ? %$data : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 20 2 22
284 22 $cmd = $pkg->new(@arg);
285 22 $cmd->app($self);
286 22 return $cmd;
287 }
288  
289 =head1 SEE ALSO
290  
291 =over 4
292  
293 =item *
294  
295 L<App::CLI::Command>
296  
297 =item *
298  
299 L<Getopt::Long>
300  
301 =back
302  
303 =head1 AUTHORS
304  
305 =over 4
306  
307 =item *
308  
309 Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
310  
311 =item *
312  
313 Alex Vandiver E<lt>alexmv@bestpractical.comE<gt>
314  
315 =item *
316  
317 Yo-An Lin E<lt>cornelius.howl@gmail.comE<gt>
318  
319 =item *
320  
321 Shelling E<lt>navyblueshellingford@gmail.comE<gt>
322  
323 =item *
324  
325 Paul Cochrane E<lt>paul@liekut.deE<gt> (current maintainer)
326  
327 =back
328  
329 =head1 CONTRIBUTORS
330  
331 The following people have contributed patches to the project:
332  
333 =over 4
334  
335 =item *
336  
337 José Joaquín Atria E<lt>jjatria@gmail.comE<gt>
338  
339 =item *
340  
341 sunnavy E<lt>sunnavy@gmail.comE<gt>
342  
343 =item *
344  
345  
346 Ildar Shaimordanov E<lt>ildar.shaimordanov@gmail.comE<gt>
347  
348 =back
349  
350 =head1 COPYRIGHT
351  
352 Copyright 2005-2010 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.
353 Copyright 2010 by Yo-An Lin E<lt>cornelius.howl@gmail.comE<gt>
354 and Shelling E<lt>navyblueshellingford@gmail.comE<gt>.
355 Copyright 2017-2018 by Paul Cochrane E<lt>paul@liekut.deE<gt>
356  
357 This program is free software; you can redistribute it and/or modify it
358 under the same terms as Perl itself.
359  
360 See L<http://www.perl.com/perl/misc/Artistic.html>
361  
362 =cut
363  
364 2 1;
« CLI/Helper.pm ↑ Summary CLI/Command/Commands.pm »