Sha256: 55383f537318451e6af11f7b2ab8d061234abca1b487c3937caddd03719d8b58

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

class RakeCommander
  module Options
    # Relies between OptionParser and RakeCommander errors
    class ErrorRely < StandardError
      extend RakeCommander::Options::Name

      OPTION_REGEX = /(?:argument|option): (?<option>.+)/i.freeze

      def initialize(value)
        case value
        when OptionParser::MissingArgument, OptionParser::InvalidArgument
          super(value.message)
        when String
          super(value)
        else
          raise ArgumentError, "Expecting String or OptionParser error. Given: #{value.class}"
        end
      end

      def name?
        option_sym.to_s.length > 1
      end

      def short?
        option_sym.to_s.length == 1
      end

      def option_sym
        return @option_sym if @option_sym
        return nil unless match = message.match(self.class::OPTION_REGEX)
        option = match[:option]
        @option_sym = \
          if option.length > 1
            self.class.name_word_sym(option)
          else
            self.class.short_sym(option)
          end
      end

      private

      def to_description(value)
        case value
        when Hash
          to_description(value.values.uniq)
        when Array
          value.map do |v|
            to_description(v)
          end.join(', ')
        when RakeCommander::Option
          "#{value.name_hyphen} (#{value.short_hyphen})"
        else
          value
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rake-commander-0.1.4 lib/rake-commander/options/error_rely.rb
rake-commander-0.1.2 lib/rake-commander/options/error_rely.rb