class OptparsePlus::OptPlus

Constants

RUBY_OPTIONS
RUBY_OPTION_TO_LABEL

Attributes

callbacks[R]
config[R]
config_source[R]
opt[W]

Public Class Methods

create_opt(config_yaml_source) click to toggle source
# File lib/optparse_plus.rb, line 66
def self.create_opt(config_yaml_source)
  opt_plus = OptPlus.new(config_yaml_source)

  if banner = opt_plus.config["banner"]
    opt = ::OptionParser.new(banner)
  else
    opt = ::OptionParser.new
  end

  opt.setup_opt_plus(opt_plus)
  opt
end
new(config_yaml_source) click to toggle source
# File lib/optparse_plus.rb, line 79
def initialize(config_yaml_source)
  @config_source = config_yaml_source
  @config = YAML.load(config_yaml_source)
  @ruby_option_callbacks = ruby_option_callbacks
  @callbacks = {}
  @opt = nil
end

Public Instance Methods

config_to_args(label, config=@config) click to toggle source
# File lib/optparse_plus.rb, line 97
def config_to_args(label, config=@config)
  options = config[label.to_s]
  %w(short long desc description).map {|type| options[type] }
    .select {|option| not option.nil? }.flatten
end
inherit_ruby_options(*short_option_names) click to toggle source
# File lib/optparse_plus.rb, line 103
def inherit_ruby_options(*short_option_names)
  short_option_names.each do |opt_name|
    label = RUBY_OPTION_TO_LABEL[opt_name]
    args = config_to_args(label, RUBY_OPTIONS)
    callback = @ruby_option_callbacks[label]
    @opt.on(*args, callback)
  end
end
opt_on() click to toggle source
# File lib/optparse_plus.rb, line 87
def opt_on
  @callbacks.keys.each {|label| reflect_callback(label) }
end
reflect_callback(label) click to toggle source
# File lib/optparse_plus.rb, line 91
def reflect_callback(label)
  callback = @callbacks[label]
  args = config_to_args(label)
  @opt.on(*args, callback)
end

Private Instance Methods

ruby_option_callbacks() click to toggle source
# File lib/optparse_plus.rb, line 124
def ruby_option_callbacks
  {}.tap do |callbacks|
    callbacks[:optplus_ruby_encoding] = proc {|given_opt| ruby_set_encoding(given_opt) }
    callbacks[:optplus_ruby_debug] = proc {|given_opt| ruby_set_debug }
  end
end
ruby_set_debug() click to toggle source
# File lib/optparse_plus.rb, line 120
def ruby_set_debug
  $DEBUG = true
end
ruby_set_encoding(given_opt) click to toggle source
# File lib/optparse_plus.rb, line 114
def ruby_set_encoding(given_opt)
  external, internal = given_opt.split(/:/o, 2)
  Encoding.default_external = external if external and not external.empty?
  Encoding.default_internal = internal if internal and not internal.empty?
end