| 1 | package CLI::Dispatch; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use String::CamelCase; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | our $VERSION = '0.19'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # you may want to override these three methods. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 68 | ▶ | sub options {qw( help|h|? verbose|v debug logfilter=s )} | ||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub default_command { 'help' } | ||||||||||||||||||||||||||
|
Subroutine
default_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub get_command { | ||||||||||||||||||||||||||||
| 19 | 54 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_command: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 54 | ▶ | my $command = shift @ARGV || $self->default_command; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift @ARGV || $self->default_command
MC/DC0%0/2
shift @ARGV || $self->default_command
shift @ARGV
$self->default_command
Truth table66%2/3
A || B
A= shift @ARGVB= $self->default_command
| |||||||||||||||||||||||||||||
| 22 | 54 | return $self->convert_command($command); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub convert_command { | ||||||||||||||||||||||||||||
| 26 | 74 | ▶ | my ($self, $command) = @_; | ||||||||||||||||||||||||||
|
Subroutine
convert_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 74 | $command = String::CamelCase::camelize( $command ); | |||||||||||||||||||||||||||
| 29 | 74 | $command =~ tr/a-zA-Z0-9_//cd; | |||||||||||||||||||||||||||
| 30 | 74 | return $command; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # you usually don't need to care below. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub new { | ||||||||||||||||||||||||||||
| 36 | 68 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 68 | bless \%opts, $class; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub get_options { | ||||||||||||||||||||||||||||
| 41 | 133 | ▶ | my ($self, @specs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 133 | my $parser = Getopt::Long::Parser->new( | |||||||||||||||||||||||||||
| 44 | config => [qw( bundling ignore_case pass_through )] | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 133 | $parser->getoptions( \my %hash => @specs ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 133 | return %hash; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub load_command { | ||||||||||||||||||||||||||||
| 53 | 59 | ▶ | my ($self, $namespaces, $help) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_command: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 59 | my $command = $self->get_command; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 59 | ▶ | if ( $help ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 7 | unshift @ARGV, $command; | |||||||||||||||||||||||||||
| 59 | 7 | $command = 'Help'; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 59 | my $instance = $self->_load_command($namespaces, $command); | |||||||||||||||||||||||||||
| 63 | 56 | ▶ | return $instance if $instance; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # fallback to help (maybe the command is just a pod) | ||||||||||||||||||||||||||||
| 66 | 4 | unshift @ARGV, $command; | |||||||||||||||||||||||||||
| 67 | 4 | $instance = $self->_load_command($namespaces, 'Help'); | |||||||||||||||||||||||||||
| 68 | 4 | ▶ | return $instance if $instance; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # this shouldn't happen | ||||||||||||||||||||||||||||
| 71 | 0 | print STDERR "Help command is missing or broken.\n"; | |||||||||||||||||||||||||||
| 72 | 0 | print STDERR "Prerequisite modules may not be installed.\n"; | |||||||||||||||||||||||||||
| 73 | 0 | print STDERR "Please check your installation.\n"; | |||||||||||||||||||||||||||
| 74 | 0 | exit; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub _load_command { | ||||||||||||||||||||||||||||
| 78 | 63 | ▶ | my ($self, $namespaces, $command) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_load_command: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 63 | foreach my $namespace (@$namespaces) { | |||||||||||||||||||||||||||
| 81 | 67 | my $package = $namespace.'::'.$command; | |||||||||||||||||||||||||||
| 82 | 67 | ▶ | return $package->new if $package->can('new'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 36 | my $error; | |||||||||||||||||||||||||||
| 85 | 36 | ▶ | try { eval "require $package" or die } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 36 | ▶ | catch { $error = $_ || 'Obscure error' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$_ || 'Obscure error'
MC/DC0%0/1
$_ || 'Obscure error'
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||
| 87 | 36 | ▶ | return $package->new unless $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 18 | my $file = _package_file($package); | |||||||||||||||||||||||||||
| 90 | 18 | ▶ | next if $error =~ /Can't locate $file/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 3 | croak $error; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | ▶ | if ($command eq 'Help') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 7 | require CLI::Dispatch::Help; | |||||||||||||||||||||||||||
| 96 | 7 | return CLI::Dispatch::Help->new; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 4 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub _package_file { | ||||||||||||||||||||||||||||
| 102 | 18 | ▶ | my $package = shift; | ||||||||||||||||||||||||||
|
Subroutine
_package_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 18 | $package =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 105 | 18 | $package .= '\.(?:pm|pod)'; | |||||||||||||||||||||||||||
| 106 | 18 | $package; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub run { | ||||||||||||||||||||||||||||
| 110 | 59 | ▶ | my ($self, @namespaces) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 59 | my $class; | |||||||||||||||||||||||||||
| 113 | 59 | ▶ | unless ($class = ref $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 59 | $class = $self; | |||||||||||||||||||||||||||
| 115 | 59 | $self = $self->new; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 59 | ▶ | if (!grep { $_ ne $class } @namespaces) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 29 | push @namespaces, $class; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 59 | my %global = $self->get_options( $self->options ); | |||||||||||||||||||||||||||
| 123 | 59 | my $command = $self->load_command( \@namespaces, $global{help} ); | |||||||||||||||||||||||||||
| 124 | 56 | my %local = $self->get_options( $command->options ); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 56 | $command->set_options( %$self, %global, %local, _namespaces => \@namespaces ); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 56 | ▶ | if ( $command->isa('CLI::Dispatch::Help') and @ARGV ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$command->isa("CLI::Dispatch::Help") and @ARGV
MC/DC100%2/2
$command->isa("CLI::Dispatch::Help") and @ARGV
$command->isa("CLI::Dispatch::Help")
@ARGV
Truth table100%3/3
A and B
A= $command->isa("CLI::Dispatch::Help")B= @ARGV
| |||||||||||||||||||||||||||||
| 129 | 20 | $ARGV[0] = $self->convert_command($ARGV[0]); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 56 | ▶ | $command->check if $command->can('check'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 53 | $command->run(@ARGV); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub run_directly { | ||||||||||||||||||||||||||||
| 138 | 9 | ▶ | my ($self, $package) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run_directly: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 9 | ▶ | unless ($package->can('new')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | my $error; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | try { eval "require $package" or die } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | catch { $error = $_ || 'Obscure error' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_ || 'Obscure error'
MC/DC0%0/1
$_ || 'Obscure error'
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | croak $error if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 9 | my $class; | |||||||||||||||||||||||||||
| 148 | 9 | ▶ | unless ($class = ref $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 9 | $class = $self; | |||||||||||||||||||||||||||
| 150 | 9 | $self = $self->new; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 9 | my %global = $self->get_options( $self->options ); | |||||||||||||||||||||||||||
| 154 | 9 | my $command = $package->new; | |||||||||||||||||||||||||||
| 155 | 9 | ▶ | if ($global{help}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 1 | require CLI::Dispatch::Help; | |||||||||||||||||||||||||||
| 157 | 1 | $command = CLI::Dispatch::Help->new; | |||||||||||||||||||||||||||
| 158 | 1 | unshift @ARGV, "+$package"; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 9 | my %local = $self->get_options( $command->options ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 9 | $command->set_options( %global, %local ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 9 | ▶ | $command->check if $command->can('check'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 9 | $command->run(@ARGV); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 10 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||