Sha256: bc6d95ff9f1ee68117fa632b7184fbf289148b46c0eb4d489d0b5141341b4844

Contents?: true

Size: 676 Bytes

Versions: 3

Compression:

Stored size: 676 Bytes

Contents

# frozen_string_literal: true

require 'optparse'

# Initializes the OptionParser instance using the given configuration
class Runger::OptionParserBuilder
  class << self
    def call(options)
      OptionParser.new do |opts|
        options.each do |key, descriptor|
          opts.on(*option_parser_on_args(key, **descriptor)) do |val|
            yield([key, val])
          end
        end
      end
    end

    private

    def option_parser_on_args(key, flag: false, desc: nil, type: ::String)
      on_args = ["--#{key.to_s.tr('_', '-')}#{flag ? '' : ' VALUE'}"]
      on_args << type unless flag
      on_args << desc unless desc.nil?
      on_args
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
runger_config-5.2.0 lib/runger/option_parser_builder.rb
runger_config-5.1.0 lib/runger/option_parser_builder.rb
runger_config-5.0.0 lib/runger/option_parser_builder.rb