Circle/Commandable.pm

filter: Statementstmt 63.6% 133 / 209 Branchbran 37.7% 40 / 106 Conditioncond 41.6% 10 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 9 totaltotal 53.3% 206 / 386 CC 67 SCAR 51.0
CC
67
Cov
54%
command_help
506.0
do_command
109.9
Command_arg
11.8
CRAP 164.1
« Circle/Net/IRC.pm ↑ Summary Circle/Collection.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2012 -- leonerd@leonerd.org.uk
4  
5 package Circle::Commandable;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.173320';
11  
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Attribute::Storage 0.06 qw( get_subattr get_subattrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Circle::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Circle::CommandInvocation;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶use Circle::Widget::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #############################################
22 ### Attribute handlers for command_* subs ###
23 #############################################
24  
25 334 ▶sub Command_description :ATTR(CODE)
Subroutine Command_description: called CC=1 SCAR=0.0
Pod undocumented
26 {
27 334 my $class = shift;
28 334 my ( $text ) = @_;
29  
30 334 my ( $brief, $detail ) = split( m/\n/, $text, 2 );
31  
32 334 return [ $brief, $detail ];
33 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 280 ▶sub Command_arg :ATTR(CODE,MULTI)
Subroutine Command_arg: called CC=11 SCAR=24.6
Pod undocumented
36 {
37 280 my $class = shift;
38 280 my ( $args, $name, %spec ) = @_;
39  
40 # Some things are only allowed on the last argument. Check none of these
41 # apply to the previous one
42 280 ▶ my $prev = $args ? $args->[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 64 216 280
43  
44 280 ▶ if( $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 64 216 280
45 64 ▶ $prev->{eatall} and croak "Cannot have another argument after an eatall";
Branch: 1 branch
BranchTrueFalseTotal
$prev->{"eatall"} and croak("Cannot have another argument after an eatall") 0 64 64
46 64 ▶ $prev->{collect} and croak "Cannot have another argument after a collect";
Branch: 1 branch
BranchTrueFalseTotal
$prev->{"collect"} and croak("Cannot have another argument after a collect") 0 64 64
47 64 ▶ $prev->{trail} and croak "Cannot have another argument after a trail";
Branch: 1 branch
BranchTrueFalseTotal
$prev->{"trail"} and croak("Cannot have another argument after a trail") 0 64 64
48 }
49  
50 280 my $optional = $name =~ s/\?$//; # No error if this is missing
51  
52 my %arg = (
53 name => uc $name,
54 optional => $optional,
55 eatall => delete $spec{eatall}, # This argument consumes all the remaining text in one string
56 collect => delete $spec{collect}, # This argument collects all the non-option tokens in an ARRAY ref
57 280 );
58  
59 280 ▶ $arg{eatall} and $arg{collect} and croak "Cannot eatall and collect";
Branch: 1 branch
BranchTrueFalseTotal
$arg{"eatall"} and $arg{"collect"} and croak("Cannot eatall and collect") 0 280 280
Condition66%2/3
$arg{"eatall"} and $arg{"collect"}
!ll&&!rl&&r
212680
MC/DC0%0/2
$arg{"eatall"} and $arg{"collect"}
$arg{"eatall"} $arg{"collect"}
Truth table66%2/3
A and B
A= $arg{"eatall"}B= $arg{"collect"}
ABresult
0X0
100
111
60  
61 280 ▶ keys %spec and croak "Unrecognised argument specification keys: ".join( ", ", keys %spec );
Branch: 1 branch
BranchTrueFalseTotal
keys %spec and croak('Unrecognised argument specification keys: ' . join(", ", keys %spec)) 0 280 280
62  
63 280 my $trail = 0;
64 280 ▶ if( $name eq "..." ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '...') { } 4 276 280
65 4 $arg{trail} = 1;
66 }
67 else {
68 276 ▶ $name =~ m/\W/ and croak "Cannot use $name as an argument name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\W/ and croak("Cannot use $name as an argument name") 0 276 276
69 }
70  
71 280 push @$args, \%arg;
72  
73 280 return $args;
74 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 138 ▶sub Command_opt :ATTR(CODE,MULTI)
Subroutine Command_opt: called CC=4 SCAR=14.2
Pod undocumented
77 {
78 138 my $class = shift;
79 138 my ( $opts, $name, %spec ) = @_;
80  
81 my %opt = (
82 desc => delete $spec{desc},
83 138 );
84  
85 138 ▶ keys %spec and croak "Unrecognised option specification keys: ".join( ", ", keys %spec );
Branch: 1 branch
BranchTrueFalseTotal
keys %spec and croak('Unrecognised option specification keys: ' . join(", ", keys %spec)) 0 138 138
86  
87 138 ▶ $name =~ s/=(.*)$// or croak "Cannot recognise $name as an option spec";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/=(.*)$// or croak("Cannot recognise $name as an option spec") 138 0 138
88 138 $opt{type} = $1;
89  
90 138 ▶ $opt{type} =~ m/^[\$\+]$/ or croak "Cannot recognise $opt{type} as an option type";
Branch: 1 branch
BranchTrueFalseTotal
$opt{"type"} =~ /^[\$\+]$/ or croak(qq[Cannot recognise $opt{"type"} as an option type]) 138 0 138
91  
92 138 $opts->{$name} = \%opt;
93  
94 138 return $opts;
95 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 122 ▶sub Command_subof :ATTR(CODE)
Subroutine Command_subof: called CC=1 SCAR=0.0
Pod undocumented
98 {
99 122 my $class = shift;
100 122 my ( $parent ) = @_;
101  
102 122 return $parent;
103 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 32 ▶sub Command_default :ATTR(CODE)
Subroutine Command_default: called CC=1 SCAR=0.0
Pod undocumented
106 {
107 32 return 1; # Just a boolean
108 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 sub do_command
111 5 ▶{
Subroutine do_command: called CC=30 SCAR=47.0
Pod undocumented
112 5 my $self = shift;
113 5 my ( $cinv ) = @_;
114  
115 5 my $cmd = $cinv->pull_token;
116  
117 5 my $command = undef;
118 5 my %commands = Circle::Command->root_commands( $cinv );
119  
120 5 ▶ while( keys %commands and $cmd ||= $cinv->pull_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %commands and $cmd ||= $cinv->pull_token) 7 5 12
Condition66%4/6
keys %commands and $cmd ||= $cinv->pull_token
!ll&&!rl&&r
507
$cmd ||= $cinv->pull_token
l!l&&r!l&&!r
520
MC/DC0%0/3
keys %commands and $cmd ||= $cinv->pull_token
keys %commands $cmd $cinv->pull_token
Truth table0%0/4
A and B ||= C
A= keys %commandsB= $cmdC= $cinv->pull_token
ABCresult
0XX0
1000
11X1
1011
121 7 ▶ unless( exists $commands{$cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{$cmd}) 0 7 7
122 0 ▶ $cinv->responderr( $command ? $command->name . " has no sub command $cmd"
Branch: 1 branch
BranchTrueFalseTotal
$command ? : 0 0 0
123 : "No such command $cmd" );
124 0 return;
125 }
126  
127 7 $command = $commands{$cmd};
128 7 %commands = $command->sub_commands( $cinv );
129  
130 7 undef $cmd;
131 }
132  
133 5 ▶ while( keys %commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %commands) 0 5 5
134 0 my $subcmd = $command->default_sub( $cinv );
135  
136 0 ▶ if( !$subcmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcmd) 0 0 0
137 # No default subcommand - issue help on $command instead
138 0 my $helpinv = $cinv->nest( "help " . $command->name );
139 0 return $self->do_command( $helpinv );
140 }
141  
142 0 $command = $subcmd;
143 0 %commands = $command->sub_commands( $cinv );
144 }
145  
146 5 my $cname = $command->name;
147  
148 5 my @args;
149 my %opts;
150  
151 5 my @argspec = $command->args;
152 5 my $optspec = $command->opts;
153  
154 5 my $argindex = 0;
155  
156 5 my $no_more_opts;
157 5 ▶ while( length $cinv->peek_remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cinv->peek_remaining) 8 3 11
158 8 ▶ if( $cinv->peek_token eq "--" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cinv->peek_token eq "--") 0 8 8
159 0 $cinv->pull_token;
160 0 $no_more_opts++;
161 0 next;
162 }
163  
164 8 ▶ if( !$no_more_opts and $cinv->peek_remaining =~ m/^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_more_opts and $cinv->peek_remaining =~ /^-/) { } 2 6 8
Condition66%2/3
not $no_more_opts and $cinv->peek_remaining =~ /^-/
!ll&&!rl&&r
062
MC/DC50%1/2
not $no_more_opts and $cinv->peek_remaining =~ /^-/
not $no_more_opts $cinv->peek_remaining =~ /^-/
Truth table66%2/3
not(A) and B
A= not $no_more_optsB= $cinv->peek_remaining =~ /^-/
ABresult
0X0
100
111
165 # An option
166 2 my $optname = $cinv->pull_token;
167 2 $optname =~ s/^-//;
168  
169 2 ▶ $optspec and exists $optspec->{$optname} or
Branch: 1 branch
BranchTrueFalseTotal
$optspec and exists $optspec->{$optname} or return $cinv->responderr("${cname}: unrecognised option $optname") 2 0 2
Condition33%1/3
$optspec and exists $optspec->{$optname}
!ll&&!rl&&r
002
MC/DC0%0/2
$optspec and exists $optspec->{$optname}
$optspec exists $optspec->{$optname}
Truth table33%1/3
A and B
A= $optspecB= exists $optspec->{$optname}
ABresult
0X0
100
111
170 return $cinv->responderr( "$cname: unrecognised option $optname" );
171  
172 2 my $optvalue;
173  
174 2 ▶ if( $optspec->{$optname}{type} eq '$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optspec->{$optname}{'type'} eq '$') { } 2 0 2
175 2 $optvalue = $cinv->pull_token;
176 2 ▶ defined $optvalue or
Branch: 1 branch
BranchTrueFalseTotal
defined $optvalue or return $cinv->responderr("${cname}: option $optname require a value") 2 0 2
177 return $cinv->responderr( "$cname: option $optname require a value" );
178 }
179 else {
180 0 $optvalue = 1;
181 }
182  
183 2 $opts{$optname} = $optvalue;
184 }
185 else {
186 6 ▶ return $cinv->responderr( "$cname: Too many arguments" ) if !@argspec or $argindex >= @argspec;
Branch: 1 branch
BranchTrueFalseTotal
if not @argspec or $argindex >= @argspec 0 6 6
Condition33%1/3
not @argspec or $argindex >= @argspec
l!l&&r!l&&!r
006
MC/DC0%0/2
not @argspec or $argindex >= @argspec
not @argspec $argindex >= @argspec
Truth table33%1/3
not(A) or B
A= not @argspecB= $argindex >= @argspec
ABresult
1X1
011
000
187  
188 6 my $a = $argspec[$argindex];
189  
190 6 ▶ if( $a->{eatall} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($a->{'eatall'}) { } 2 4 6
elsif ($a->{'collect'}) { } 0 4 4
elsif ($a->{'trail'}) { } 0 4 4
191 2 push @args, $cinv->peek_remaining;
192 2 $argindex++;
193 2 last;
194 }
195 elsif( $a->{collect} ) {
196 # If this is the first one, $args[-1] won't be an ARRAY ref
197 0 ▶ push @args, [] unless ref $args[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[-1] 0 0 0
198 0 push @{ $args[-1] }, $cinv->pull_token;
199 }
200 elsif( $a->{trail} ) {
201 0 last;
202 }
203 else {
204 4 push @args, $cinv->pull_token;
205 4 $argindex++;
206 }
207 }
208 }
209  
210 5 ▶ while( $argindex < @argspec ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argindex < @argspec) 0 5 5
211 0 my $a = $argspec[$argindex++];
212  
213 0 ▶ if( $a->{collect} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a->{'collect'}) { } 0 0 0
elsif ($a->{'trail'}) { } 0 0 0
214 0 ▶ push @args, [] unless ref $args[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[-1] 0 0 0
215 0 last;
216 }
217 elsif( $a->{trail} ) {
218 0 last;
219 }
220  
221 $a->{optional} or
222 0 ▶ return $cinv->responderr( "$cname: expected $a->{name}" );
Branch: 1 branch
BranchTrueFalseTotal
$a->{"optional"} or return $cinv->responderr(qq[${cname}: expected $a->{"name"}]) 0 0 0
223  
224 0 push @args, undef;
225 }
226  
227 5 ▶ push @args, \%opts if $optspec;
Branch: 1 branch
BranchTrueFalseTotal
if $optspec 2 3 5
228  
229 5 push @args, $cinv;
230  
231 5 my @response = eval { $command->invoke( @args ) };
232 5 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 5 5
233 0 my $text = $@; chomp $text;
234 0 $cinv->responderr( $text );
235 }
236 else {
237 5 $cinv->respond( $_ ) foreach @response;
238 }
239 }
240  
241 sub command_help
242 0 ▶ : Command_description("Display help on a command")
Subroutine command_help: not called CC=22 SCAR=62.3
Pod undocumented
243 : Command_arg('command?')
244 : Command_arg('...')
245 {
246 0 my $self = shift;
247 0 my ( $cmd, $cinv ) = @_;
248  
249 0 my $command = undef;
250 0 my %commands = Circle::Command->root_commands( $cinv );
251  
252 0 ▶ if( !defined $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmd) 0 0 0
253 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
254 0 $cinv->respond( "Available commands for $class:" );
255 }
256  
257 0 ▶ while( ( $cmd ||= $cinv->pull_token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd ||= $cinv->pull_token) 0 0 0
Condition0%0/3
$cmd ||= $cinv->pull_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd ||= $cinv->pull_token
$cmd $cinv->pull_token
Truth table0%0/3
A ||= B
A= $cmdB= $cinv->pull_token
ABresult
1X1
011
000
258 0 ▶ unless( exists $commands{$cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{$cmd}) 0 0 0
259 0 ▶ $cinv->responderr( $command ? $command->name . " has no sub command $cmd"
Branch: 1 branch
BranchTrueFalseTotal
$command ? : 0 0 0
260 : "No such command $cmd" );
261 0 return;
262 }
263  
264 0 $command = $commands{$cmd};
265 0 %commands = $command->sub_commands( $cinv );
266  
267 0 undef $cmd;
268 }
269  
270 0 ▶ if( $command ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) 0 0 0
271 0 $cinv->respond( "/" . $command->name . " - " . $command->desc );
272 }
273  
274 0 ▶ if( keys %commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %commands) 0 0 0
275 0 ▶ $cinv->respond( "Usage: " . $command->name . " SUBCMD ..." ) if $command;
Branch: 1 branch
BranchTrueFalseTotal
if $command 0 0 0
276  
277 0 my @table;
278 0 foreach my $sub ( map { $commands{$_} } sort keys %commands ) {
279 0 my $subname;
280 # bold function name if it's default
281 0 ▶ if( $sub->is_default ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->is_default) { } 0 0 0
282 0 $subname = Circle::TaggedString->new( " /" . $sub->name );
283 0 $subname->apply_tag( 0, $subname->length, b => 1 );
284 }
285 else {
286 0 $subname = " /" . $sub->name;
287 }
288  
289 0 push @table, [ $subname, $sub->desc ];
290 }
291  
292 0 $cinv->respond_table( \@table, colsep => " - ", headings => [ "Command", "Description" ] );
293  
294 0 return;
295 }
296  
297 0 my @argdesc;
298 0 foreach my $a ( $command->args ) {
299 0 my $name = $a->{name};
300 0 ▶ $name .= "..." if $a->{eatall};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"eatall"} 0 0 0
301 0 ▶ $name .= "+" if $a->{collect};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"collect"} 0 0 0
302 0 ▶ $name = "[$name]" if $a->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"optional"} 0 0 0
303 0 push @argdesc, $name;
304 }
305  
306 0 $cinv->respond( "Usage: " . join( " ", $command->name, @argdesc ) );
307  
308 0 ▶ if( my $opts = $command->opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = $command->opts) 0 0 0
309 0 $cinv->respond( "Options:" );
310  
311 0 my @table;
312  
313 0 foreach my $opt ( sort keys %$opts ) {
314 0 my $opttype = $opts->{$opt}{type};
315 0 ▶ my $desc = defined $opts->{$opt}{desc} ? $opts->{$opt}{desc} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{$opt}{'desc'} ? : 0 0 0
316  
317 0 ▶ push @table, [ " -$opt" . ( $opttype eq '$' ? " VALUE" : "" ), $desc ];
Branch: 1 branch
BranchTrueFalseTotal
$opttype eq '$' ? : 0 0 0
318 }
319  
320 0 $cinv->respond_table( \@table, headings => [ "Option", "Description" ] );
321 }
322  
323 0 ▶ if( my $detail = $command->detail ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $detail = $command->detail) 0 0 0
324 0 $cinv->respond( "" );
325 0 $cinv->respond( $_ ) for split( m/\n/, $detail );
326 }
327  
328 0 return;
329 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
330  
331 sub method_do_command
332 4 ▶{
Subroutine method_do_command: called CC=1 SCAR=0.0
Pod undocumented
333 4 my $self = shift;
334 4 my ( $ctx, $command ) = @_;
335  
336 4 my $cinv = Circle::CommandInvocation->new( $command, $ctx->stream, $self );
337 4 $self->do_command( $cinv );
338 }
339  
340 ###
341 # Widget
342 ###
343  
344 sub get_widget_commandentry
345 2 ▶{
Subroutine get_widget_commandentry: called CC=2 SCAR=7.0
Pod undocumented
346 2 my $self = shift;
347  
348 2 ▶ return $self->{widget_commandentry} if defined $self->{widget_commandentry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"widget_commandentry"} 0 2 2
349  
350 2 my $registry = $self->{registry};
351  
352 my $widget = $registry->construct(
353 "Circle::Widget::Entry",
354 autoclear => 1,
355 focussed => 1,
356 history => 100, # TODO
357 2 ▶ on_enter => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
358 2 my ( $text, $ctx ) = @_;
359  
360 2 ▶ if( $text =~ m{^/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ m[^/]) { } 1 1 2
elsif ($self->can('enter_text')) { } 1 0 1
361 1 substr( $text, 0, 1 ) = "";
362  
363 1 my $cinv = Circle::CommandInvocation->new( $text, $ctx->stream, $self );
364 1 $self->do_command( $cinv );
365 }
366 elsif( $self->can( "enter_text" ) ) {
367 1 $self->enter_text( $text );
368 }
369 else {
370 0 $self->responderr( "Cannot enter raw text here" );
371 }
372 },
373 2 );
374  
375 2 return $self->{widget_commandentry} = $widget;
376 }
377  
378 4 0x55AA;
« Circle/Net/IRC.pm ↑ Summary Circle/Collection.pm »