Class: Eco::CLI::Config::OptionsSet

Inherits:
Object
  • Object
show all
Includes:
Help
Defined in:
lib/eco/cli/config/options_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core_config:) ⇒ OptionsSet

Returns a new instance of OptionsSet.



8
9
10
11
12
# File 'lib/eco/cli/config/options_set.rb', line 8

def initialize(core_config:)
  @core_config = core_config
  @options_set = {}
  @description = {}
end

Instance Attribute Details

#core_configObject (readonly)

Returns the value of attribute core_config.



6
7
8
# File 'lib/eco/cli/config/options_set.rb', line 6

def core_config
  @core_config
end

Instance Method Details

#add(option, desc = nil) ⇒ Object

Parameters:

  • option (String)

    the command line option.

  • desc (String) (defaults to: nil)

    description of the option.



27
28
29
30
31
32
33
34
35
# File 'lib/eco/cli/config/options_set.rb', line 27

def add(option, desc = nil)
  raise "Missing block to define the options builder" unless block_given?
  callback = Proc.new
  [option].flatten.compact.each do |opt|
    @options_set[opt] = callback
    @description[opt] = desc
  end
  self
end

#helpString

Returns summary of the options.

Returns:

  • (String)

    summary of the options.



15
16
17
18
19
20
21
22
23
# File 'lib/eco/cli/config/options_set.rb', line 15

def help
  ["The following are the available options:"].yield_self do |lines|
    max_len = keys_max_len(@options_set.keys)
    @options_set.keys.each do |key|
      lines << help_line(key, @description[key], max_len)
    end
    lines
  end.join("\n")
end

#process(io:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/eco/cli/config/options_set.rb', line 37

def process(io:)
  unless io && io.is_a?(Eco::API::UseCases::BaseIO)
    raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
  end

  @options_set.each do |arg, callback|
    callback.call(io.options, io.session) if SCR.get_arg(arg)
  end
  io.options
end