Class: Parabot::CLI::CommandRouter

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

Overview

Routes commands to appropriate handlers based on command type

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ CommandRouter

Returns a new instance of CommandRouter.



9
10
11
# File 'lib/parabot/cli/command_router.rb', line 9

def initialize(registry)
  @registry = registry
end

Instance Method Details

#find_builtin_command(args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/parabot/cli/command_router.rb', line 13

def find_builtin_command(args)
  # Get all registered command names from the registry
  result = @registry.get([])
  node = result.instance_variable_get(:@node)
  children = node.instance_variable_get(:@children).keys
  aliases = node.instance_variable_get(:@aliases).keys
  builtin_commands = children + aliases

  args.find { |arg| builtin_commands.include?(arg) }
end

#find_custom_command(args) ⇒ Object



24
25
26
27
# File 'lib/parabot/cli/command_router.rb', line 24

def find_custom_command(args)
  custom_commands = Configuration.current.custom_commands
  args.find { |arg| custom_commands.key?(arg) || custom_commands.key?(arg.to_sym) }
end

#route_command(args, parser) ⇒ Object



29
30
31
32
33
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/parabot/cli/command_router.rb', line 29

def route_command(args, parser)
  # First, extract options to get project_root before any configuration access
  options = parser.extract_options(args)

  # Now identify if we have known builtin commands anywhere in args
  builtin_command = find_builtin_command(args)

  # Load configuration with proper project root early (skip for init command and help)
  unless builtin_command == "init" || parser.help_requested?(args)
    CLI.ensure_configuration_loaded(options[:config], options[:project_root])
  end

  if builtin_command
    handle_builtin_command(args, builtin_command, parser)
  elsif args.empty? || parser.only_options?(args)
    handle_help([], parser)
  elsif parser.help_requested?(args)
    handle_help(args, parser)
  else
    # Check for custom commands or treat as message
    custom_command = find_custom_command(args)
    if custom_command
      handle_custom_command(args, custom_command, parser)
    else
      handle_message(args, parser)
    end
  end
end