Scroll to navigation

App::Spec::Run(3) User Contributed Perl Documentation App::Spec::Run(3)

NAME

App::Spec::Run - App::Spec framework to run your app

DESCRIPTION

App::Spec::Run is the framework which runs your app defined by the spec. Your app class should inherit from App::Spec::Run::Cmd.

SYNOPSIS

    sub your_command {
        my ($self, $run) = @_;
        my $options = $run->options;
        $run->out("It works");
    }

METHODS

You can create the object yourself like this:

    my $run = App::Spec::Run->new(
        spec => $appspec,
        cmd => App::YourApp->new,
    );
    

Or you use the runner method of App::Spec, which will create it for you:

    my $run = $appspec->runner(...);
    

Both methods take optional arguments:

    my $run = App::Spec::Run->new(
        spec => $appspec,
        cmd => App::YourApp->new,
        # Custom array instead of the default ARGV.
        # The contents of this array will be modified
        argv => \@my_arguments,
    );
    
    $run->run;
    

Actually runs your app. Calls "process" and "finish".

    $run->process;
    

Processes input, validates, runs your command and fills the response object.

Does not print the output and does not exit.

    $run->out("Hello world!");
    

Appends to response output. Adds a newline if not present. You can also pass a data structure:

    $run->out($hashref);
    

This will be formatted with Data::Dumper.

See also App::Spec::Plugin::Format.

    $run->err("Oops, that went wrong");
    

Appends to response error output. Adds a newline if not present

    $run->halt;
    

Further processing is halted.

    $run->finish;
    

Prints the output and exits with the exit code stored in "response".

    $run->process_input(
        option_specs => \%option_specs,
        param_specs => \%param_specs,
    );
    
    $run->process_parameters(
        parameter_list => $global_parameters,
        param_specs => $param_specs,
    );
    
    $run->run_op("yourcommand");
    # shortcut for
    $run->cmd->yourcommand($run);
    
    $run->completion_output(
        param_specs => \%param_specs,
        completion_parameter => $completion_parameter,
    );
    

Is called when in completion mode

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!");
    
    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.

    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

    my $msg = $run->colorize_error("ouch");
    

Returns the message in the standard error color (bold red).

    $run->error_output;
    

Outputs any errors.

Calls "halt"

Calls any plugin that needs to know
A plugin can subscribe for an event:

    $run->subscribe(
        print_output => {
            plugin => $self,
            method => "print_output",
        },
    

ATTRIBUTES

Your spec, (App::Spec)
A hashref with the given options

    {
        verbose => 1,
        foo => 23,
    }
    
A hashref with the given parameters
An arrayref containing all subcommands from the commandline
This contains the original contents of "argv" before processing
This is a reference to the commandline arguments array @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.
Contains errors from option/parameter validation
Contains the operation (subroutine) which will be executed to run yor command
This is an instance of your app class
This contains the response of your command (exit code, output, ...)

See App::Spec::Run::Response

Contains a hashref

    {
        print_output => {
            module => $plugin,
            method => 'print_output',
        },
    }
    
2025-03-26 perl v5.42.1