Class: RVC::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rvc/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns, name, summary, parser) ⇒ Command

Returns a new instance of Command.



29
30
31
32
33
34
35
# File 'lib/rvc/command.rb', line 29

def initialize ns, name, summary, parser
  @ns = ns
  @name = name
  @summary = summary
  @parser = parser
  @completor = nil
end

Instance Attribute Details

#completorObject

Returns the value of attribute completor.



27
28
29
# File 'lib/rvc/command.rb', line 27

def completor
  @completor
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/rvc/command.rb', line 26

def name
  @name
end

#nsObject (readonly)

Returns the value of attribute ns.



26
27
28
# File 'lib/rvc/command.rb', line 26

def ns
  @ns
end

#parserObject (readonly)

Returns the value of attribute parser.



26
27
28
# File 'lib/rvc/command.rb', line 26

def parser
  @parser
end

#summaryObject (readonly)

Returns the value of attribute summary.



26
27
28
# File 'lib/rvc/command.rb', line 26

def summary
  @summary
end

Instance Method Details

#complete(word, args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/rvc/command.rb', line 45

def complete word, args
  if @completor
    candidates = @completor.call word, args
    prefix_regex = /^#{Regexp.escape word}/
    candidates.select { |x,a| x =~ prefix_regex }
  else
    return @ns.shell.completion.fs_candidates(word) +
           long_option_candidates(word)
  end
end

#inspectObject



37
38
39
# File 'lib/rvc/command.rb', line 37

def inspect
  "#<RVC::Command:#{name}>"
end

#invoke(*args) ⇒ Object



41
42
43
# File 'lib/rvc/command.rb', line 41

def invoke *args
  @ns.slate.send @name, *args
end

#long_option_candidates(word) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rvc/command.rb', line 56

def long_option_candidates word
  return [] unless parser.is_a? RVC::OptionParser
  prefix_regex = /^#{Regexp.escape(word)}/
  parser.specs.map { |k,v| "--#{v[:long]}" }.
               grep(prefix_regex).sort.
               map { |x| [x, ' '] }
end