class Eco::CLI::Config::OptionsSet
Attributes
core_config[R]
Public Class Methods
new(core_config:)
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 11 def initialize(core_config:) @core_config = core_config @sets = {} end
Public Instance Methods
active_namespaces()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 99 def active_namespaces @active_namespaces ||= [].tap do |active| active << :general other = (namespaces - [:general]).select {|nm| SCR.arg?(nm)} active.concat(other) end end
active_options()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 73 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)
click to toggle source
@param option [String, Array<String>] the command line option(s). @param namespace [String] preceding command(s) argument that enables this option. @param desc [String] description of the option.
# File lib/eco/cli/config/options_set.rb, line 47 def add(option, desc = nil, namespace: :general) raise "Missing block to define the options builder" unless block_given? opts = [option].flatten.compact unless opts.empty? callback = Proc.new opts.each do |opt| puts "Overriding option '#{option}' in '#{namespace}' namespace" if option_exists?(opt, namespace) options_set(namespace)[opt] = OptConfig.new(opt, namespace, desc, callback) end end self end
all_options()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 83 def all_options sets.each_with_object([]) do |(namespace, opts_set), options| options << opts_set.values end end
any_non_general_space_active?()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 95 def any_non_general_space_active? (active_namespaces - [:general]).length > 0 end
help(refine: nil)
click to toggle source
@return [String] summary of the options.
# 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}:"].yield_self 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| !refine.is_a?(String) || option.name.include?(refine) end.each do |arg, option| lines << help_line(" " * indent + "#{option.name}", option.description, max_len) end end lines end.join("\n") end
namespaces()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 89 def namespaces sets.keys.sort_by do |key| key == :general end end
options_args(namespaces)
click to toggle source
@return [Array<String>] all the argument of the options in `namespaces`
# File lib/eco/cli/config/options_set.rb, line 38 def options_args(namespaces) namespaces.each_with_object([]) do |space, args| args.concat(options_set(space).keys) end.uniq end
process(io:)
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 61 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 active_options.each do |option| option.callback.call(io.options, io.session) end io.options end
Private Instance Methods
active_namespace?(namespace)
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 110 def active_namespace?(namespace) (namespace == :general) || SCR.get_arg(namespace) end
active_option?(opt, namespace = :general)
click to toggle source
Is the option active?
1. If :general namespace, it does just a direct check 2. Otherwise, the `namespace` wording should come first in the `cli` or it is considered inactive
# File lib/eco/cli/config/options_set.rb, line 117 def active_option?(opt, namespace = :general) if namespace == :general SCR.get_arg(opt) else active_namespace?(namespace) && SCR.arg_order?(namespace, opt) && SCR.get_arg(opt) end end
option_exists?(opt, namespace = :general)
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 125 def option_exists?(opt, namespace = :general) options_set(namespace).key?(opt) end
options_set(namespace = :general)
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 139 def options_set(namespace = :general) @sets[namespace] ||= {} end
sets()
click to toggle source
# File lib/eco/cli/config/options_set.rb, line 129 def sets @sets ||= { general: {} } end