Class: Brut::CLI::Runner
- Inherits:
-
Object
- Object
- Brut::CLI::Runner
- Defined in:
- lib/brut/cli/runner.rb
Overview
Runs a CLI app/command. Handles all the logic of parsing command line arguments, locating the command object to process the execution, allow for help requests, and understanding the output.
Instance Method Summary collapse
-
#initialize(app_command, stdout:, stderr:, stdin:, project_root:) ⇒ Runner
constructor
Create the runner, which can be used to run
app_commandwith whatever command line was provided. -
#run!(argv, env) ⇒ Integer
Run the commmand or subcommand based on the
app_commandgiven to the constructor and the command line provided inargv.
Constructor Details
#initialize(app_command, stdout:, stderr:, stdin:, project_root:) ⇒ Runner
Create the runner, which can be used to run app_command with whatever command line was provided.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/brut/cli/runner.rb', line 13 def initialize(app_command, stdout:, stderr:,stdin:, project_root:) prefix = if $0 =~ /\/brut$/ "brut" else $0 end @app_command = app_command @stdout = Brut::CLI::Output.new(io: stdout, prefix: "[ #{prefix} ] ") @stderr = Brut::CLI::Output.new(io: stderr, prefix: "[ #{prefix} ] ") @stdin = stdin @project_root = project_root end |
Instance Method Details
#run!(argv, env) ⇒ Integer
Run the commmand or subcommand based on the app_command given to the constructor and the command line
provided in argv.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/brut/cli/runner.rb', line 34 def run!(argv, env) parsed_command_line = Brut::CLI::ParsedCommandLine.new(app_command: @app_command, argv:, env:) load_unix_environment!(env, parsed_command_line) setup_log_level(env, parsed_command_line) bootstrap!(env, parsed_command_line) execute_result = Brut::CLI::ExecuteResult.new do execution_context = Brut::CLI::Commands::ExecutionContext.new( argv: parsed_command_line.argv, options: Brut::CLI::Options.new(parsed_command_line.), env:, stdout: @stdout, stderr: @stderr, stdin: @stdin ) parsed_command_line.command.execute(execution_context) end execute_result.exit_status do || @stderr.puts end end |