Sha256: 6729cd64882fde6be415a631273d63ba6613b27d3c72862db1d2d23f83b92f47

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Cri
  # The definition of an option.
  class OptionDefinition
    attr_reader :short
    attr_reader :long
    attr_reader :desc
    attr_reader :argument
    attr_reader :multiple
    attr_reader :block
    attr_reader :hidden
    attr_reader :default
    attr_reader :transform

    def initialize(params = {})
      @short     = params.fetch(:short)
      @long      = params.fetch(:long)
      @desc      = params.fetch(:desc)
      @argument  = params.fetch(:argument)
      @multiple  = params.fetch(:multiple)
      @block     = params.fetch(:block)
      @hidden    = params.fetch(:hidden)
      @default   = params.fetch(:default)
      @transform = params.fetch(:transform)

      if @short.nil? && @long.nil?
        raise ArgumentError, 'short and long options cannot both be nil'
      end

      if @default && @argument == :forbidden
        raise ArgumentError, 'a default value cannot be specified for flag options'
      end
    end

    def to_h
      {
        short:     @short,
        long:      @long,
        desc:      @desc,
        argument:  @argument,
        multiple:  @multiple,
        block:     @block,
        hidden:    @hidden,
        default:   @default,
        transform: @transform,
      }
    end

    def formatted_name
      @long ? '--' + @long : '-' + @short
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cri-2.15.2 lib/cri/option_definition.rb
cri-2.15.1 lib/cri/option_definition.rb
cri-2.15.0 lib/cri/option_definition.rb
cri-2.14.0 lib/cri/option_definition.rb
cri-2.13.0 lib/cri/option_definition.rb
cri-2.12.0 lib/cri/option_definition.rb