blib/lib/Circle/Commandable.pm

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