Cmd/Simple.pm

filter: Statementstmt 93.5% 58 / 62 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 94.4% 17 / 18 Podpod 100% 1 / 1 totaltotal 87.3% 90 / 103 CC 9 SCAR 8.9
CC
9
Cov
88%
import
5.1
__ANON__
4.0
__ANON__
2.7
CRAP 2.4
« Cmd.pm ↑ Summary Cmd/Command/help.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package App::Cmd::Simple 0.340;
5  
6 5 ▶use App::Cmd::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶BEGIN { our @ISA = 'App::Cmd::Command' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: a helper for building one-command App::Cmd applications
10  
11 5 ▶use App::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #pod =head1 SYNOPSIS
15 #pod
16 #pod in F<simplecmd>:
17 #pod
18 #pod use YourApp::Cmd;
19 #pod Your::Cmd->run;
20 #pod
21 #pod in F<YourApp/Cmd.pm>:
22 #pod
23 #pod package YourApp::Cmd 0.01;
24 #pod use parent qw(App::Cmd::Simple);
25 #pod
26 #pod sub opt_spec {
27 #pod return (
28 #pod [ "blortex|X", "use the blortex algorithm" ],
29 #pod [ "recheck|r", "recheck all results" ],
30 #pod );
31 #pod }
32 #pod
33 #pod sub validate_args {
34 #pod my ($self, $opt, $args) = @_;
35 #pod
36 #pod # no args allowed but options!
37 #pod $self->usage_error("No args allowed") if @$args;
38 #pod }
39 #pod
40 #pod sub execute {
41 #pod my ($self, $opt, $args) = @_;
42 #pod
43 #pod my $result = $opt->{blortex} ? blortex() : blort();
44 #pod
45 #pod recheck($result) if $opt->{recheck};
46 #pod
47 #pod print $result;
48 #pod }
49 #pod
50 #pod and, finally, at the command line:
51 #pod
52 #pod knight!rjbs$ simplecmd --recheck
53 #pod
54 #pod All blorts successful.
55 #pod
56 #pod =head1 SUBCLASSING
57 #pod
58 #pod When writing a subclass of App::Cmd:Simple, there are only a few methods that
59 #pod you might want to implement. They behave just like the same-named methods in
60 #pod App::Cmd.
61 #pod
62 #pod =head2 opt_spec
63 #pod
64 #pod This method should be overridden to provide option specifications. (This is
65 #pod list of arguments passed to C<describe_options> from Getopt::Long::Descriptive,
66 #pod after the first.)
67 #pod
68 #pod If not overridden, it returns an empty list.
69 #pod
70 #pod =head2 usage_desc
71 #pod
72 #pod This method should be overridden to provide the top level usage line.
73 #pod It's a one-line summary of how the command is to be invoked, and
74 #pod should be given in the format used for the C<$usage_desc> parameter to
75 #pod C<describe_options> in Getopt::Long::Descriptive.
76 #pod
77 #pod If not overridden, it returns something that prints out like:
78 #pod
79 #pod yourapp [-?h] [long options...]
80 #pod
81 #pod =head2 validate_args
82 #pod
83 #pod $cmd->validate_args(\%opt, \@args);
84 #pod
85 #pod This method is passed a hashref of command line options (as processed by
86 #pod Getopt::Long::Descriptive) and an arrayref of leftover arguments. It may throw
87 #pod an exception (preferably by calling C<usage_error>) if they are invalid, or it
88 #pod may do nothing to allow processing to continue.
89 #pod
90 #pod =head2 execute
91 #pod
92 #pod Your::App::Cmd::Simple->execute(\%opt, \@args);
93 #pod
94 #pod This method does whatever it is the command should do! It is passed a hash
95 #pod reference of the parsed command-line options and an array reference of left
96 #pod over arguments.
97 #pod
98 #pod =cut
99  
100 # The idea here is that the user will someday replace "Simple" in his ISA with
101 # "Command" and then write a standard App::Cmd package. To make that possible,
102 # we produce a behind-the-scenes App::Cmd object when the user says 'use
103 # MyApp::Simple' and redirect MyApp::Simple->run to that.
104 5 my $i;
105 5 ▶BEGIN { $i = 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 5 ▶sub import {
Subroutine import: called CC=5 SCAR=16.3
108 5 my ($class) = @_;
109 5 ▶ return if $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 5 5
110  
111 # This signals that something has already set the target up.
112 5 ▶ return $class if $class->_cmd_pkg;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_cmd_pkg 0 5 5
113  
114 5 my $core_execute = App::Cmd::Command->can('execute');
115 5 my $our_execute = $class->can('execute');
116 5 ▶ Carp::confess(
Branch: 1 branch
BranchTrueFalseTotal
unless $our_execute and $our_execute != $core_execute 0 5 5
Condition33%1/3
$our_execute and $our_execute != $core_execute
!ll&&!rl&&r
005
MC/DC0%0/2
$our_execute and $our_execute != $core_execute
$our_execute $our_execute != $core_execute
Truth table33%1/3
A and B
A= $our_executeB= $our_execute != $core_execute
ABresult
0X0
100
111
117 "App::Cmd::Simple subclasses must implement ->execute, not ->run"
118 ) unless $our_execute and $our_execute != $core_execute;
119  
120 # I doubt the $i will ever be needed, but let's start paranoid.
121 5 my $generated_name = join('::', $class, '_App_Cmd', $i++);
122  
123 {
124 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 5 *{$generated_name . '::ISA'} = [ 'App::Cmd' ];
126 }
127  
128 Sub::Install::install_sub({
129 into => $class,
130 as => '_cmd_pkg',
131 0 ▶ code => sub { $generated_name },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 5 });
133  
134 Sub::Install::install_sub({
135 into => $class,
136 as => 'command_names',
137 5 ▶ code => sub { 'only' },
Subroutine __ANON__: called CC=1 SCAR=0.0
138 5 });
139  
140 Sub::Install::install_sub({
141 into => $generated_name,
142 as => '_plugins',
143 5 ▶ code => sub { $class },
Subroutine __ANON__: called CC=1 SCAR=0.0
144 5 });
145  
146 Sub::Install::install_sub({
147 into => $generated_name,
148 as => 'default_command',
149 6 ▶ code => sub { 'only' },
Subroutine __ANON__: called CC=1 SCAR=0.0
150 5 });
151  
152 Sub::Install::install_sub({
153 into => $generated_name,
154 as => '_cmd_from_args',
155 6 ▶ code => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
156 6 my ($self, $args) = @_;
157 6 ▶ if (defined(my $command = $args->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $command = $args->[0])) 5 1 6
158 5 my $plugin = $self->plugin_for($command);
159 # If help was requested, show the help for the command, not the
160 # main help. Because the main help would talk about subcommands,
161 # and a "Simple" app has no subcommands.
162 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin and grep {$plugin eq $self->plugin_for($_);} "help", "version") 2 3 5
Condition100%3/3
$plugin and grep {$plugin eq $self->plugin_for($_);} "help", "version"
!ll&&!rl&&r
212
MC/DC100%2/2
$plugin and grep {$plugin eq $self->plugin_for($_);} "help", "version"
$plugin grep {$plugin eq $self->plugin_for($_);} "help", "version"
Truth table100%3/3
A and B
A= $pluginB= grep {$plugin eq $self->plugin_for($_);} "help", "version"
ABresult
0X0
100
111
163 $plugin
164 6 and grep { $plugin eq $self->plugin_for($_) } qw(help version)
165 ) {
166 2 return ($command, [ $self->default_command ]);
167 }
168 # Any other value for "command" isn't really a command at all --
169 # it's the first argument. So call the default command instead.
170 }
171 4 return ($self->default_command, $args);
172 },
173 5 });
174  
175 {
176 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
177 5 *{ "$generated_name\::VERSION" } = \$class->VERSION;
178 }
179  
180 Sub::Install::install_sub({
181 into => $class,
182 as => 'run',
183 5 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=10.1
184 5 ▶ if ($ENV{GETOPT_LONG_DESCRIPTIVE_COMPLETION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GETOPT_LONG_DESCRIPTIVE_COMPLETION"}) 0 5 5
185 0 require Getopt::Long::Descriptive;
186 0 Getopt::Long::Descriptive::describe_options(
187 $generated_name->usage_desc,
188 $class->opt_spec,
189 );
190  
191 0 Carp::confess("this code should never be reached: still running after App::Cmd::Simple handled GETOPT_LONG_DESCRIPTIVE_COMPLETION")
192 }
193  
194 $generated_name->new({
195 5 no_help_plugin => 0,
196 no_version_plugin => 0,
197 no_commands_plugin => 1,
198 })->run(@_);
199 }
200 5 });
201  
202 5 return $class;
203 }
204  
205 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
206 4 return "%c %o"
207 }
208  
209 5 ▶sub _cmd_pkg { }
Subroutine _cmd_pkg: called CC=1 SCAR=0.0
210  
211 #pod =head1 WARNINGS
212 #pod
213 #pod B<This should be considered experimental!> Although it is probably not going
214 #pod to change much, don't build your business model around it yet, okay?
215 #pod
216 #pod App::Cmd::Simple is not rich in black magic, but it does do some somewhat
217 #pod gnarly things to make an App::Cmd::Simple look as much like an
218 #pod App::Cmd::Command as possible. This means that you can't deviate too much from
219 #pod the sort of thing shown in the synopsis as you might like. If you're doing
220 #pod something other than writing a fairly simple command, and you want to screw
221 #pod around with the App::Cmd-iness of your program, Simple might not be the best
222 #pod choice.
223 #pod
224 #pod B<One specific warning...> if you are writing a program with the
225 #pod App::Cmd::Simple class embedded in it, you B<must> call import on the class.
226 #pod That's how things work. You can just do this:
227 #pod
228 #pod YourApp::Cmd->import->run;
229 #pod
230 #pod =cut
231  
232 5 1;
233  
234 __END__
« Cmd.pm ↑ Summary Cmd/Command/help.pm »