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