Sha256: 6da0119e49cd261d238f293e9ff10e86957a5ae398adce468a3bdffc1b0f4769

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'optparse'
require 'yaml'

module Balboa
  module CLI
    class Options
      ERROR_EXIT_CODE   = 1
      BALBOA_FILE       = '.balboa'.freeze

      MissingOptionError = Class.new(RuntimeError)

      def self.parse(shifted_argv=[])
        new(shifted_argv).parse
      rescue OptionParser::InvalidOption => error
        $stderr.puts "Error: #{error}"
        exit ERROR_EXIT_CODE
      end

      def initialize(shifted_argv)
        @parser = OptionParser.new(shifted_argv)
        @config = Hash.new { fail MissingOptionError }

        configure_parser
      end

      def parse
        @parser.parse!

        @config
      end

      private

      def configure_parser
        load_balboa_file
        load_config_file
        load_params
      end

      def load_balboa_file
        @config.merge!(YAML.load_file(BALBOA_FILE)) if File.exists?(BALBOA_FILE)
      end
      
      def load_config_file
        @parser.on('-c', '--config FILE') do |file|
          @config.merge!(YAML.load_file(file)) if File.exists?(file)
        end
      end

      def load_params
        @parser.on('-d', '--data PARAMS') do |data|
          @config.merge!(data)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
balboa-0.1.1 lib/balboa/cli/options.rb
balboa-0.1.0 lib/balboa/cli/options.rb