Sha256: 73a01403c2041836ef0204c95020c2621c778700189d6f3a8b1eff3bb9a37981

Contents?: true

Size: 1.47 KB

Versions: 22

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module EacCli
  class Parser
    class Alternative
      module Options
        DOUBLE_DASH = '--'

        private

        attr_accessor :argument_option, :double_dash

        def argument_option_collect_argv(option)
          self.argument_option = option
          self.phase = PHASE_OPTION_ARGUMENT
        end

        def argv_current_option?
          phase == PHASE_ANY && argv_enum.peek.start_with?('-')
        end

        def argv_current_double_dash?
          argv_enum.peek == DOUBLE_DASH && !double_dash
        end

        def boolean_option_collect_argv(option)
          collector.collect(option, true)
        end

        def option_argument_collect_argv_value
          collector.collect(argument_option, argv_enum.peek)
          self.argument_option = nil
          self.phase = PHASE_ANY
        end

        def option_collect_argv_value
          return double_dash_collect_argv_value if argv_current_double_dash?

          alternative.options.any? do |option|
            next false unless [option.short, option.long].include?(argv_enum.peek)

            if option.argument?
              argument_option_collect_argv(option)
            else
              boolean_option_collect_argv(option)
            end

            true
          end || raise_argv_current_invalid_option
        end

        def raise_argv_current_invalid_option
          raise_error "Invalid option: \"#{argv_enum.peek}\""
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
avm-tools-0.77.0 vendor/eac_cli/lib/eac_cli/parser/alternative/options.rb
eac_cli-0.12.0 lib/eac_cli/parser/alternative/options.rb