Sha256: a50d049e7d54832115e217f100e835900f2cd65e4e0c70279f1a7ab52b59830b

Contents?: true

Size: 2 KB

Versions: 14

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

require 'eac_cli/parser/collector'
require 'eac_cli/parser/error'
require 'eac_ruby_utils/core_ext'

module EacCli
  class Parser
    class Alternative
      require_sub __FILE__, include_modules: true
      enable_listable
      lists.add_symbol :phase, :any, :option_argument, :positional
      attr_reader :error

      common_constructor :alternative, :argv do
        alternative.assert_argument(::EacCli::Definition::Alternative, :alternative)
        self.phase = PHASE_ANY
        collect
      end

      def error?
        error.present?
      end

      def success?
        !error?
      end

      def parsed
        @parsed ||= collector.to_data.freeze
      end

      private

      attr_accessor :phase

      def any_collect_argv_value
        if argv_current_option?
          option_collect_argv_value
        else
          positional_collect_argv_value
        end
      end

      def collector
        @collector ||= ::EacCli::Parser::Collector.new(alternative)
      end

      def collect
        loop do
          break unless argv_pending?

          collect_argv_value
        end
        validate
      rescue ::EacCli::Parser::Error => e
        @error = e
      end

      def collect_argv_value
        send("#{phase}_collect_argv_value")
        argv_enum.next
      end

      def collect_option_argv_value
        alternative.options.each do |option|
        end

        raise ::EacCli::Parser::Error.new(
          alternative, argv, "Invalid option: #{argv_enum.current}"
        )
      end

      def raise_error(message)
        raise ::EacCli::Parser::Error.new(alternative, argv, message)
      end

      def validate
        (alternative.options + alternative.positional).each do |option|
          validate_option(option)
        end
      end

      def validate_option(option)
        return unless option.required?
        return if collector.supplied?(option)

        raise_error("Required option/positional #{option} not supplied")
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
ehbrs-tools-0.17.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.86.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.85.1 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.85.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
ehbrs-tools-0.16.5 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.84.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.83.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
eac_cli-0.12.4 lib/eac_cli/parser/alternative.rb
ehbrs-tools-0.16.4 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.82.1 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.82.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
avm-tools-0.81.0 vendor/eac_cli/lib/eac_cli/parser/alternative.rb
eac_cli-0.12.3 lib/eac_cli/parser/alternative.rb
ehbrs-tools-0.16.3 vendor/eac_cli/lib/eac_cli/parser/alternative.rb