Sha256: 6032285fb7c80702a6985ab9ebe60eafa9db996e433df03a7b976ff1232ef5d5

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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('-p', '--password PASSWORD') do |password|
          @config.merge!('password' => password)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balboa-0.1.2 lib/balboa/cli/options.rb