Class: TabTab::CLI

Inherits:
Object
  • Object
show all
Includes:
LocalConfig
Defined in:
lib/tabtab/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LocalConfig

#config, #home

Instance Attribute Details

#current_tokenObject (readonly)

Returns the value of attribute current_token.



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

def current_token
  @current_token
end

#full_lineObject (readonly)

Returns the value of attribute full_line.



8
9
10
# File 'lib/tabtab/cli.rb', line 8

def full_line
  @full_line
end

#global_configObject (readonly)

Returns the value of attribute global_config.



9
10
11
# File 'lib/tabtab/cli.rb', line 9

def global_config
  @global_config
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/tabtab/cli.rb', line 9

def options
  @options
end

#previous_tokenObject (readonly)

Returns the value of attribute previous_token.



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

def previous_token
  @previous_token
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



7
8
9
# File 'lib/tabtab/cli.rb', line 7

def stdout
  @stdout
end

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



13
14
15
# File 'lib/tabtab/cli.rb', line 13

def self.execute(stdout, arguments=[])
  self.new.execute(stdout, arguments)
end

Instance Method Details

#app_nameObject



68
69
70
# File 'lib/tabtab/cli.rb', line 68

def app_name
  options[:alias] || @app_name
end

#execute(stdout, arguments = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tabtab/cli.rb', line 17

def execute(stdout, arguments=[])
  @stdout = stdout
  extract_tokens_and_parse_options(arguments)
  load_global_config
  if options[:external]
    process_external
  elsif options[:gem]
    process_gem
  elsif options[:file]
    process_file
  else
    usage
  end
end

#externalsObject



92
93
94
# File 'lib/tabtab/cli.rb', line 92

def externals
  config["external"] || config["externals"]
end

#extract_tokens_and_parse_options(arguments) ⇒ Object

parses the incoming tokens either via ENV or tokens from ARGV



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tabtab/cli.rb', line 33

def extract_tokens_and_parse_options(arguments)
  if ENV['COMP_LINE']
    require "shellwords"
    line  = ENV['COMP_LINE']
    words = Shellwords.shellwords(line)
    words << "" if line.split("")[-1] == " "
    @app_name = words[0]
    @previous_token, @current_token = words[-2..-1]
    parse_options(arguments)
  else
    @app_name, @current_token, @previous_token = arguments[-3..-1]
    parse_options(arguments[0..-4])
  end
end

#load_global_configObject



112
113
114
115
# File 'lib/tabtab/cli.rb', line 112

def load_global_config
  @global_config ||= {}
  @global_config[:shortflags] = config["shortflags"] || "enable"
end

#parse_options(arguments) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tabtab/cli.rb', line 48

def parse_options(arguments)
  @options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] app_name current_token previous_token"

    opts.on("--alias ALIAS", "Map an alias to an actual command with its own tabtab definition") do |v|
      options[:alias] = v
    end
    opts.on("--external", "Automatically import flags from application's -h flags") do |v|
      options[:external] = v
    end
    opts.on("--gem GEM_NAME", "Load the tabtab definition from within target RubyGem") do |v|
      options[:gem] = v
    end
    opts.on("--file FILE_NAME", "Load the tabtab definition from a specific file") do |v|
      options[:file] = v
    end
  end.parse!(arguments)
end

#process_externalObject

Support for external apps (optionally configured in ~/.tabtab.yml) Generates a completion list from the -h help output of the target application

--external


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tabtab/cli.rb', line 77

def process_external
  usage unless config
  # 'externals' => ['app1', 'app2', { '-?' => ['app3'] }]
  # Only look for the internal hashes, and return -? if app_name == 'app3', else nil
  options_flag = externals.inject(nil) do |o_flag, app_or_hash|
    next if app_or_hash.is_a?(String) || app_or_hash.is_a?(Symbol)
    app_or_hash.inject(nil) do |flag, flag_app_list|
      flag, app_list = flag_app_list
      flag if app_list.include?(app_name)
    end
  end
  options_flag = options_flag.nil? ? '-h' : options_flag
  stdout.puts TabTab::Completions::External.new(app_name, options_flag, global_config).starts_with(current_token)
end

#process_fileObject

Support for file-based completion definitions (found in target file)

--file /path/to/definition.rb


108
109
110
# File 'lib/tabtab/cli.rb', line 108

def process_file
  stdout.puts TabTab::Completions::File.new(options[:file], app_name, current_token, previous_token, global_config).extract.join("\n")
end

#process_gemObject

Support for RubyGem-based completion definitions (found in any gem path)

--gem gem_name


100
101
102
# File 'lib/tabtab/cli.rb', line 100

def process_gem
  stdout.puts TabTab::Completions::Gem.new(options[:gem], app_name, current_token, previous_token, global_config).extract.join("\n")
end

#usageObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/tabtab/cli.rb', line 117

def usage
  stdout.puts <<-EOS.gsub(/^      /, '')
  Invalid #{@app_type} flag provided to #{$0}. 
  USAGE: 
    #{$0} --external
    #{$0} --gem GEM_NAME
    #{$0} --file FILE_PATH
  EOS
  exit
end