Class: TabTab::CLI
Instance Attribute Summary collapse
-
#current_token ⇒ Object
readonly
Returns the value of attribute current_token.
-
#full_line ⇒ Object
readonly
Returns the value of attribute full_line.
-
#global_config ⇒ Object
readonly
Returns the value of attribute global_config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#previous_token ⇒ Object
readonly
Returns the value of attribute previous_token.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
- #app_name ⇒ Object
- #execute(stdout, arguments = []) ⇒ Object
- #externals ⇒ Object
-
#extract_tokens_and_parse_options(arguments) ⇒ Object
parses the incoming tokens either via ENV or tokens from ARGV.
- #load_global_config ⇒ Object
- #parse_options(arguments) ⇒ Object
-
#process_external ⇒ Object
Support for external apps (optionally configured in ~/.tabtab.yml) Generates a completion list from the -h help output of the target application –external.
-
#process_file ⇒ Object
Support for file-based completion definitions (found in target file) –file /path/to/definition.rb.
-
#process_gem ⇒ Object
Support for RubyGem-based completion definitions (found in any gem path) –gem gem_name.
- #usage ⇒ Object
Methods included from LocalConfig
Instance Attribute Details
#current_token ⇒ Object (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_line ⇒ Object (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_config ⇒ Object (readonly)
Returns the value of attribute global_config.
9 10 11 |
# File 'lib/tabtab/cli.rb', line 9 def global_config @global_config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/tabtab/cli.rb', line 9 def @options end |
#previous_token ⇒ Object (readonly)
Returns the value of attribute previous_token.
10 11 12 |
# File 'lib/tabtab/cli.rb', line 10 def previous_token @previous_token end |
#stdout ⇒ Object (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_name ⇒ Object
68 69 70 |
# File 'lib/tabtab/cli.rb', line 68 def app_name [: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 (arguments) load_global_config if [:external] process_external elsif [:gem] process_gem elsif [:file] process_file else usage end end |
#externals ⇒ Object
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 (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] (arguments) else @app_name, @current_token, @previous_token = arguments[-3..-1] (arguments[0..-4]) end end |
#load_global_config ⇒ Object
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 (arguments) @options = {} OptionParser.new do |opts| opts. = "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| [:alias] = v end opts.on("--external", "Automatically import flags from application's -h flags") do |v| [:external] = v end opts.on("--gem GEM_NAME", "Load the tabtab definition from within target RubyGem") do |v| [:gem] = v end opts.on("--file FILE_NAME", "Load the tabtab definition from a specific file") do |v| [:file] = v end end.parse!(arguments) end |
#process_external ⇒ Object
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 = 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 = .nil? ? '-h' : stdout.puts TabTab::Completions::External.new(app_name, , global_config).starts_with(current_token) end |
#process_file ⇒ Object
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([:file], app_name, current_token, previous_token, global_config).extract.join("\n") end |
#process_gem ⇒ Object
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([:gem], app_name, current_token, previous_token, global_config).extract.join("\n") end |
#usage ⇒ Object
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 |