| 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 ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 18 | 45 | ▶ | die "Command definition is not a hashref.\n" unless ref $commands eq ref {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 43 | ▶ | die "No commands specified.\n" unless keys %{$commands}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 42 | ▶ | die "Options parameter is not a hashref.\n" unless $options and ref $options eq ref {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options and ref $options eq ref {}
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 {}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{"config"}{"command_sort"} and "CODE" eq ref $self->{"config"}{"command_sort"}
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"}
| |||||||||||||||||||||||||||||
| 28 | 41 | ▶ | if( defined $config_file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | die "Supplied config is not a file.\n" unless -f $config_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||
| 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(@_);
}
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(@_);
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 3 | my $ex = $@; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | if( ref( $ex ) =~ /\AApp::CmdDispatch::Exception/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return $self->{_helper}->hint( $arg ) if defined $self->{_helper}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return $self->{_helper}->help( $arg ) if defined $self->{_helper}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 5 | chomp $line; | |||||||||||||||||||||||||||
| 129 | 5 | ▶ | next unless $line =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 4 | ▶ | last if $line eq 'quit'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | eval { | ||||||||||||||||||||||||||||
| 168 | 4 | $io = App::CmdDispatch::IO->new(); | |||||||||||||||||||||||||||
| 169 | 4 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 23 | foreach my $def ( split / /, $self->{config}->{default_commands} ) | |||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||
| 199 | 44 | ▶ | if( $def eq 'shell' ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||