Class: Tabry::OptionsFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/tabry/options_finder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, params) ⇒ OptionsFinder

Returns a new instance of OptionsFinder.



14
15
16
17
# File 'lib/tabry/options_finder.rb', line 14

def initialize(result, params)
  @result = result
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/tabry/options_finder.rb', line 7

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/tabry/options_finder.rb', line 7

def result
  @result
end

Class Method Details

.options(result, token, params) ⇒ Object

Returns an array of options



10
11
12
# File 'lib/tabry/options_finder.rb', line 10

def self.options(result, token, params)
  new(result, params).options(token)
end

Instance Method Details

#options(token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tabry/options_finder.rb', line 19

def options(token)
  # Bit of a hack: send this down to autocomplete shell commands
  # TODO: set this only in ShellOption -- would require passing state down on thru
  before_env = ENV.fetch("TABRY_AUTOCOMPLETE_STATE", nil)
  ENV["TABRY_AUTOCOMPLETE_STATE"] = {
    cmd: result.config.cmd,
    flags: result.state.flags,
    args: result.state.args,
    current_token: token,
    current_flag: result.state.current_flag
  }.to_json

  send(
    { subcommand: :options_subcommand, flagarg: :options_flagarg }[state.mode],
    token || ""
  )
ensure
  ENV["TABRY_AUTOCOMPLETE_STATE"] = before_env
end

#summary_descriptionsObject

Descriptions of what is expected; particularly, argument names and descriptions returns an array, each can be one of three things:

symbol :flag or :subcommand
array [name, description]
array [:flagarg, flag_description]


44
45
46
47
48
49
# File 'lib/tabry/options_finder.rb', line 44

def summary_descriptions
  send({
    subcommand: :summary_descriptions_subcommand,
    flagarg: :summary_descriptions_flagarg
  }[state.mode])
end

#summary_descriptions_flagargObject



51
52
53
54
55
# File 'lib/tabry/options_finder.rb', line 51

def summary_descriptions_flagarg
  result.current_flags_for_flagargs.map do |flag|
    [:flagarg, flag.description]
  end.compact
end

#summary_descriptions_subcommandObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tabry/options_finder.rb', line 57

def summary_descriptions_subcommand
  descriptions = []
  if result.expected_arg
    descriptions << [
      result.expected_arg.title || result.expected_arg.name,
      result.expected_arg.description
    ]
  end
  descriptions << :subcommand if options_subcommand_subs("").any?
  descriptions << :flag if options_subcommand_flags("").any?
  descriptions
end