CmdDispatch/Table.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 30 / 30 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 100% 121 / 121 CC 18 SCAR 14.1
CC
18
Cov
100%
_ensure_valid_command_description
6.0
new
5.0
run
5.0
CRAP 4.1
« CmdDispatch/Help.pm ↑ Summary CmdDispatch/IO.pm »
1 package App::CmdDispatch::Table;
2  
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use App::CmdDispatch::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 our $VERSION = '0.44';
8  
9 sub new
10 62 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod documented
11 62 my ( $class, $commands, $aliases ) = @_;
12 62 ▶ $aliases ||= {};
Condition100%2/2
$aliases ||= {}
l!l
4715
MC/DC100%1/1
$aliases ||= {}
$aliases
Truth table100%2/2
A
A= $aliases
Aresult
11
00
13 62 ▶ die "Command definition is not a hashref.\n" unless ref $commands eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $commands eq ref {} 2 60 62
14 60 ▶ die "No commands specified.\n" unless keys %{$commands};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$commands;} 1 59 60
15 59 ▶ die "Aliases definition is not a hashref.\n" unless ref $aliases eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aliases eq ref {} 1 58 59
16  
17 58 my $self = bless {
18 cmds => {},
19 alias => {},
20 }, $class;
21  
22 58 $self->_ensure_valid_command_description( $commands );
23 52 $self->_ensure_valid_aliases( $aliases );
24  
25 50 return $self;
26 }
27  
28 sub run
29 54 ▶{
Subroutine run: called CC=5 SCAR=16.1
Pod documented
30 54 my ( $self, $base, $cmd, @args ) = @_;
31  
32 54 ▶ die App::CmdDispatch::Exception::MissingCommand->new if !defined $cmd || $cmd eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd or $cmd eq "" 5 49 54
Condition100%3/3
not defined $cmd or $cmd eq ""
l!l&&r!l&&!r
3249
MC/DC100%2/2
not defined $cmd or $cmd eq ""
not defined $cmd $cmd eq ""
Truth table100%3/3
not(A) or B
A= not defined $cmdB= $cmd eq ""
ABresult
1X1
011
000
33  
34 # Handle alias if one is supplied
35 49 ▶ if( exists $self->{alias}->{$cmd} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"alias"}{$cmd}) 2 47 49
36 {
37 2 ( $cmd, @args ) = ( ( split / /, $self->{alias}->{$cmd} ), @args );
38 }
39  
40 # Handle builtin commands
41 49 ▶ die App::CmdDispatch::Exception::UnknownCommand->new( $cmd ) unless $self->{cmds}->{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmds"}{$cmd} 2 47 49
42 47 $self->{cmds}->{$cmd}->{'code'}->( $base, @args );
43  
44 47 return;
45 }
46  
47 sub _ensure_valid_command_description
48 58 ▶{
Subroutine _ensure_valid_command_description: called CC=6 SCAR=17.9
49 58 my ( $self, $cmds ) = @_;
50 58 ▶ while ( my ( $key, $val ) = each %{$cmds} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$cmds;}) 130 52 182
51 {
52 130 ▶ next if $key eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" 1 129 130
53 129 ▶ if( !defined $val )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 128 129
54 {
55 1 delete $self->{cmds}->{$key};
56 1 next;
57 }
58 128 ▶ die "Command '$key' is an invalid descriptor.\n" unless ref $val eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq ref {} 2 126 128
59 126 ▶ die "Command '$key' has no handler.\n" unless ref $val->{code} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val->{"code"} eq "CODE" 4 122 126
60  
61 122 my $desc = { %{$val} };
62 122 $self->{cmds}->{$key} = $desc;
63 }
64  
65 52 return;
66 }
67  
68 sub _ensure_valid_aliases
69 52 ▶{
Subroutine _ensure_valid_aliases: called CC=5 SCAR=16.1
70 52 my ( $self, $aliases ) = @_;
71 52 ▶ while ( my ( $key, $val ) = each %{$aliases} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$aliases;}) 23 50 73
72 {
73 23 ▶ next if $key eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" 1 22 23
74 22 ▶ if( !defined $val )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 21 22
75 {
76 1 delete $self->{alias}->{$key};
77 1 next;
78 }
79 21 ▶ die "Alias '$key' mapping is not a string.\n" if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 2 19 21
80 19 $self->{alias}->{$key} = $val;
81 }
82  
83 50 return;
84 }
85  
86 sub command_list
87 97 ▶{
Subroutine command_list: called CC=1 SCAR=0.0
Pod documented
88 97 my ($self) = @_;
89 97 return sort keys %{$self->{cmds}}; ## no critic - intended to return list
90 }
91  
92 sub alias_list
93 27 ▶{
Subroutine alias_list: called CC=1 SCAR=0.0
Pod documented
94 27 my ($self) = @_;
95 27 return sort keys %{$self->{alias}}; ## no critic - intended to return list
96 }
97  
98 sub get_command
99 506 ▶{
Subroutine get_command: called CC=1 SCAR=0.0
Pod documented
100 506 my ($self, $cmd) = @_;
101 506 return $self->{cmds}->{$cmd};
102 }
103  
104 sub get_alias
105 34 ▶{
Subroutine get_alias: called CC=1 SCAR=0.0
Pod documented
106 34 my ($self, $alias) = @_;
107 34 return $self->{alias}->{$alias};
108 }
109  
110 sub has_aliases
111 39 ▶{
Subroutine has_aliases: called CC=1 SCAR=0.0
Pod documented
112 39 my ($self) = @_;
113 39 return 0 != keys %{ $self->{alias} };
114 }
115  
116 14 1;
117 __END__
« CmdDispatch/Help.pm ↑ Summary CmdDispatch/IO.pm »