Class: Parabot::CLI::CommandRouter
- Inherits:
-
Object
- Object
- Parabot::CLI::CommandRouter
- Defined in:
- lib/parabot/cli/command_router.rb
Overview
Routes commands to appropriate handlers based on command type
Instance Method Summary collapse
- #find_builtin_command(args) ⇒ Object
- #find_custom_command(args) ⇒ Object
-
#initialize(registry) ⇒ CommandRouter
constructor
A new instance of CommandRouter.
- #route_command(args, parser) ⇒ Object
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 = parser.(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([:config], [:project_root]) end if builtin_command handle_builtin_command(args, builtin_command, parser) elsif args.empty? || parser.(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 (args, parser) end end end |