Class: TabTab::Completions::External

Inherits:
Object
  • Object
show all
Defined in:
lib/tabtab/completions/external.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, options_flag = '-h', global_config = {}) ⇒ External

Returns a new instance of External.



4
5
6
7
8
# File 'lib/tabtab/completions/external.rb', line 4

def initialize(app_name, options_flag = '-h', global_config = {})
  @app_name     = app_name
  @options_flag = options_flag
  @global_config  = global_config
end

Instance Attribute Details

#global_configObject (readonly)

Returns the value of attribute global_config.



2
3
4
# File 'lib/tabtab/completions/external.rb', line 2

def global_config
  @global_config
end

Instance Method Details

#extract(_options_str = nil) ⇒ Object

Returns list of long and short options for an application’s –help display which was passed into initializer



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tabtab/completions/external.rb', line 16

def extract(_options_str = nil)
  @options_str = _options_str if _options_str # hook for testing
  @extract ||= begin
    lines_containing_options = options_str.split(/\n/).grep(/^[\s\t]+-/)
    all_options = lines_containing_options.inject([]) do |list, line|
      list + line.scan(/(?:^\s+|,\s)(-[\w-]+)/).flatten
    end
    long_options  = all_options.grep(/^--/).sort
    short_options = hide_short_flags? ? [] : (all_options - long_options).sort
    long_options + short_options
  end
end

#hide_short_flags?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/tabtab/completions/external.rb', line 36

def hide_short_flags?
  global_config[:shortflags] == 'disable'
end

#options_strObject



10
11
12
# File 'lib/tabtab/completions/external.rb', line 10

def options_str
  @options_str ||= `#{@app_name} #{@options_flag}`
end

#starts_with(prefix) ⇒ Object

Returns the sub-list of all options filtered by a common prefix e.g. if current extract list is [‘–help’, ‘–extra’, ‘-h’, ‘-x’] then starts_with(‘–’) returns [‘–help’, ‘–extra’]



32
33
34
# File 'lib/tabtab/completions/external.rb', line 32

def starts_with(prefix)
  extract.grep(/^#{prefix}/)
end