Commandable/Finder.pm

filter: Statementstmt 95.9% 164 / 171 Branchbran 78.1% 86 / 110 Conditioncond 76.9% 20 / 26 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 95.0% 19 / 20 Podpod 63.6% 7 / 11 totaltotal 86.1% 305 / 354 CC 74 SCAR 28.1
CC
74
Cov
88%
parse_invocation_options
36.9
builtin_command_helpcmd
9.9
find_and_invoke
8.2
CRAP 16.7
« Commandable/Finder/SubAttributes.pm ↑ Summary Commandable/Finder/Packages.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2021-2024 -- leonerd@leonerd.org.uk
5  
6 package Commandable::Finder 0.14;
7  
8 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 require Commandable::Output;
16  
17 =head1 NAME
18  
19 C<Commandable::Finder> - an interface for discovery of L<Commandable::Command>s
20  
21 =head1 SYNOPSIS
22  
23 use Commandable::Finder::...;
24  
25 my $finder = Commandable::Finder::...->new(
26 ...
27 );
28  
29 $finder->find_and_invoke( Commandable::Invocation->new( $text ) );
30  
31 =head1 DESCRIPTION
32  
33 This base class is common to the various finder subclasses:
34  
35 =over 4
36  
37 =item *
38  
39 L<Commandable::Finder::SubAttributes>
40  
41 =item *
42  
43 L<Commandable::Finder::MethodAttributes>
44  
45 =item *
46  
47 L<Commandable::Finder::Packages>
48  
49 =back
50  
51 =head1 METHODS
52  
53 =cut
54  
55 =head2 configure
56  
57 $finder = $finder->configure( %conf );
58  
59 Sets configuration options on the finder instance. Returns the finder instance
60 itself, to permit easy chaining.
61  
62 The following configuration options are recognised:
63  
64 =head3 allow_multiple_commands
65  
66 If enabled, the L</find_and_invoke> method will permit multiple command
67 invocations within a single call.
68  
69 =head3 require_order
70  
71 If enabled, stop processing options when the first non-option argument
72 is seen.
73  
74 =head3 bundling
75  
76 If enabled, short (single-letter) options of simple boolean type can be
77 combined into a single C<-abc...> argument. Incrementable options can be
78 specified multiple times (as common with things like C<-vvv> for
79 C<--verbose 3>).
80  
81 =cut
82  
83 4 ▶sub configure ( $self, %conf )
Subroutine configure: called CC=4 SCAR=14.0
Pod documented
84 {
85 exists $conf{$_} and $self->{config}{$_} = delete $conf{$_}
86 4 ▶ for qw( allow_multiple_commands require_order bundling );
Branch: 1 branch
BranchTrueFalseTotal
exists $conf{$_} and $self->{"config"}{$_} = delete $conf{$_} 5 7 12
87  
88 4 ▶ keys %conf and croak "Unrecognised ->configure params: " . join( ", ", sort keys %conf );
Branch: 1 branch
BranchTrueFalseTotal
keys %conf and croak('Unrecognised ->configure params: ' . join(", ", sort(keys %conf))) 0 4 4
89  
90 4 return $self;
91 }
92  
93 =head2 add_global_options
94  
95 $finder->add_global_options( @optspecs );
96  
97 I<Since version 0.13.>
98  
99 Adds additional global options to the stored set.
100  
101 Each is specified as a HASH reference containing keys to specify one option,
102 in the same style as the per-command options used by
103 L<Commandable::Finder::Packages>.
104  
105 In addition, each should also provide a key named C<into>, whose value should
106 be a SCALAR or CODE reference to be used for applying the value for the option
107 when it is parsed. SCALAR references will be assigned to directly; CODE
108 references will be invoked with the option's name and value as positional
109 arguments:
110  
111 $$into = $value;
112 $into->( $name, $value );
113  
114 This style permits a relatively easy upgrade from such modules as
115 L<Getopt::Long>, to handle global options.
116  
117 GetOptions(
118 'verbose|v+' => \my $VERBOSE,
119 'silent|s' => \my $SILENT,
120 ) or exit 1;
121  
122 Can now become
123  
124 $finder->add_global_options(
125 { name => "verbose|v", mode => "inc", into => \my $VERBOSE,
126 description => "Increase verbosity of output" },
127 { name => "silent|s", into => \my $SILENT,
128 description => "Silence output entirely" },
129 );
130  
131 with the added benefit of automated integration with the global C<help>
132 command, more consistent option parsing along with other command handling, and
133 so on.
134  
135 =cut
136  
137 2 ▶sub add_global_options ( $self, @optspecs )
Subroutine add_global_options: called CC=5 SCAR=16.3
Pod documented
138 {
139 2 foreach my $optspec ( @optspecs ) {
140 5 my $into = $optspec->{into};
141 5 my $opt = Commandable::Command::_Option->new( %$optspec );
142  
143 5 my $name = $opt->name;
144 5 ▶ defined $into or
Branch: 1 branch
BranchTrueFalseTotal
defined $into or croak("Global option $name requires an 'into'") 5 0 5
145 croak "Global option $name requires an 'into'";
146 5 ▶ ( ref $into ) =~ m/^(?:SCALAR|CODE)$/ or
Branch: 1 branch
BranchTrueFalseTotal
ref($into) =~ /^(?:SCALAR|CODE)$/u or croak("Global option $name 'into' must be a SCALAR or CODE reference; got ") 5 0 5
147 croak "Global option $name 'into' must be a SCALAR or CODE reference; got ";
148  
149 5 $self->{global_options}{ $_ } = $opt for $opt->names;
150 5 $self->{global_options_into}{ $opt->keyname } = $into;
151 }
152  
153 2 return $self;
154 }
155  
156 =head2 handle_global_options
157  
158 $finder->handle_global_options( $cinv );
159  
160 I<Since version 0.13.>
161  
162 Extracts global options from the command invocation and process them into the
163 C<into> references previously supplied.
164  
165 Normally it would not be necessary to invoke this directly, because the main
166 L</find_and_invoke> method does this anyway. It is provided in case the
167 implementing program performs its own command handling or changes the logic in
168 some other way.
169  
170 =cut
171  
172 6 ▶sub handle_global_options ( $self, $cinv )
Subroutine handle_global_options: called CC=4 SCAR=13.9
Pod documented
173 {
174 my $global_optspecs = $self->{global_options}
175 6 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $global_optspecs = $self->{"global_options"} or return 6 0 6
176  
177 6 my $opts = $self->parse_invocation_options( $cinv, $global_optspecs, passthrough => 1 );
178  
179 6 foreach ( keys %$opts ) {
180 10 my $value = $opts->{$_};
181 10 my $into = $self->{global_options_into}{$_};
182 10 ▶ if( ref $into eq "SCALAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $into eq 'SCALAR') { } 7 3 10
183 7 $into->$* = $value;
184 }
185 else {
186 3 $into->( $_, $value );
187 }
188 }
189 }
190  
191 =head2 find_commands
192  
193 @commands = $finder->find_commands;
194  
195 Returns a list of command instances, in no particular order. Each will be an
196 instance of L<Commandable::Command>.
197  
198 =head2 find_command
199  
200 $command = $finder->find_command( $cmdname );
201  
202 Returns a command instance of the given name as an instance of
203 L<Commandable::Command>, or C<undef> if there is none.
204  
205 =cut
206  
207 =head2 parse_invocation
208  
209 @vals = $finder->parse_invocation( $command, $cinv );
210  
211 I<Since version 0.12.>
212  
213 Parses values out of a L<Commandable::Invocation> instance according to the
214 specification for the command's arguments. Returns a list of perl values
215 suitable to pass into the function implementing the command.
216  
217 This method will throw an exception if mandatory arguments are missing.
218  
219 =cut
220  
221 38 ▶sub parse_invocation ( $self, $command, $cinv )
Subroutine parse_invocation: called CC=7 SCAR=19.5
Pod documented
222 {
223 38 my @args;
224  
225 38 ▶ if( my %optspec = $command->options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%optspec) = $command->options) 30 8 38
226 30 push @args, $self->parse_invocation_options( $cinv, \%optspec );
227 }
228  
229 34 foreach my $argspec ( $command->arguments ) {
230 16 my $val = $cinv->pull_token;
231 16 ▶ if( defined $val ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 10 6 16
elsif (not $argspec->optional) { } 1 5 6
232 10 ▶ if( $argspec->slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->slurpy) 4 6 10
233 4 my @vals = ( $val );
234 4 ▶ while( defined( $val = $cinv->pull_token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($val = $cinv->pull_token)) 6 4 10
235 6 push @vals, $val;
236 }
237 4 $val = \@vals;
238 }
239 10 push @args, $val;
240 }
241 elsif( !$argspec->optional ) {
242 1 die "Expected a value for '".$argspec->name."' argument\n";
243 }
244 else {
245 # optional argument was missing; this is the end of the args
246 5 last;
247 }
248 }
249  
250 33 return @args;
251 }
252  
253 36 ▶sub parse_invocation_options ( $self, $cinv, $optspec, %params )
Subroutine parse_invocation_options: called CC=33 SCAR=36.1
Pod undocumented
254 {
255 36 my $passthrough = $params{passthrough};
256  
257 36 my $opts = {};
258 36 my @remaining;
259  
260 36 ▶ while( defined( my $token = $cinv->pull_token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = $cinv->pull_token)) 47 31 78
261 49 ▶ if( $token eq "--" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "--") 0 49 49
262 0 ▶ push @remaining, $token if $passthrough;
Branch: 1 branch
BranchTrueFalseTotal
if $passthrough 0 0 0
263 0 last;
264 }
265  
266 49 my $spec;
267 my $value_in_token;
268 49 my $token_again;
269  
270 49 my $value = 1;
271 49 my $orig = $token;
272  
273 49 ▶ if( $token =~ s/^--([^=]+)(=|$)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ s/^--([^=]+)(=|$)//u) { } 21 28 49
elsif ($token =~ s/^-(.)//u) { } 13 15 28
274 21 my ( $opt, $equal ) = ($1, $2);
275 21 ▶ if( !$optspec->{$opt} and $opt =~ /no-(.+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $optspec->{$opt} and $opt =~ /no-(.+)/u) { } 1 20 21
elsif ($spec = $optspec->{$opt}) { } 19 1 20
Condition100%3/3
not $optspec->{$opt} and $opt =~ /no-(.+)/u
!ll&&!rl&&r
1911
MC/DC100%2/2
not $optspec->{$opt} and $opt =~ /no-(.+)/u
not $optspec->{$opt} $opt =~ /no-(.+)/u
Truth table100%3/3
not(A) and B
A= not $optspec->{$opt}B= $opt =~ /no-(.+)/u
ABresult
0X0
100
111
276 1 ▶ $spec = $optspec->{$1} and $spec->negatable
Branch: 1 branch
BranchTrueFalseTotal
$spec = $optspec->{$1} and $spec->negatable or die "Unrecognised option name --$opt\n" 1 0 1
Condition33%1/3
$spec = $optspec->{$1} and $spec->negatable
!ll&&!rl&&r
001
MC/DC0%0/2
$spec = $optspec->{$1} and $spec->negatable
$spec = $optspec->{$1} $spec->negatable
Truth table33%1/3
A and B
A= $spec = $optspec->{$1}B= $spec->negatable
ABresult
0X0
100
111
277 or die "Unrecognised option name --$opt\n";
278 1 $value = undef;
279 }
280 elsif( $spec = $optspec->{$opt} ) {
281 19 $value_in_token = length $equal;
282 }
283 else {
284 1 ▶ die "Unrecognised option name --$opt\n" unless $passthrough;
Branch: 1 branch
BranchTrueFalseTotal
unless $passthrough 0 1 1
285 1 push @remaining, $orig;
286 1 next;
287 }
288 }
289 elsif( $token =~ s/^-(.)// ) {
290 13 ▶ unless( $spec = $optspec->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec = $optspec->{$1}) 0 13 13
291 0 ▶ die "Unrecognised option name -$1\n" unless $passthrough;
Branch: 1 branch
BranchTrueFalseTotal
unless $passthrough 0 0 0
292 0 push @remaining, $orig;
293 0 next;
294 }
295 13 ▶ if( $spec->mode_expects_value ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->mode_expects_value) { } 4 9 13
elsif ($self->{'config'}{'bundling'} and length $token and length $1 == 1) { } 2 14 16
296 4 $value_in_token = length $token;
297 }
298 ▶ elsif( $self->{config}{bundling} and length $token and length($1) == 1 ) {
Condition83%5/6
$self->{"config"}{"bundling"} and length $token and length $1 == 1
!ll&&!rl&&r
702
$self->{"config"}{"bundling"} and length $token
!ll&&!rl&&r
612
MC/DC66%2/3
$self->{"config"}{"bundling"} and length $token and length $1 == 1
$self->{"config"}{"bundling"} length $token length $1 == 1
Truth table75%3/4
A and B and C
A= $self->{"config"}{"bundling"}B= length $tokenC= length $1 == 1
ABCresult
0XX0
10X0
1100
1111
299 2 $token_again = "-$token";
300 2 undef $token;
301 }
302 }
303 else {
304 15 push @remaining, $token;
305 15 ▶ if( $self->{config}{require_order} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'require_order'}) { } 1 14 15
306 1 last;
307 }
308 else {
309 14 next;
310 }
311 }
312  
313 33 my $name = $spec->name;
314  
315 33 ▶ if( $spec->mode_expects_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->mode_expects_value) { } 19 14 33
316 19 ▶ $value = $value_in_token ? $token
Branch: 1 branch
BranchTrueFalseTotal
$value_in_token ? : 14 5 19
Condition50%1/2
$cinv->pull_token // die("Expected value for option --$name\n")
l!l
50
MC/DC0%0/1
$cinv->pull_token // die("Expected value for option --$name\n")
$cinv->pull_token
Truth table50%1/2
A
A= $cinv->pull_token
Aresult
11
00
317 : ( $cinv->pull_token // die "Expected value for option --$name\n" );
318 }
319 else {
320 14 ▶ die "Unexpected value for parameter $name\n" if $value_in_token or length $token;
Branch: 1 branch
BranchTrueFalseTotal
if $value_in_token or length $token 1 13 14
Condition66%2/3
$value_in_token or length $token
l!l&&r!l&&!r
0113
MC/DC50%1/2
$value_in_token or length $token
$value_in_token length $token
Truth table66%2/3
A or B
A= $value_in_tokenB= length $token
ABresult
1X1
011
000
321 }
322  
323 32 ▶ if( defined( my $matches = $spec->matches ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $matches = $spec->matches)) 6 26 32
324 6 ▶ $value =~ $matches or
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /$matches/u or die "Value for --$name option must " . $spec->match_msg . "\n" 3 3 6
325 die "Value for --$name option must " . $spec->match_msg . "\n";
326 }
327  
328 29 my $keyname = $spec->keyname;
329  
330 29 ▶ if( $spec->mode eq "multi_value" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spec->mode eq 'multi_value') { } 5 24 29
elsif ($spec->mode eq 'inc') { } 8 16 24
elsif ($spec->mode eq 'bool') { } 1 15 16
331 5 push $opts->{$keyname}->@*, $value;
332 }
333 elsif( $spec->mode eq "inc" ) {
334 8 $opts->{$keyname}++;
335 }
336 elsif( $spec->mode eq "bool" ) {
337 1 $opts->{$keyname} = !!$value;
338 }
339 else {
340 15 $opts->{$keyname} = $value;
341 }
342  
343 29 ▶ $token = $token_again, redo if defined $token_again;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token_again 2 27 29
344 }
345  
346 32 $cinv->putback_tokens( @remaining );
347  
348 32 foreach my $spec ( values %$optspec ) {
349 196 my $keyname = $spec->keyname;
350 $opts->{$keyname} = $spec->default if
351 196 ▶ defined $spec->default and !exists $opts->{$keyname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->default and not exists $opts->{$keyname} 7 189 196
Condition100%3/3
defined $spec->default and not exists $opts->{$keyname}
!ll&&!rl&&r
18727
MC/DC100%2/2
defined $spec->default and not exists $opts->{$keyname}
defined $spec->default not exists $opts->{$keyname}
Truth table100%3/3
A and not(B)
A= defined $spec->defaultB= not exists $opts->{$keyname}
ABresult
0X0
100
111
352 }
353  
354 32 return $opts;
355 }
356  
357 =head2 find_and_invoke
358  
359 $result = $finder->find_and_invoke( $cinv );
360  
361 A convenient wrapper around the common steps of finding a command named after
362 the initial token in a L<Commandable::Invocation>, parsing arguments from it,
363 and invoking the underlying implementation function.
364  
365 If the C<allow_multiple_commands> configuration option is set, it will
366 repeatedly attempt to parse a command name followed by arguments and options
367 while the invocation string is non-empty.
368  
369 =cut
370  
371 14 ▶sub find_and_invoke ( $self, $cinv )
Subroutine find_and_invoke: called CC=8 SCAR=21.1
Pod documented
372 {
373 14 my $multiple = $self->{config}{allow_multiple_commands};
374  
375 # global options come first
376 $self->handle_global_options( $cinv )
377 14 ▶ if $self->{global_options};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"global_options"} 6 8 14
378  
379 14 my $result;
380 {
381 14 ▶ defined( my $cmdname = $cinv->pull_token ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $cmdname = $cinv->pull_token) or die "Expected a command name\n" 15 0 15
382 die "Expected a command name\n";
383  
384 15 ▶ my $cmd = $self->find_command( $cmdname ) or
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = $self->find_command($cmdname) or die "Unrecognised command '${cmdname}'" 15 0 15
385 die "Unrecognised command '$cmdname'";
386  
387 15 my @args = $self->parse_invocation( $cmd, $cinv );
388  
389 15 ▶ !$multiple and length $cinv->peek_remaining and
Branch: 1 branch
BranchTrueFalseTotal
not $multiple and length $cinv->peek_remaining and die 'Unrecognised extra input: ' . $cinv->peek_remaining . "\n" 0 15 15
Condition66%2/3
not $multiple and length $cinv->peek_remaining
!ll&&!rl&&r
870
MC/DC0%0/2
not $multiple and length $cinv->peek_remaining
not $multiple length $cinv->peek_remaining
Truth table66%2/3
not(A) and B
A= not $multipleB= length $cinv->peek_remaining
ABresult
0X0
100
111
390 die "Unrecognised extra input: " . $cinv->peek_remaining . "\n";
391  
392 15 $result = $cmd->code->( @args );
393  
394 # TODO configurable separator - ';' or '|' or whatever
395 # currently blank
396  
397 15 ▶ redo if $multiple and length $cinv->peek_remaining;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple and length $cinv->peek_remaining 1 14 15
Condition100%3/3
$multiple and length $cinv->peek_remaining
!ll&&!rl&&r
771
MC/DC100%2/2
$multiple and length $cinv->peek_remaining
$multiple length $cinv->peek_remaining
Truth table100%3/3
A and B
A= $multipleB= length $cinv->peek_remaining
ABresult
0X0
100
111
398 }
399  
400 14 return $result;
401 }
402  
403 =head2 find_and_invoke_list
404  
405 $result = $finder->find_and_invoke_list( @tokens );
406  
407 A further convenience around creating a L<Commandable::Invocation> from the
408 given list of values and using that to invoke a command.
409  
410 =cut
411  
412 12 ▶sub find_and_invoke_list ( $self, @args )
Subroutine find_and_invoke_list: called CC=1 SCAR=0.0
Pod documented
413 {
414 12 require Commandable::Invocation;
415 12 return $self->find_and_invoke( Commandable::Invocation->new_from_tokens( @args ) );
416 }
417  
418 =head2 find_and_invoke_ARGV
419  
420 $result = $finder->find_and_invoke_ARGV();
421  
422 A further convenience around creating a L<Commandable::Invocation> from the
423 C<@ARGV> array and using that to invoke a command. Often this allows an entire
424 wrapper script to be created in a single line of code:
425  
426 exit Commandable::Finder::SOMESUBCLASS->new( ... )
427 ->find_and_invoke_ARGV();
428  
429 =cut
430  
431 0 ▶sub find_and_invoke_ARGV ( $self )
Subroutine find_and_invoke_ARGV: not called CC=1 SCAR=6.9
Pod documented
432 {
433 0 $self->find_and_invoke_list( @ARGV );
434 }
435  
436 =head1 BUILTIN COMMANDS
437  
438 The following built-in commands are automatically provided.
439  
440 =cut
441  
442 10 ▶sub add_builtin_commands ( $self, $commands )
Subroutine add_builtin_commands: called CC=1 SCAR=0.0
Pod undocumented
443 {
444 $commands->{help} =
445 Commandable::Command->new(
446 name => "help",
447 description => "Display a list of available commands",
448 arguments => [
449 Commandable::Command::_Argument->new(
450 name => "cmd",
451 description => "command name",
452 optional => 1,
453 )
454 ],
455 2 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
456 2 ▶ @_ ? return $self->builtin_command_helpcmd( @_ )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
457 : return $self->builtin_command_helpsummary;
458 },
459 10 );
460 }
461  
462 # TODO: some pretty output formatting maybe using S:T:Terminal?
463 2 ▶sub _print_table2 ( $sep, @rows )
Subroutine _print_table2: called CC=2 SCAR=6.9
464 {
465 2 my $max_len = max map { length $_->[0] } @rows;
466  
467 Commandable::Output->printf( "%-*s%s%s\n",
468 $max_len, $_->[0], $sep, $_->[1]
469 2 ) for @rows;
470 }
471  
472 # A join() that respects stringify overloading
473 sub _join
474 5 ▶{
Subroutine _join: called CC=2 SCAR=6.9
475 5 my $sep = shift;
476 5 my $ret = shift;
477 5 $ret .= "$sep$_" for @_;
478 5 return $ret;
479 }
480  
481 =head2 help
482  
483 help
484  
485 help $commandname
486  
487 With no arguments, prints a summary table of known command names and their
488 descriptive text. If any global options have been registered, these are
489 described as well.
490  
491 With a command name argument, prints more descriptive text about that command,
492 additionally detailing the arguments and options.
493  
494 The package that implements a particular command can provide more output by
495 implementing a method called C<commandable_more_help>, which will take as a
496 single argument the name of the command being printed. It should make use of
497 the various printing methods in L<Commandable::Output> to generate whatever
498 extra output it wishes.
499  
500 =cut
501  
502 2 ▶sub _print_optspecs ( $optspecs )
Subroutine _print_optspecs: called CC=7 SCAR=19.5
503 {
504 # @optspecs may contain duplicates; filter them
505 2 my %primary_names = map { $_->name => 1 } values %$optspecs;
506 2 my @optspecs = @$optspecs{ sort keys %primary_names };
507  
508 2 my $first = 1;
509 2 foreach my $optspec ( @optspecs ) {
510 5 ▶ Commandable::Output->printf( "\n" ) unless $first; undef $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 3 2 5
511  
512 5 my $default = $optspec->default;
513 5 ▶ my $value = $optspec->mode eq "value" ? " <value>" : "";
Branch: 1 branch
BranchTrueFalseTotal
$optspec->mode eq 'value' ? : 2 3 5
514 5 ▶ my $no = $optspec->negatable ? "[no-]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$optspec->negatable ? : 1 4 5
515  
516 Commandable::Output->printf( " %s\n",
517 _join( ", ", map {
518 5 ▶ Commandable::Output->format_note( length $_ > 1 ? "--$no$_$value" : "-$_$value", 1 )
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 5 2 7
519 } $optspec->names )
520 );
521 5 ▶ Commandable::Output->printf( " %s%s\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 1 4 5
522 $optspec->description,
523 ( defined $default ? " (default: $default)" : "" ),
524 );
525 }
526 }
527  
528 1 ▶sub builtin_command_helpsummary ( $self )
Subroutine builtin_command_helpsummary: called CC=2 SCAR=6.9
Pod undocumented
529 {
530 1 my @commands = sort { $a->name cmp $b->name } $self->find_commands;
531  
532 1 Commandable::Output->print_heading( "COMMANDS:" );
533 _print_table2 ": ", map {
534 1 [ " " . Commandable::Output->format_note( $_->name ), $_->description ]
535 } @commands;
536  
537 1 ▶ if( my $opts = $self->{global_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = $self->{"global_options"}) 1 0 1
538 1 Commandable::Output->printf( "\n" );
539 1 Commandable::Output->print_heading( "GLOBAL OPTIONS:" );
540 1 _print_optspecs( $opts );
541 }
542 }
543  
544 1 ▶sub builtin_command_helpcmd ( $self, $cmdname )
Subroutine builtin_command_helpcmd: called CC=9 SCAR=22.9
Pod undocumented
545 {
546 1 ▶ my $cmd = $self->find_command( $cmdname ) or
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = $self->find_command($cmdname) or die "Unrecognised command '${cmdname}' - see 'help' for a list of commands\n" 1 0 1
547 die "Unrecognised command '$cmdname' - see 'help' for a list of commands\n";
548  
549 1 my @argspecs = $cmd->arguments;
550 1 my %optspecs = $cmd->options;
551  
552 1 Commandable::Output->printf( "%s - %s\n",
553 Commandable::Output->format_note( $cmd->name ),
554 $cmd->description
555 );
556 1 Commandable::Output->printf( "\n" );
557  
558 1 Commandable::Output->print_heading( "SYNOPSIS:" );
559 Commandable::Output->printf( " %s\n",
560 join " ",
561 $cmd->name,
562 %optspecs ? "[OPTIONS...]" : (),
563 @argspecs ? (
564 map {
565 1 ▶ my $argspec = $_;
Branch: 2 branches
BranchTrueFalseTotal
%optspecs ? : 1 0 1
@argspecs ? : 1 0 1
566 1 my $str = "\$" . uc $argspec->name;
567 1 ▶ $str .= "..." if $argspec->slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $argspec->slurpy 0 1 1
568 1 ▶ $str = "($str)" if $argspec->optional;
Branch: 1 branch
BranchTrueFalseTotal
if $argspec->optional 0 1 1
569 1 $str;
570 } @argspecs
571 ) : ()
572 );
573  
574 1 ▶ if( %optspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%optspecs) 1 0 1
575 1 Commandable::Output->printf( "\n" );
576 1 Commandable::Output->print_heading( "OPTIONS:" );
577  
578 1 _print_optspecs( \%optspecs );
579 }
580  
581 1 ▶ if( @argspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argspecs) 1 0 1
582 1 Commandable::Output->printf( "\n" );
583 1 Commandable::Output->print_heading( "ARGUMENTS:" );
584  
585 _print_table2 " ", map {
586 1 [ " " . Commandable::Output->format_note( '$' . uc $_->name, 1 ),
587 $_->description ]
588 } @argspecs;
589 }
590  
591 1 my $cmdpkg = $cmd->package;
592 1 ▶ if( $cmdpkg->can( "commandable_more_help" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdpkg->can("commandable_more_help")) 0 1 1
593 0 $cmdpkg->commandable_more_help( $cmdname );
594 }
595  
596 1 return 0;
597 }
598  
599 =head1 AUTHOR
600  
601 Paul Evans <leonerd@leonerd.org.uk>
602  
603 =cut
604  
605 11 0x55AA;
« Commandable/Finder/SubAttributes.pm ↑ Summary Commandable/Finder/Packages.pm »