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