Sha256: eefdaf8eb608591be2db7120a3d8fb3bab18c59b7fcccb14f3fea5384ccd05e8

Contents?: true

Size: 1.45 KB

Versions: 40

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacCli
  class Definition
    class BaseOption
      require_sub __FILE__

      class << self
        def from_args(args)
          p = ::EacCli::Definition::BaseOption::InitializeArgsParser.new(args)
          new(p.short, p.long, p.description, p.options)
        end
      end

      DEFAULT_REQUIRED = false

      enable_listable
      enable_abstract_methods :build_value, :default_value
      lists.add_symbol :option, :optional, :usage, :repeat, :required
      common_constructor :short, :long, :description, :options, default: [{}] do
        raise 'Nor short neither long selector was set' if short.blank? && long.blank?

        self.options = ::EacCli::Definition::BaseOption.lists.option.hash_keys_validate!(
          options.symbolize_keys
        )
      end

      def identifier
        [long, short].each do |v|
          v.to_s.if_present { |vv| return vv.variableize.to_sym }
        end

        raise 'No short or long option to build identifier'
      end

      def repeat?
        options[OPTION_REPEAT]
      end

      def required?
        return true if options.key?(:required) && options.fetch(:required)
        return false if options.key?(:optional) && options.fetch(:optional)

        DEFAULT_REQUIRED
      end

      def to_s
        "#{self.class.name.demodulize}[#{identifier}]"
      end

      def show_on_usage?
        options[:usage]
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 3 rubygems

Version Path
ehbrs-tools-0.26.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
avm-tools-0.105.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.25.1 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.25.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.24.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
eac_cli-0.22.2 lib/eac_cli/definition/base_option.rb
eac_cli-0.22.1 lib/eac_cli/definition/base_option.rb
avm-tools-0.104.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.23.1 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.23.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
avm-tools-0.103.1 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
eac_cli-0.22.0 lib/eac_cli/definition/base_option.rb
avm-tools-0.103.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
eac_cli-0.21.1 lib/eac_cli/definition/base_option.rb
ehbrs-tools-0.22.0 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
eac_cli-0.21.0 lib/eac_cli/definition/base_option.rb
eac_cli-0.20.4 lib/eac_cli/definition/base_option.rb
eac_cli-0.20.3 lib/eac_cli/definition/base_option.rb
avm-tools-0.102.2 vendor/eac_cli/lib/eac_cli/definition/base_option.rb
eac_cli-0.20.2 lib/eac_cli/definition/base_option.rb