Sha256: 1d4cf63033ca863985ef47799c24a34eec36ae4b24f46f297f431046f92dd106

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'optparse'

module EacCli
  class Parser
    class OptionsCollection
      enable_simple_cache
      common_constructor(:definition, :argv, :collector) { collect }
      attr_reader :arguments

      def options_first?
        definition.options_first? || definition.subcommands?
      end

      private

      def check_required_options
        definition.options.each do |option|
          next unless option.required?
          next if collector.supplied?(option)

          raise ::EacCli::Parser::Error.new(
            definition, argv, "Option \"#{option}\" is required and a value was not supplied"
          )
        end
      end

      def collect
        build_banner
        build_options
        parse_argv
        check_required_options
      end

      def option_parser_uncached
        ::OptionParser.new
      end

      def parse_argv
        @arguments = options_first? ? option_parser.order(argv) : option_parser.parse(argv)
      rescue ::OptionParser::InvalidOption => e
        raise ::EacCli::Parser::Error.new(definition, argv, e.message)
      end

      def build_banner
        option_parser.banner = definition.help_formatter.to_banner
      end

      def build_options
        definition.options.each do |option|
          build_option(option)
        end
      end

      def build_option(option)
        option_parser.on(
          *[::EacCli::Definition::HelpFormatter.option_short(option),
            ::EacCli::Definition::HelpFormatter.option_long(option),
            option.description].reject(&:blank?)
        ) do |value|
          collector.collect(option, value)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
ehbrs-tools-0.16.1 vendor/eac_cli/lib/eac_cli/parser/options_collection.rb
eac_cli-0.11.1 lib/eac_cli/parser/options_collection.rb
avm-tools-0.76.1 vendor/eac_cli/lib/eac_cli/parser/options_collection.rb
eac_cli-0.11.0 lib/eac_cli/parser/options_collection.rb