CmdDispatch.pm

filter: Statementstmt 91.9% 114 / 124 Branchbran 70.3% 38 / 54 Conditioncond 57.1% 8 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 9 / 9 totaltotal 83.1% 193 / 232 CC 34 SCAR 17.8
CC
34
Cov
83%
new
14.4
_initialize_io_object
5.5
_setup_commands
5.0
CRAP 5.9
↑ Summary CmdDispatch/Help.pm »
1 package App::CmdDispatch;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use App::CmdDispatch::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use App::CmdDispatch::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.44';
12  
13 sub new
14 45 ▶{
Subroutine new: called CC=14 SCAR=26.6
Pod documented
15 45 my ( $class, $commands, $options ) = @_;
16  
17 45 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
387
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
18 45 ▶ die "Command definition is not a hashref.\n" unless ref $commands eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $commands eq ref {} 2 43 45
19 43 ▶ die "No commands specified.\n" unless keys %{$commands};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$commands;} 1 42 43
20 42 ▶ die "Options parameter is not a hashref.\n" unless $options and ref $options eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless $options and ref $options eq ref {} 1 41 42
Condition66%2/3
$options and ref $options eq ref {}
!ll&&!rl&&r
0141
MC/DC50%1/2
$options and ref $options eq ref {}
$options ref $options eq ref {}
Truth table66%2/3
A and B
A= $optionsB= ref $options eq ref {}
ABresult
0X0
100
111
21  
22 41 $options = { %{$options} };
23 41 my $config_file = delete $options->{config};
24 41 my $aliases;
25 41 my $self = bless { config => $options }, $class;
26 $self->{_command_sorter} = delete $self->{config}->{command_sort}
27 41 ▶ if $self->{config}->{command_sort} && 'CODE' eq ref $self->{config}->{command_sort};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"command_sort"} and "CODE" eq ref $self->{"config"}{"command_sort"} 0 41 41
Condition33%1/3
$self->{"config"}{"command_sort"} and "CODE" eq ref $self->{"config"}{"command_sort"}
!ll&&!rl&&r
4100
MC/DC0%0/2
$self->{"config"}{"command_sort"} and "CODE" eq ref $self->{"config"}{"command_sort"}
$self->{"config"}{"command_sort"} "CODE" eq ref $self->{"config"}{"command_sort"}
Truth table33%1/3
A and B
A= $self->{"config"}{"command_sort"}B= "CODE" eq ref $self->{"config"}{"command_sort"}
ABresult
0X0
100
111
28 41 ▶ if( defined $config_file )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 7 34 41
29 {
30 7 ▶ die "Supplied config is not a file.\n" unless -f $config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $config_file 1 6 7
31 6 $self->_initialize_config( $config_file );
32 }
33 40 $aliases = delete $self->{config}->{alias};
34 40 ▶ $aliases = {} unless ref $aliases eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aliases eq ref {} 32 8 40
35  
36 40 $commands = $self->_setup_commands( $commands );
37  
38 # TODO - replace the hard-coded Table module name with a parameter.
39 40 my $table = App::CmdDispatch::Table->new( $commands, $aliases );
40 37 ▶ if( $self->{_helper} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_helper"}) 22 15 37
41 {
42 22 $self->{_helper}->normalize_command_help( $table );
43 22 ▶ $self->{_command_sorter} ||= ( ref $self->{_helper} )->can( "sort_commands" );
Condition33%1/3
$self->{"_command_sorter"} ||= ref($self->{"_helper"})->can("sort_commands")
l!l&&r!l&&!r
0220
MC/DC0%0/2
$self->{"_command_sorter"} ||= ref($self->{"_helper"})->can("sort_commands")
$self->{"_command_sorter"} ref($self->{"_helper"})->can("sort_commands")
Truth table33%1/3
A ||= B
A= $self->{"_command_sorter"}B= ref($self->{"_helper"})->can("sort_commands")
ABresult
1X1
011
000
44 }
45 37 $self->{table} = $table;
46 37 $self->_initialize_io_object();
47 37 ▶ $self->{_command_sorter} ||= sub { return ( sort @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$self->{"_command_sorter"} ||= sub { return sort(@_); }
l!l&&r!l&&!r
22150
MC/DC0%0/2
$self->{"_command_sorter"} ||= sub { return sort(@_); }
$self->{"_command_sorter"} sub { return sort(@_); }
Truth table66%2/3
A ||= B
A= $self->{"_command_sorter"}B= sub { return sort(@_); }
ABresult
1X1
011
000
48  
49 37 return $self;
50 }
51  
52 1 ▶sub get_config { return $_[0]->{config}; }
Subroutine get_config: called CC=1 SCAR=0.0
Pod documented
53  
54 sub run
55 49 ▶{
Subroutine run: called CC=3 SCAR=11.1
Pod documented
56 49 my ( $self, $cmd, @args ) = @_;
57  
58 eval {
59 49 $self->{table}->run( $self, $cmd, @args );
60 46 1;
61 } or do
62 49 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"table"}->run($self, $cmd, @args); 1 } } or do { my $ex = $@; if (ref($ex) =~ /\AApp::CmdDispatch::Exception/) { $self->_print($ex->why, "\n"); $self->command_hint; } else { die $ex; } } 46 3 49
63 3 my $ex = $@;
64 3 ▶ if( ref( $ex ) =~ /\AApp::CmdDispatch::Exception/ )
Branch: 1 branch
BranchTrueFalseTotal
if (ref($ex) =~ /\AApp::CmdDispatch::Exception/) { } 3 0 3
65 {
66 3 $self->_print( $ex->why(), "\n" );
67 3 $self->command_hint;
68 }
69 else
70 {
71 0 die $ex;
72 }
73 };
74 49 return;
75 }
76  
77 sub command_list
78 44 ▶{
Subroutine command_list: called CC=1 SCAR=0.0
Pod documented
79 44 my ( $self ) = @_;
80 44 return $self->{_command_sorter}->( $self->{table}->command_list() );
81 }
82  
83 sub command_hint
84 3 ▶{
Subroutine command_hint: called CC=2 SCAR=9.2
Pod documented
85 3 my ( $self ) = @_;
86 3 ▶ return $self->{_helper}->hint() if defined $self->{_helper};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_helper"} 3 0 3
87 0 $self->_print( "Commands: ", join( ', ', $self->command_list() ), "\n" );
88 0 return;
89 }
90  
91 sub hint
92 19 ▶{
Subroutine hint: called CC=3 SCAR=13.5
Pod documented
93 19 my ( $self, $arg ) = @_;
94 eval {
95 19 $self->run( 'hint', $arg );
96 19 1;
97 } or do
98 19 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->run("hint", $arg); 1 } } or do { return $self->{"_helper"}->hint($arg) if defined $self->{"_helper"}; $self->_print("Commands: ", join(", ", $self->command_list), "\n") } 19 0 19
99 0 ▶ return $self->{_helper}->hint( $arg ) if defined $self->{_helper};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_helper"} 0 0 0
100 0 $self->_print( "Commands: ", join( ', ', $self->command_list() ), "\n" );
101 };
102 19 return;
103 }
104  
105 sub help
106 19 ▶{
Subroutine help: called CC=3 SCAR=13.5
Pod documented
107 19 my ( $self, $arg ) = @_;
108 eval {
109 19 $self->run( 'help', $arg );
110 19 1;
111 } or do
112 19 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->run("help", $arg); 1 } } or do { return $self->{"_helper"}->help($arg) if defined $self->{"_helper"}; $self->_print("Commands: ", join(", ", $self->command_list), "\n") } 19 0 19
113 0 ▶ return $self->{_helper}->help( $arg ) if defined $self->{_helper};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_helper"} 0 0 0
114 0 $self->_print( "Commands: ", join( ', ', $self->command_list() ), "\n" );
115 };
116 19 return;
117 }
118  
119 23 ▶sub alias_list { return $_[0]->{table}->alias_list(); }
Subroutine alias_list: called CC=1 SCAR=0.0
Pod documented
120  
121 sub shell
122 3 ▶{
Subroutine shell: called CC=4 SCAR=13.9
Pod documented
123 3 my ( $self ) = @_;
124  
125 3 $self->_print( "Enter a command or 'quit' to exit:\n" );
126 3 ▶ while ( my $line = $self->_prompt( '> ' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_prompt("> ")) 5 0 5
127 {
128 5 chomp $line;
129 5 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 1 4 5
130 4 ▶ last if $line eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" 3 1 4
131 1 $self->run( split /\s+/, $line );
132 }
133 3 return;
134 }
135  
136 sub _print
137 215 ▶{
Subroutine _print: called CC=1 SCAR=0.0
138 215 my $self = shift;
139 215 return $self->{io}->print( @_ );
140 }
141  
142 sub _prompt
143 5 ▶{
Subroutine _prompt: called CC=1 SCAR=0.0
144 5 my $self = shift;
145 5 return $self->{io}->prompt( @_ );
146 }
147  
148 sub _initialize_config
149 6 ▶{
Subroutine _initialize_config: called CC=2 SCAR=7.0
150 6 my ( $self, $config_file ) = @_;
151 6 my $conf = Config::Tiny->read( $config_file );
152 6 %{ $self->{config} } = (
153 6 ( $conf->{_} ? %{ delete $conf->{_} } : () ), # first extract the top level
154 6 %{$conf}, # Keep any multi-levels that are not aliases
155 6 ▶ %{ $self->{config} }, # Override with supplied parameters
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'_'} ? : 6 0 6
156 );
157 6 return;
158 }
159  
160 sub _initialize_io_object
161 37 ▶{
Subroutine _initialize_io_object: called CC=5 SCAR=17.1
162 37 my ( $self ) = @_;
163  
164 37 my $io = delete $self->{config}->{'io'};
165 37 ▶ if( !defined $io )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $io) { } 4 33 37
elsif (not _is_valid_io_object($io)) { } 0 33 33
166 {
167 eval {
168 4 $io = App::CmdDispatch::IO->new();
169 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $io = "App::CmdDispatch::IO"->new } } or do { $io = "App::CmdDispatch::MinimalIO"->new } 4 0 4
170 0 $io = App::CmdDispatch::MinimalIO->new();
171 };
172 4 ▶ die "Unable to create an IO object for CmdDispatch.\n" unless defined $io;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io 0 4 4
173 }
174 elsif( !_is_valid_io_object( $io ) )
175 {
176 0 die "Object supplied as io parameter does not supply correct interface.\n";
177 }
178  
179 37 $self->{io} = $io;
180 37 return;
181 }
182  
183 sub _is_valid_io_object
184 33 ▶{
Subroutine _is_valid_io_object: called CC=2 SCAR=7.0
185 33 my ( $io ) = @_;
186 33 ▶ return unless ref $io;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $io 0 33 33
187 33 return 2 == grep { $io->can( $_ ) } qw/print prompt/;
188 }
189  
190 sub _setup_commands
191 40 ▶{
Subroutine _setup_commands: called CC=5 SCAR=16.2
192 40 my ( $self, $commands ) = @_;
193 40 $commands = { %{$commands} };
194  
195 40 ▶ return $commands unless $self->{config}->{default_commands};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"default_commands"} 17 23 40
196  
197 23 foreach my $def ( split / /, $self->{config}->{default_commands} )
198 {
199 44 ▶ if( $def eq 'shell' )
Branch: 2 branches
BranchTrueFalseTotal
if ($def eq 'shell') { } 22 22 44
elsif ($def eq 'help') { } 22 0 22
200 {
201 $commands->{shell} = {
202 22 code => \&App::CmdDispatch::shell,
203 clue => 'shell',
204 abstract => 'Launch an interactive command shell.',
205 help => 'Execute commands as entered until quit.',
206 };
207 }
208 elsif( $def eq 'help' )
209 {
210 22 require App::CmdDispatch::Help;
211 22 $self->{_helper} = App::CmdDispatch::Help->new( $self, $commands, $self->{config} );
212 }
213 else
214 {
215 0 die "Unrecognized default command: '$def'\n";
216 }
217 }
218 23 return $commands;
219 }
220  
221 11 1;
222  
223 __END__
↑ Summary CmdDispatch/Help.pm »