Sha256: a909c72fca82c660b5684797ff445d8c10aaa3f1ee72b0e9df91b25ca557ad36

Contents?: true

Size: 1.57 KB

Versions: 15

Compression:

Stored size: 1.57 KB

Contents

module GLI
  class CommandFinder
    # Initialize a finder on the given list of commands, using default_command as the default if none found
    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

    # 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
    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

  private

    def find_command_by_partial_name(names_to_commands, command_to_match)
      partial_matches = names_to_commands.keys.select { |command_name| command_name =~ /^#{command_to_match}/ }
      return names_to_commands[partial_matches[0]] if partial_matches.size == 1
      partial_matches
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
gli-2.12.3 lib/gli/command_finder.rb
gli-2.12.2 lib/gli/command_finder.rb
gli-2.12.1 lib/gli/command_finder.rb
gli-2.12.0 lib/gli/command_finder.rb
gli-2.11.0 lib/gli/command_finder.rb
gli-2.10.0 lib/gli/command_finder.rb
gli-2.9.0 lib/gli/command_finder.rb
gli_aziz_light-2.8.1 lib/gli/command_finder.rb
gli-2.8.1 lib/gli/command_finder.rb
gli-2.8.0 lib/gli/command_finder.rb
gli-2.7.0 lib/gli/command_finder.rb
gli-2.6.2 lib/gli/command_finder.rb
gli-2.6.1 lib/gli/command_finder.rb
gli-2.6.0 lib/gli/command_finder.rb
gli-2.6.0.rc1 lib/gli/command_finder.rb