Class: Executable::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/executable/completion.rb

Overview

Encpsulates command completion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class) ⇒ Completion

Setup new completion object.



10
11
12
13
14
15
# File 'lib/executable/completion.rb', line 10

def initialize(cli_class)
  @cli_class = cli_class

  @subcommands = nil
  @options     = nil
end

Instance Attribute Details

#cli_classObject (readonly)

The Executable subclass to which this help applies.



23
24
25
# File 'lib/executable/completion.rb', line 23

def cli_class
  @cli_class
end

Instance Method Details

#call(*args) ⇒ Object



55
56
57
# File 'lib/executable/completion.rb', line 55

def call(*args)
  puts self
end

#inspectObject



18
# File 'lib/executable/completion.rb', line 18

alias_method :inspect, :to_s

#optionsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/executable/completion.rb', line 36

def options
  @options ||= (
    method_list.map do |meth|
      case meth.name
      when /^(.*?)[\!\=]$/
        name = meth.name.to_s.chomp('!').chomp('=')
        mark = name.to_s.size == 1 ? '-' : '--'
        mark + name
      end
    end.compact.sort
  )
end

#subcommandsString, NilClass

List of subcommands converted to a printable string. But will return nil if there are no subcommands.

Returns:

  • (String, NilClass)

    subcommand list text



31
32
33
# File 'lib/executable/completion.rb', line 31

def subcommands
  @subcommands ||= @cli_class.subcommands.keys
end

#to_sObject



50
51
52
# File 'lib/executable/completion.rb', line 50

def to_s
  (subcommands + options).join(' ')
end