| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::CmdDispatch::Table; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 14 |  |  | 14 |  | 66410 | use warnings; | 
|  | 14 |  |  |  |  | 23 |  | 
|  | 14 |  |  |  |  | 354 |  | 
| 4 | 14 |  |  | 14 |  | 59 | use strict; | 
|  | 14 |  |  |  |  | 23 |  | 
|  | 14 |  |  |  |  | 260 |  | 
| 5 | 14 |  |  | 14 |  | 7359 | use App::CmdDispatch::Exception; | 
|  | 14 |  |  |  |  | 33 |  | 
|  | 14 |  |  |  |  | 9994 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.43'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub new | 
| 10 |  |  |  |  |  |  | { | 
| 11 | 62 |  |  | 62 | 1 | 8073 | my ( $class, $commands, $aliases ) = @_; | 
| 12 | 62 |  | 100 |  |  | 162 | $aliases ||= {}; | 
| 13 | 62 | 100 |  |  |  | 219 | die "Command definition is not a hashref.\n" unless ref $commands eq ref {}; | 
| 14 | 60 | 100 |  |  |  | 95 | die "No commands specified.\n"               unless keys %{$commands}; | 
|  | 60 |  |  |  |  | 179 |  | 
| 15 | 59 | 100 |  |  |  | 166 | die "Aliases definition is not a hashref.\n" unless ref $aliases eq ref {}; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 58 |  |  |  |  | 218 | my $self = bless { | 
| 18 |  |  |  |  |  |  | cmds    => {}, | 
| 19 |  |  |  |  |  |  | alias   => {}, | 
| 20 |  |  |  |  |  |  | }, $class; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 58 |  |  |  |  | 172 | $self->_ensure_valid_command_description( $commands ); | 
| 23 | 52 |  |  |  |  | 121 | $self->_ensure_valid_aliases( $aliases ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 50 |  |  |  |  | 124 | return $self; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub run | 
| 29 |  |  |  |  |  |  | { | 
| 30 | 54 |  |  | 54 | 1 | 244 | my ( $self, $base, $cmd, @args ) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 54 | 100 | 100 |  |  | 288 | die App::CmdDispatch::Exception::MissingCommand->new if !defined $cmd || $cmd eq ''; | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Handle alias if one is supplied | 
| 35 | 49 | 100 |  |  |  | 125 | if( exists $self->{alias}->{$cmd} ) | 
| 36 |  |  |  |  |  |  | { | 
| 37 | 2 |  |  |  |  | 8 | ( $cmd, @args ) = ( ( split / /, $self->{alias}->{$cmd} ), @args ); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # Handle builtin commands | 
| 41 | 49 | 100 |  |  |  | 147 | die App::CmdDispatch::Exception::UnknownCommand->new( $cmd ) unless $self->{cmds}->{$cmd}; | 
| 42 | 47 |  |  |  |  | 145 | $self->{cmds}->{$cmd}->{'code'}->( $base, @args ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 47 |  |  |  |  | 114 | return; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub _ensure_valid_command_description | 
| 48 |  |  |  |  |  |  | { | 
| 49 | 58 |  |  | 58 |  | 104 | my ( $self, $cmds ) = @_; | 
| 50 | 58 |  |  |  |  | 84 | while ( my ( $key, $val ) = each %{$cmds} ) | 
|  | 182 |  |  |  |  | 627 |  | 
| 51 |  |  |  |  |  |  | { | 
| 52 | 130 | 100 |  |  |  | 250 | next if $key eq ''; | 
| 53 | 129 | 100 |  |  |  | 239 | if( !defined $val ) | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 1 |  |  |  |  | 2 | delete $self->{cmds}->{$key}; | 
| 56 | 1 |  |  |  |  | 2 | next; | 
| 57 |  |  |  |  |  |  | } | 
| 58 | 128 | 100 |  |  |  | 346 | die "Command '$key' is an invalid descriptor.\n" unless ref $val eq ref {}; | 
| 59 | 126 | 100 |  |  |  | 364 | die "Command '$key' has no handler.\n" unless ref $val->{code} eq 'CODE'; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 122 |  |  |  |  | 131 | my $desc = { %{$val} }; | 
|  | 122 |  |  |  |  | 432 |  | 
| 62 | 122 |  |  |  |  | 336 | $self->{cmds}->{$key} = $desc; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 52 |  |  |  |  | 79 | return; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub _ensure_valid_aliases | 
| 69 |  |  |  |  |  |  | { | 
| 70 | 52 |  |  | 52 |  | 76 | my ( $self, $aliases ) = @_; | 
| 71 | 52 |  |  |  |  | 68 | while ( my ( $key, $val ) = each %{$aliases} ) | 
|  | 73 |  |  |  |  | 224 |  | 
| 72 |  |  |  |  |  |  | { | 
| 73 | 23 | 100 |  |  |  | 63 | next if $key eq ''; | 
| 74 | 22 | 100 |  |  |  | 49 | if( !defined $val ) | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 1 |  |  |  |  | 2 | delete $self->{alias}->{$key}; | 
| 77 | 1 |  |  |  |  | 2 | next; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 21 | 100 |  |  |  | 58 | die "Alias '$key' mapping is not a string.\n" if ref $val; | 
| 80 | 19 |  |  |  |  | 44 | $self->{alias}->{$key} = $val; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 50 |  |  |  |  | 76 | return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub command_list | 
| 87 |  |  |  |  |  |  | { | 
| 88 | 97 |  |  | 97 | 1 | 2514 | my ($self) = @_; | 
| 89 | 97 |  |  |  |  | 115 | return sort keys %{$self->{cmds}};  ## no critic - intended to return list | 
|  | 97 |  |  |  |  | 598 |  | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub alias_list | 
| 93 |  |  |  |  |  |  | { | 
| 94 | 27 |  |  | 27 | 1 | 46 | my ($self) = @_; | 
| 95 | 27 |  |  |  |  | 33 | return sort keys %{$self->{alias}};  ## no critic - intended to return list | 
|  | 27 |  |  |  |  | 162 |  | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub get_command | 
| 99 |  |  |  |  |  |  | { | 
| 100 | 506 |  |  | 506 | 1 | 681 | my ($self, $cmd) = @_; | 
| 101 | 506 |  |  |  |  | 1455 | return $self->{cmds}->{$cmd}; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub get_alias | 
| 105 |  |  |  |  |  |  | { | 
| 106 | 34 |  |  | 34 | 1 | 55 | my ($self, $alias) = @_; | 
| 107 | 34 |  |  |  |  | 192 | return $self->{alias}->{$alias}; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub has_aliases | 
| 111 |  |  |  |  |  |  | { | 
| 112 | 39 |  |  | 39 | 1 | 57 | my ($self) = @_; | 
| 113 | 39 |  |  |  |  | 45 | return 0 != keys %{ $self->{alias} }; | 
|  | 39 |  |  |  |  | 187 |  | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | 1; | 
| 117 |  |  |  |  |  |  | __END__ |