Sha256: eba401b6064e5f2db5372a500194a59c6e24bb3e9d3cc6c31bea6c982c0a06fe

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'optparse'

module Racli
  class OptionParser
    def call(args)
      options, args = parse(args)
      config_params = { config: options[:config], rcfile: options[:rcfile] }

      params = { method: 'GET', path: '/' }
      params[:method] = args[0] unless args.empty?
      params[:path] = args[1] if args.length > 1

      request_params = options.dup
      request_params.delete(:config)
      request_params.delete(:rcfile)
      params[:params] = request_params

      [config_params, params]
    end

    private

    def parse(args)
      keys = args.select { |key| key.include?('--') }
                 .map { |key| key.gsub(/^\-\-/, '') }
                 .reject { |key| %w[config rcfile].include?(key) }
      options = {}
      options[:config] = File.expand_path('./config.ru', Dir.pwd)
      options[:rcfile] = File.expand_path('./.raclirc', Dir.pwd)

      parser = ::OptionParser.new
      parser.on('-c', '--config VALUE') { |v| options[:config] = v }
      parser.on('--rcfile VALUE') { |v| options[:rcfile] = v }
      keys.each do |key|
        parser.on("--#{key} VALUE") { |v| options[key.to_s] = v }
      end

      args = parser.parse(ARGV)
      [options, args]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
racli-0.1.2 lib/racli/option_parser.rb