lib/eco/cli/config/options_set.rb in eco-helpers-2.7.16 vs lib/eco/cli/config/options_set.rb in eco-helpers-2.7.17

- old
+ new

@@ -1,35 +1,37 @@ module Eco class CLI class Config class OptionsSet include Eco::CLI::Config::Help + attr_reader :core_config - class OptConfig < Struct.new(:name, :namespace, :description, :callback) - end + OptConfig = Struct.new(:name, :namespace, :description, :callback) def initialize(core_config:) @core_config = core_config @sets = {} end # @return [String] summary of the options. def help(refine: nil) - indent = 2 - spaces = any_non_general_space_active? ? active_namespaces : namespaces + 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| + + ["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| + + 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(" " * indent + "#{option.name}", option.description, max_len) + end.each do |_arg, option| + lines << help_line("#{str_indent}#{option.name}", option.description, max_len) end end lines end.join("\n") end @@ -39,74 +41,89 @@ namespaces.each_with_object([]) do |space, args| args.concat(options_set(space).keys) end.uniq end + # Options that are known for active namespaces (CLI-present) + 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 + # @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. def add(option, desc = nil, namespace: :general, &block) raise "Missing block to define the options builder" unless block_given? opts = [option].flatten.compact unless opts.empty? callback = block opts.each do |opt| - puts "Overriding CLI option '#{option}' in '#{namespace}' CLI case / namespace" if option_exists?(opt, namespace) + 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 end + self end 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 + 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 + # Options that have been invoked via CLI def active_options - @active_options ||= sets.select do |namespace, opts_set| + @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 def all_options - sets.each_with_object([]) do |(namespace, opts_set), options| + sets.each_with_object([]) do |(_namespace, opts_set), options| options << opts_set.values end end def namespaces sets.keys.sort_by do |key| - key == :general + next 1 if key == :general + 0 end end def any_non_general_space_active? - (active_namespaces - [:general]).length > 0 + (active_namespaces - [:general]).length.positive? end def active_namespaces @active_namespaces ||= [].tap do |active| active << :general other = (namespaces - [:general]).select {|nm| SCR.arg?(nm)} active.concat(other) end end - private def active_namespace?(namespace) (namespace == :general) || SCR.get_arg(namespace) end @@ -128,13 +145,9 @@ def sets @sets ||= { general: {} } - end - - def namespaces - @sets.keys end def options_set(namespace = :general) @sets[namespace] ||= {} end