lib/balboa/cli/options.rb in balboa-0.1.2 vs lib/balboa/cli/options.rb in balboa-0.1.3

- old
+ new

@@ -1,59 +1,26 @@ # frozen_string_literal: true -require 'optparse' -require 'yaml' +require_relative 'parser_builder' 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 + def self.parse(*args) + new(*args).parse rescue OptionParser::InvalidOption => error $stderr.puts "Error: #{error}" - exit ERROR_EXIT_CODE + exit end - def initialize(shifted_argv) - @parser = OptionParser.new(shifted_argv) - @config = Hash.new { fail MissingOptionError } - - configure_parser + def initialize(argv=[], config={}) + @config = config + @parser = Balboa::CLI::ParserBuilder.create(argv, config) 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