App-Spec
view release on metacpan or search on metacpan
lib/App/Spec/Run.pm view on Meta::CPAN
$self->err($spec->usage(
commands => \@cmds,
colored => $self->colorize_code('err'),
highlights => {
subcommands => 1,
},
));
$self->err( $self->colorize_error("Unknown subcommand '$cmd'") );
$self->halt(1);
last;
};
$subcommand_required = $cmd_spec->{subcommand_required} // 1;
my $cmd_options = $cmd_spec->options;
my @getopt = $spec->make_getopt($cmd_options, \%options, $option_specs);
GetOptions(@getopt);
push @cmds, $cmd;
$commands = $cmd_spec->subcommands || {};
$op = '::' . $cmd_spec->op if $cmd_spec->op;
$opclass = $cmd_spec->class if $cmd_spec->class;
$self->process_parameters(
parameter_list => $cmd_spec->parameters,
param_specs => $param_specs,
);
}
unless ($self->response->halted) {
unless ($op) {
if ($spec->has_subcommands) {
$self->err( "Missing op for commands (@cmds)\n" );
my $help = $spec->usage(
commands => \@cmds,
colored => $self->colorize_code('err'),
);
$self->err( $help );
$self->halt(1);
}
else {
$op = "::execute";
}
}
$self->commands(\@cmds);
$self->options(\%options);
if ($op =~ m/^::/) {
$op = $opclass . $op;
}
$self->op($op);
return $op;
}
return;
}
sub colorize_error {
my ($self, $msg) = @_;
$msg = $self->colored('err', [qw/ error /], $msg) . "\n";
}
sub colored {
my ($self, $out, $colors, $msg) = @_;
$colors = [ map { $_ eq 'error' ? qw/ bold red / : $_ } @$colors ];
require Term::ANSIColor;
$self->colorize($out)
and $msg = Term::ANSIColor::colored($colors, $msg);
return $msg;
}
sub subscribe {
my ($self, %args) = @_;
for my $event (sort keys %args) {
next unless exists $EVENTS{ $event };
my $info = $args{ $event };
push @{ $self->subscribers->{ $event } }, $info;
}
}
sub event_globaloptions {
my ($self) = @_;
my $subscribers = $self->subscribers->{global_options};
for my $sub (@$subscribers) {
my $plugin = $sub->{plugin};
my $method = $sub->{method};
$plugin->$method( run => $self);
}
}
#sub event_processed {
# my ($self) = @_;
# my $plugins = $self->spec->plugins_by_type->{GlobalOptions};
# for my $plugin (@$plugins) {
# next unless $plugin->can("event_processed");
# $plugin->event_processed(
# run => $self,
# );
# }
#}
1;
__END__
=pod
=head1 NAME
App::Spec::Run - App::Spec framework to run your app
=head1 DESCRIPTION
App::Spec::Run is the framework which runs your app defined by the spec.
Your app class should inherit from L<App::Spec::Run::Cmd>.
=head1 SYNOPSIS
sub your_command {
my ($self, $run) = @_;
my $options = $run->options;
$run->out("It works");
}
lib/App/Spec/Run.pm view on Meta::CPAN
=item process_input
$run->process_input(
option_specs => \%option_specs,
param_specs => \%param_specs,
);
=item process_parameters
$run->process_parameters(
parameter_list => $global_parameters,
param_specs => $param_specs,
);
=item run_op
$run->run_op("yourcommand");
# shortcut for
$run->cmd->yourcommand($run);
=item completion_output
$run->completion_output(
param_specs => \%param_specs,
completion_parameter => $completion_parameter,
);
Is called when in completion mode
=item colored
Returns the given text colored, if colors are active for the given
output.
$msg = $run->colored('err', [qw/ error /], "Oops");
$msg = $run->colored('out', [qw/ green /], "Everything is fine!");
=item colorize
my $color_active = $run->colorize('out');
my $color_error_active = $run->colorize('err');
Returns 1 or 0 if given output color are active. That means, the output
is going to a terminal instead of being redirected.
=item colorize_code
my $colored = $run->colorize_code('out');
my $text = $colored->(['green'], "Hurray");
# or
my $text = "Hurray";
$colored->(['green'], $text);
Returns a coderef which you can use for coloring
=item colorize_error
my $msg = $run->colorize_error("ouch");
Returns the message in the standard error color (bold red).
=item error_output
$run->error_output;
Outputs any errors.
Calls C<halt>
=item event_globaloptions
Calls any plugin that needs to know
=item subscribe
A plugin can subscribe for an event:
$run->subscribe(
print_output => {
plugin => $self,
method => "print_output",
},
=back
=head1 ATTRIBUTES
=over 4
=item spec
Your spec, (App::Spec)
=item options
A hashref with the given options
{
verbose => 1,
foo => 23,
}
=item parameters
A hashref with the given parameters
=item commands
An arrayref containing all subcommands from the commandline
=item argv_orig
This contains the original contents of C<argv> before processing
=item argv
This is a reference to the commandline arguments array C<@ARGV>, or the
array reference you specified otherwise. When calling your command method,
it will contain the rest of the arguments which weren't processed as
any subcommand, option or parameter.
( run in 1.115 second using v1.01-cache-2.11-cpan-39bf76dae61 )