| 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 ||= {}
MC/DC100%1/1
$aliases ||= {}
$aliases
Truth table100%2/2
A
A= $aliases
| |||||||||||||||||||||||||||||
| 13 | 62 | ▶ | die "Command definition is not a hashref.\n" unless ref $commands eq ref {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 60 | ▶ | die "No commands specified.\n" unless keys %{$commands}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 59 | ▶ | die "Aliases definition is not a hashref.\n" unless ref $aliases eq ref {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $cmd or $cmd eq ""
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 ""
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Handle alias if one is supplied | ||||||||||||||||||||||||||||
| 35 | 49 | ▶ | if( exists $self->{alias}->{$cmd} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 130 | ▶ | next if $key eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 129 | ▶ | if( !defined $val ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | 126 | ▶ | die "Command '$key' has no handler.\n" unless ref $val->{code} eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 23 | ▶ | next if $key eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 22 | ▶ | if( !defined $val ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||