Class: Toys::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/runner.rb

Overview

An internal class that orchestrates execution of a tool.

Generaly, you should not need to use this class directly. Instead, run a tool using CLI#run.

Instance Method Summary collapse

Constructor Details

#initialize(cli, tool_definition) ⇒ Runner

Create a runner for a particular tool in a particular CLI.

Parameters:

  • cli (Toys::CLI)

    The CLI that is running the tool. This will provide needed context information.

  • tool_definition (Toys::Definition::Tool)

    The tool to run.



49
50
51
52
# File 'lib/toys/runner.rb', line 49

def initialize(cli, tool_definition)
  @cli = cli
  @tool_definition = tool_definition
end

Instance Method Details

#run(args, verbosity: 0) ⇒ Integer

Run the tool, provided given arguments.

Parameters:

  • args (Array<String>)

    Command line arguments passed to the tool.

  • verbosity (Integer) (defaults to: 0)

    Initial verbosity. Default is 0.

Returns:

  • (Integer)

    The resulting status code



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/toys/runner.rb', line 62

def run(args, verbosity: 0)
  data = create_data(args, verbosity)
  parse_args(args, data) unless @tool_definition.argument_parsing_disabled?
  tool = @tool_definition.tool_class.new(@cli, data)
  @tool_definition.run_initializers(tool)

  original_level = @cli.logger.level
  @cli.logger.level = @cli.base_level - data[Tool::Keys::VERBOSITY]
  begin
    perform_execution(tool)
  ensure
    @cli.logger.level = original_level
  end
end