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

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

Defined Under Namespace

Classes: OptConfig

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core_config:) ⇒ OptionsSet

Returns a new instance of OptionsSet.



11
12
13
14
# File 'lib/eco/cli/config/options_set.rb', line 11

def initialize(core_config:)
  @core_config = core_config
  @sets        = {}
end

Instance Attribute Details

#core_configObject (readonly)

Returns the value of attribute core_config.



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

def core_config
  @core_config
end

Instance Method Details

#active_namespacesObject



120
121
122
123
124
125
126
# File 'lib/eco/cli/config/options_set.rb', line 120

def active_namespaces
  @active_namespaces ||= [].tap do |active|
    active << :general
    other = (namespaces - [:general]).select {|nm| SCR.arg?(nm)}
    active.concat(other)
  end
end

#active_optionsObject

Options that have been invoked via CLI



93
94
95
96
97
98
99
100
101
# File 'lib/eco/cli/config/options_set.rb', line 93

def active_options
  @active_options ||= sets.select do |namespace, _opts_set|
    active_namespace?(namespace)
  end.each_with_object([]) do |(namespace, opts_set), options|
    opts_set.each do |arg, option|
      options << option if active_option?(arg, namespace)
    end
  end
end

#add(option, desc = nil, namespace: :general, &block) ⇒ Object

Parameters:

  • option (String, Array<String>)

    the command line option(s).

  • namespace (String) (defaults to: :general)

    preceding command(s) argument that enables this option.

  • desc (String) (defaults to: nil)

    description of the option.



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

def add(option, desc = nil, namespace: :general, &block)
  raise "Missing block to define the options builder" unless block_given?

  opts = [option].flatten.compact
  return self if opts.empty?

  callback = block
  opts.each do |opt|
    msg = "Overriding CLI option '#{option}' in '#{namespace}' CLI case / namespace"
    puts msg if option_exists?(opt, namespace)
    options_set(namespace)[opt] = OptConfig.new(opt, namespace, desc, callback)
  end

  self
end

#all_optionsObject



103
104
105
106
107
# File 'lib/eco/cli/config/options_set.rb', line 103

def all_options
  sets.each_with_object([]) do |(_namespace, opts_set), options|
    options << opts_set.values
  end
end

#any_non_general_space_active?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/eco/cli/config/options_set.rb', line 116

def any_non_general_space_active?
  (active_namespaces - [:general]).length.positive?
end

#available(keys: false) ⇒ Object

Options that are known for active namespaces (CLI-present)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/eco/cli/config/options_set.rb', line 50

def available(keys: false)
  sets.select do |namespace, _opts_set|
    active_namespace?(namespace)
  end.each_value.with_object([]) do |opts_set, options|
    options.concat(opts_set.values)
  end.then do |options|
    next options unless keys

    options.map(&:name)
  end
end

#help(refine: nil) ⇒ String

Returns summary of the options.

Returns:

  • (String)

    summary of the options.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eco/cli/config/options_set.rb', line 17

def help(refine: nil)
  indent     = 2
  spaces     = any_non_general_space_active? ? active_namespaces : namespaces
  refinement = refine.is_a?(String)? " (containing: '#{refine}')" : ""

  ["The following are the available options#{refinement}:"].then do |lines|
    max_len = keys_max_len(options_args(spaces)) + indent

    spaces.each do |namespace|
      is_general = (namespace == :general)
      str_indent = is_general ? "" : " " * indent

      lines << help_line(namespace, "", max_len) unless is_general

      options_set(namespace).select do |_arg, option| # rubocop:disable Style/HashEachMethods
        !refine.is_a?(String) || option.name.include?(refine)
      end.each do |_arg, option|
        lines << help_line("#{str_indent}#{option.name}", option.description, max_len)
      end
    end

    lines
  end.join("\n")
end

#namespacesObject



109
110
111
112
113
114
# File 'lib/eco/cli/config/options_set.rb', line 109

def namespaces
  sets.keys.sort_by do |key|
    next 1 if key == :general
    0
  end
end

#options_args(namespaces) ⇒ Array<String>

Returns all the argument of the options in namespaces.

Returns:

  • (Array<String>)

    all the argument of the options in namespaces



43
44
45
46
47
# File 'lib/eco/cli/config/options_set.rb', line 43

def options_args(namespaces)
  namespaces.each_with_object([]) do |space, args|
    args.concat(options_set(space).keys)
  end.uniq
end

#process(io:) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
# File 'lib/eco/cli/config/options_set.rb', line 81

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

  active_options.each do |option|
    option.callback.call(io.options, io.session)
  end

  io.options
end