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



56
57
58
# File 'lib/tabtab/cli.rb', line 56

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
31
32
33
34
# File 'lib/tabtab/cli.rb', line 17

def execute(stdout, arguments=[])
  @stdout = stdout
  # require "shellwords"
  # @full_line = ENV['COMP_LINE']
  # @full_line_argv = Shellwords.shellwords(@full_line)
  @app_name, @current_token, @previous_token = arguments[-3..-1]
  parse_options(arguments[0..-4])
  load_global_config
  if options[:external]
    process_external
  elsif options[:gem]
    process_gem
  elsif options[:file]
    process_file
  else
    usage
  end
end

#externalsObject



80
81
82
# File 'lib/tabtab/cli.rb', line 80

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

#load_global_configObject



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

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

#parse_options(arguments) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tabtab/cli.rb', line 36

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tabtab/cli.rb', line 65

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


96
97
98
# File 'lib/tabtab/cli.rb', line 96

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


88
89
90
# File 'lib/tabtab/cli.rb', line 88

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

#usageObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/tabtab/cli.rb', line 105

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