class RHC::AutoCompleteBindings

Attributes

commands[R]
global_options[R]
top_level_commands[R]

Public Class Methods

new(data) click to toggle source
# File lib/rhc/autocomplete.rb, line 23
def initialize(data)
  @commands = {}
  @top_level_commands = []

  data.runner.commands.each_pair do |name, cmd|
    next if cmd.summary.nil?
    next if cmd.deprecated(name)

    if cmd.root?
      if cmd.name == name
        @top_level_commands << name
      end
    else
      @top_level_commands << name if name == cmd.name
      commands = name.split ' '
      action = commands.pop
      id = commands.join(' ')
      v = @commands[id] || {:actions => [], :switches => []}
      v[:actions] << action unless id == '' && name != cmd.name
      @commands[id] = v
    end

    v = @commands[name.to_s] || {:actions => [], :switches => []}
    v[:switches].concat(cmd.options.map do |o| 
      if o[:switches] 
        s = o[:switches][-1].split(' ')[0]
        if m = %r--\[no-\](.+)/.match(s)
          s = ["--#{m[1]}", "--no-#{m[1]}"]
        else
          s
        end
      end
    end.flatten.compact.sort)
    @commands[name.to_s] = v
  end
  @commands.delete('')
  @commands = @commands.to_a.sort{ |a,b| a[0] <=> b[0] }

  @top_level_commands.sort!

  @global_options = data.runner.options.map{ |o| o[:switches][-1].split(' ')[0] }.sort
end