Class: Ace::Support::Cli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/cli/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(registry, parser_class: Parser) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
# File 'lib/ace/support/cli/runner.rb', line 9

def initialize(registry, parser_class: Parser)
  @registry = registry
  @parser_class = parser_class
end

Instance Method Details

#call(args: ARGV) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ace/support/cli/runner.rb', line 14

def call(args: ARGV)
  if root_help_request?(args)
    puts Ace::Support::Cli::Help::Usage.new(@registry, program_name: resolve_program_name).render
    return 0
  end

  command_target, remaining, command_name = resolve_target(args)
  command_class = command_target.is_a?(Class) ? command_target : command_target.class
  parsed = @parser_class.new(command_class, command_name: command_name).parse(remaining)
  result = if command_target.is_a?(Class)
    command_target.new.call(**parsed)
  else
    command_target.call(**parsed)
  end
  result.nil? ? 0 : result
rescue Ace::Support::Cli::HelpRendered => e
  puts e.output
  e.status
rescue Ace::Support::Cli::ParseError => e
  raise Ace::Support::Cli::Error.new(e.message)
rescue Ace::Support::Cli::CommandNotFoundError => e
  raise Ace::Support::Cli::Error.new(e.message)
end