File Coverage

blib/lib/App/Commando/Presenter.pm
Criterion Covered Total %
statement 42 42 100.0
branch 10 10 100.0
condition n/a
subroutine 10 10 100.0
pod 0 6 0.0
total 62 68 91.1


line stmt bran cond sub pod time code
1             package App::Commando::Presenter;
2              
3 4     4   20 use strict;
  4         9  
  4         129  
4 4     4   35 use warnings;
  4         8  
  4         102  
5              
6 4     4   20 use Moo;
  4         22  
  4         22  
7 4     4   1615 use Scalar::Util qw(refaddr);
  4         10  
  4         2258  
8              
9             has 'command' => ( is => 'rw' );
10              
11             sub BUILDARGS {
12 3     3 0 1007 my ($class, $command) = @_;
13              
14             return {
15 3         73 command => $command,
16             };
17             }
18              
19             sub usage_presentation {
20 2     2 0 3 my ($self) = @_;
21              
22 2         10 return ' ' . $self->command->syntax;
23             }
24              
25             sub options_presentation {
26 3     3 0 933 my ($self) = @_;
27              
28 3 100       5 return if !@{$self->command->options};
  3         20  
29              
30 2         4 return join "\n", map { $_->as_string } @{$self->command->options};
  4         14  
  2         8  
31             }
32              
33             sub subcommands_presentation {
34 3     3 0 174 my ($self) = @_;
35              
36 3 100       4 return if !%{$self->command->commands};
  3         26  
37              
38 2         11 return join "\n", map { $_->summarize }
  2         15  
39             # Remove duplicate commands
40 2         4 values %{{
41 2         4 map { refaddr($_) => $_ } values %{$self->command->commands}
  2         11  
42             }};
43             }
44              
45             sub command_header {
46 2     2 0 3 my ($self) = @_;
47              
48 2         10 my $header = $self->command->identity;
49 2 100       13 $header .= " -- " . $self->command->description
50             if $self->command->description;
51              
52 2         7 return $header;
53             }
54              
55             sub command_presentation {
56 2     2 0 15 my ($self) = @_;
57              
58 2         4 my @msg = ();
59              
60 2         7 push @msg,
61             $self->command_header,
62             'Usage:',
63             $self->usage_presentation;
64              
65 2 100       6 if (my $options = $self->options_presentation) {
66 1         3 push @msg, "Options:\n" . $options;
67             }
68              
69 2 100       6 if (my $subcommands = $self->subcommands_presentation) {
70 1         3 push @msg, "Subcommands:\n" . $subcommands;
71             }
72              
73 2         15 return join "\n\n", @msg;
74             }
75              
76             1;
77              
78             __END__