CLI-Dispatch

 view release on metacpan or  search on metacpan

lib/CLI/Dispatch.pm  view on Meta::CPAN

package CLI::Dispatch;

use strict;
use warnings;
use Carp;
use Getopt::Long ();
use String::CamelCase;
use Try::Tiny;

our $VERSION = '0.21';

# you may want to override these three methods.

sub options {qw( help|h|? verbose|v debug logfilter=s )}

sub default_command { 'help' }

sub get_command {
  my $self = shift;

  my $command = shift @ARGV || $self->default_command;
  return $self->convert_command($command);
}

sub convert_command {
  my ($self, $command) = @_;

  $command = String::CamelCase::camelize( $command );
  $command =~ tr/a-zA-Z0-9_//cd;
  return $command;
}

# you usually don't need to care below.

sub new {
  my ($class, %opts) = @_;
  bless \%opts, $class;
}

sub get_options {
  my ($self, @specs) = @_;

  my $parser = Getopt::Long::Parser->new(
    config => [qw( bundling ignore_case pass_through )]
  );

  $parser->getoptions( \my %hash => @specs );

  return %hash;
}

sub load_command {
  my ($self, $namespaces, $help) = @_;

  my $command = $self->get_command;

  if ( $help ) {
    unshift @ARGV, $command;
    $command = 'Help';
  }

  my $instance = $self->_load_command($namespaces, $command);
  return $instance if $instance;

  # fallback to help (maybe the command is just a pod)
  unshift @ARGV, $command;
  $instance = $self->_load_command($namespaces, 'Help');
  return $instance if $instance;



( run in 1.310 second using v1.01-cache-2.11-cpan-39bf76dae61 )