Sha256: 5611a437d8445cb7f8280d26bbee7bb50963a1338759f5e3da10d1755f89cd33

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module EacCli
  class Definition
    class Option
      class InitializeArgsParser
        PROPERTIES = %i[short long description options].freeze
        attr_reader(*PROPERTIES)

        def initialize(args)
          self.options = args.extract_options!.freeze
          args.each { |arg| absorb_arg(arg) }
        end

        private

        attr_writer(*PROPERTIES)

        def absorb_arg(arg)
          arg_ext = ArgumentParser.new(arg)
          send("#{arg_ext.type}=", arg_ext.value)
        end

        class ArgumentParser
          TYPES = %i[short long description].freeze
          common_constructor :value

          def type
            TYPES.find { |type| send("#{type}?") } ||
              raise(::EacCli::Definition::Error, "Unknown type for \"#{value}\"")
          end

          def short?
            value.start_with?('-') && !long?
          end

          def long?
            value.start_with?('--')
          end

          def description?
            !short? || !long?
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
eac_cli-0.42.0 lib/eac_cli/definition/option/initialize_args_parser.rb
eac_cli-0.40.1 lib/eac_cli/definition/option/initialize_args_parser.rb
eac_cli-0.40.0 lib/eac_cli/definition/option/initialize_args_parser.rb
eac_cli-0.39.0 lib/eac_cli/definition/option/initialize_args_parser.rb
eac_cli-0.38.1 lib/eac_cli/definition/option/initialize_args_parser.rb
eac_tools-0.82.0 sub/eac_cli/lib/eac_cli/definition/option/initialize_args_parser.rb