class EasyOptionParser

Attributes

options[R]
title[R]

Public Class Methods

new(title, arguments) click to toggle source
# File lib/easy_opt_parser.rb, line 7
def initialize(title, arguments)
  @options = {}
  @title = title
  assert_valid_arguments!(arguments, symbol: :name)
  assert_valid_arguments!(arguments, symbol: :short_option)
  assert_valid_arguments!(arguments, symbol: :long_option)
  populate_with_default_values(arguments)

  parser = OptionParser.new do |opts|
    opts.banner = @title
    create_value_arguments(opts, arguments.reject {|arg| arg.is_a?(EOP::FlagArgument)})
    create_flag_arguments(opts, arguments.select { |arg| arg.is_a?(EOP::FlagArgument) })
    create_help_argument(opts)
  end
  parser.parse!

  assert_required_arguments!(arguments)
end

Public Instance Methods

contains?(name) click to toggle source
# File lib/easy_opt_parser.rb, line 43
def contains?(name)
  @options.include?(name.to_sym)
end
create_flag_arguments(opts, arguments) click to toggle source
# File lib/easy_opt_parser.rb, line 26
def create_flag_arguments(opts, arguments)
  arguments.each do |arg|
    argument_with(opts, arg.name, arg.short_option, arg.long_option, arg.description)
  end
end
create_long_option_from(argument) click to toggle source
# File lib/easy_opt_parser.rb, line 39
def create_long_option_from(argument)
  "#{argument.long_option} #{argument.name}"
end
create_value_arguments(opts, arguments) click to toggle source
# File lib/easy_opt_parser.rb, line 32
def create_value_arguments(opts, arguments)
  arguments.each do |arg|
    long_option = create_long_option_from(arg)
    argument_with(opts, arg.name, arg.short_option, long_option, arg.description)
  end
end

Private Instance Methods

argument_with(opts, name, short, long, description) click to toggle source
# File lib/easy_opt_parser.rb, line 63
def argument_with(opts, name, short, long, description)
  opts.on(short, long, description) do |value|
    @options[name.to_sym] = value
  end
end
assert_required_arguments!(arguments) click to toggle source
# File lib/easy_opt_parser.rb, line 76
def assert_required_arguments!(arguments)
  required_arguments = arguments.select(&:required)
  required_arguments.each do |arg|
    raise "Missing Argument [#{arg.name}]" if @options[arg.name.to_sym].to_s.empty?
  end
end
assert_valid_arguments!(arguments, symbol:) click to toggle source
# File lib/easy_opt_parser.rb, line 49
def assert_valid_arguments!(arguments, symbol:)
  raise 'Arguments must be an Array' unless arguments.is_a?(Array)
  raise 'Arguments cannot be nil' if arguments.nil?
  raise 'Arguments cannot be empty' if arguments.empty?
  argument_names = arguments.map(&symbol)
  raise 'Duplicated value in options' if argument_names.detect{|a| argument_names.count(a) > 1}
end
create_help_argument(opts) click to toggle source
# File lib/easy_opt_parser.rb, line 69
def create_help_argument(opts)
  opts.on('-h', '--help', 'Displays Help') do
    puts opts
    exit
  end
end
populate_with_default_values(arguments) click to toggle source
# File lib/easy_opt_parser.rb, line 57
def populate_with_default_values(arguments)
  arguments.each do |argument|
    @options[argument.name.to_sym] = argument.default_value
  end
end