Class: GLI::CommandFinder
- Inherits:
-
Object
- Object
- GLI::CommandFinder
- Defined in:
- lib/gli/command_finder.rb
Instance Method Summary collapse
-
#find_command(name) ⇒ Object
Finds the command with the given name, allowing for partial matches.
-
#initialize(commands, default_command) ⇒ CommandFinder
constructor
Initialize a finder on the given list of commands, using default_command as the default if none found.
Constructor Details
#initialize(commands, default_command) ⇒ CommandFinder
Initialize a finder on the given list of commands, using default_command as the default if none found
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/gli/command_finder.rb', line 4 def initialize(commands,default_command) @default_command = default_command @names_to_commands = {} commands.each do |command_name,command| @names_to_commands[command_name.to_s] = command Array(command.aliases).each do |command_alias| @names_to_commands[command_alias.to_s] = command end end end |
Instance Method Details
#find_command(name) ⇒ Object
Finds the command with the given name, allowing for partial matches. Returns the command named by the default command if no command with name
matched
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gli/command_finder.rb', line 17 def find_command(name) name ||= @default_command raise UnknownCommand.new("No command name given nor default available") if String(name).strip == '' command_found = @names_to_commands.fetch(name.to_s) do |command_to_match| find_command_by_partial_name(@names_to_commands, command_to_match) end if Array(command_found).empty? raise UnknownCommand.new("Unknown command '#{name}'") elsif command_found.kind_of? Array raise AmbiguousCommand.new("Ambiguous command '#{name}'. It matches #{command_found.sort.join(',')}") end command_found end |