Class: Rubycli::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycli/cli.rb

Defined Under Namespace

Classes: CommandCatalog, CommandEntry

Instance Method Summary collapse

Constructor Details

#initialize(environment:, argument_parser:, documentation_registry:, help_renderer:, result_emitter:) ⇒ CLI

Returns a new instance of CLI.



27
28
29
30
31
32
33
34
# File 'lib/rubycli/cli.rb', line 27

def initialize(environment:, argument_parser:, documentation_registry:, help_renderer:, result_emitter:)
  @environment = environment
  @argument_parser = argument_parser
  @documentation_registry = documentation_registry
  @help_renderer = help_renderer
  @result_emitter = result_emitter
  @file_cache = {}
end

Instance Method Details

#available_commands(target) ⇒ Object



58
59
60
# File 'lib/rubycli/cli.rb', line 58

def available_commands(target)
  command_catalog(target).commands.sort
end

#command_catalog_for(target) ⇒ Object



77
78
79
# File 'lib/rubycli/cli.rb', line 77

def command_catalog_for(target)
  command_catalog(target)
end

#exposable_method?(method_obj) ⇒ Boolean

True when the method can be exposed as a CLI command. Rubycli::Runner uses this to lint the same methods the CLI would expose.

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/rubycli/cli.rb', line 83

def exposable_method?(method_obj)
  return false unless method_obj&.source_location
  return false if accessor_generated_method?(method_obj)

  true
end

#find_method(target, command) ⇒ Object



62
63
64
65
66
67
# File 'lib/rubycli/cli.rb', line 62

def find_method(target, command)
  catalog = command_catalog(target)
  entry = catalog.lookup(command)
  entry ||= catalog.lookup(normalize_command(command)) if command.include?('-')
  entry&.method
end

#method_description(method) ⇒ Object



73
74
75
# File 'lib/rubycli/cli.rb', line 73

def method_description(method)
  @help_renderer.method_description(method)
end

#run(target, args = ARGV, cli_mode = true) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubycli/cli.rb', line 36

def run(target, args = ARGV, cli_mode = true)
  debug_log "Starting rubycli with args: #{args.inspect}"
  catalog = command_catalog(target)

  if should_show_help?(args)
    @help_renderer.print_help(target, catalog)
    return 0
  end

  command = args.shift
  entry = resolve_command_entry(catalog, command)

  if entry.nil?
    handle_missing_method(target, catalog, command, args, cli_mode)
  else
    execute_method(entry.method, command, args, cli_mode)
  end
rescue Rubycli::ArgumentError => e
  warn "[ERROR] #{e.message}"
  1
end

#usage_for_method(command, method) ⇒ Object



69
70
71
# File 'lib/rubycli/cli.rb', line 69

def usage_for_method(command, method)
  @help_renderer.usage_for_method(command, method)
end