Class: Foobara::CommandConnectors::ShCliConnector::InputsParser::OptionSet

Inherits:
Object
  • Object
show all
Defined in:
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option_set.rb

Instance Method Summary collapse

Instance Method Details

#<<(option) ⇒ Object



10
11
12
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option_set.rb', line 10

def <<(option)
  options << option
end

#optionsObject



6
7
8
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option_set.rb', line 6

def options
  @options ||= []
end

#prepare_parser(result_source) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option_set.rb', line 14

def prepare_parser(result_source)
  parser = result_source.parser
  long_option_paths = options.map(&:full_path)
  short_options_used = Set.new

  max_width = 30

  options.each do |option|
    args = option.to_args(short_options_used, long_option_paths)

    width = args.first.size

    if width > max_width
      max_width = width
    end

    parser.on(*args) do |value|
      h = result_source.result.parsed

      option.prefix.each do |key|
        # TODO: should we support writing to arrays by index? If so we need to check inputs type to figure
        # out if we need to create a hash or an array here
        h = h[key] ||= {}
      end

      result_source.current_array = if option.array?
                                      h[option.attribute_name] = [value]
                                    else
                                      h[option.attribute_name] = value
                                      nil
                                    end
    end
  end

  parser.set_summary_width(max_width + 2)
end