| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 268 | ▶ | if( $prev ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 64 | ▶ | $prev->{eatall} and croak "Cannot have another argument after an eatall"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 64 | ▶ | $prev->{collect} and croak "Cannot have another argument after a collect"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 64 | ▶ | $prev->{trail} and croak "Cannot have another argument after a trail"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$arg{"eatall"} and $arg{"collect"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 268 | ▶ | keys %spec and croak "Unrecognised argument specification keys: ".join( ", ", keys %spec ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 268 | my $trail = 0; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 268 | ▶ | if( $name eq "..." ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 133 | ▶ | $name =~ s/=(.*)$// or croak "Cannot recognise $name as an option spec"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 133 | $opt{type} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 133 | ▶ | $opt{type} =~ m/^[\$\+]$/ or croak "Cannot recognise $opt{type} as an option type"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
keys %commands and $cmd ||= $cinv->pull_token
$cmd ||= $cinv->pull_token
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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless( exists $commands{$cmd} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $cinv->responderr( $command ? $command->name . " has no sub command $cmd" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $subcmd = $command->default_sub( $cinv ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if( !$subcmd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if( $cinv->peek_remaining =~ m/^-/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$optspec and exists $optspec->{$optname}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | return $cinv->responderr( "$cname: unrecognised option $optname" ); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my $optvalue; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if( $optspec->{$optname}{type} eq '$' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $optvalue = $cinv->pull_token; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | defined $optvalue or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not @argspec or $argindex >= @argspec
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
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | my $a = $argspec[$argindex]; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if( $a->{eatall} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my $a = $argspec[$argindex++]; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if( $a->{collect} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | push @args, [] unless ref $args[-1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | push @args, undef; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | push @args, \%opts if $optspec; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | push @args, $cinv; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my @response = eval { $command->invoke( @args ) }; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if( $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | $cinv->respond( "Available commands for $class:" ); | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | while( ( $cmd ||= $cinv->pull_token ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cmd ||= $cinv->pull_token
MC/DC0%0/2
$cmd ||= $cinv->pull_token
$cmd
$cinv->pull_token
Truth table0%0/3
A ||= B
A= $cmdB= $cinv->pull_token
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | unless( exists $commands{$cmd} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | $cinv->responderr( $command ? $command->name . " has no sub command $cmd" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | $cinv->respond( "/" . $command->name . " - " . $command->desc ); | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if( keys %commands ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $cinv->respond( "Usage: " . $command->name . " SUBCMD ..." ) if $command; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $name .= "+" if $a->{collect}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | $name = "[$name]" if $a->{optional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | push @table, [ " -$opt" . ( $opttype eq '$' ? " VALUE" : "" ), $desc ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $cinv->respond_table( \@table, headings => [ "Option", "Description" ] ); | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | if( my $detail = $command->detail ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||