App-Cmd
view release on metacpan or search on metacpan
lib/App/Cmd.pm view on Meta::CPAN
#pod
#pod arg0 - rpg
#pod full_arg0 - /Users/rjbs/bin/rpg
#pod
#pod These values are captured when the App::Cmd object is created, so it is safe to
#pod assign to C<$0> later.
#pod
#pod =cut
sub arg0 { $_[0]->{arg0} }
sub full_arg0 { $_[0]->{full_arg0} }
#pod =method prepare_command
#pod
#pod my ($cmd, $opt, @args) = $app->prepare_command(@ARGV);
#pod
#pod This method will load the plugin for the requested command, use its options to
#pod parse the command line arguments, and eventually return everything necessary to
#pod actually execute the command.
#pod
#pod =cut
sub prepare_command {
my ($self, @args) = @_;
# figure out first-level dispatch
my ($command, $opt, @sub_args) = $self->get_command(@args);
# set up the global options (which we just determined)
$self->set_global_options($opt);
# find its plugin or else call default plugin (default default is help)
if ($command) {
$self->_prepare_command($command, $opt, @sub_args);
} else {
$self->_prepare_default_command($opt, @sub_args);
}
}
sub _prepare_command {
my ($self, $command, $opt, @args) = @_;
if (my $plugin = $self->plugin_for($command)) {
return $plugin->prepare($self, @args);
} else {
return $self->_bad_command($command, $opt, @args);
}
}
sub _prepare_default_command {
my ($self, $opt, @sub_args) = @_;
$self->_prepare_command($self->default_command, $opt, @sub_args);
}
sub _bad_command {
my ($self, $command, $opt, @args) = @_;
print "Unrecognized command: $command.\n\nUsage:\n" if defined($command);
# This should be class data so that, in Bizarro World, two App::Cmds will not
# conflict.
our $_bad++;
$self->prepare_command(qw(commands --stderr));
}
END { exit 1 if our $_bad };
#pod =method default_command
#pod
#pod This method returns the name of the command to run if none is given on the
#pod command line. The default default is "help"
#pod
#pod =cut
sub default_command { "help" }
#pod =method execute_command
#pod
#pod $app->execute_command($cmd, \%opt, @args);
#pod
#pod This method will invoke C<validate_args> and then C<run> on C<$cmd>.
#pod
#pod =cut
sub execute_command {
my ($self, $cmd, $opt, @args) = @_;
local our $active_cmd = $cmd;
$cmd->validate_args($opt, \@args);
$cmd->execute($opt, \@args);
}
#pod =method plugin_search_path
#pod
#pod This method returns the plugin_search_path as set. The default implementation,
#pod if called on "YourApp::Cmd" will return "YourApp::Cmd::Command"
#pod
#pod This is a method because it's fun to override it with, for example:
#pod
#pod use constant plugin_search_path => __PACKAGE__;
#pod
#pod =cut
sub _default_command_base {
my ($self) = @_;
my $class = ref $self || $self;
return "$class\::Command";
}
sub _default_plugin_base {
my ($self) = @_;
my $class = ref $self || $self;
return "$class\::Plugin";
}
sub plugin_search_path {
my ($self) = @_;
my $dcb = $self->_default_command_base;
my $ccb = $dcb eq 'App::Cmd::Command'
? $self->App::Cmd::_default_command_base
: $self->_default_command_base;
( run in 1.710 second using v1.01-cache-2.11-cpan-39bf76dae61 )