Class: OptparsePlus::OptPlus

Inherits:
Object
  • Object
show all
Defined in:
lib/optparse_plus.rb

Constant Summary collapse

RUBY_OPTIONS =
YAML.load(ruby_options_yaml_source)
RUBY_OPTION_TO_LABEL =
{
  "E" => :optplus_ruby_encoding,
  "d" => :optplus_ruby_debug,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_yaml_source) ⇒ OptPlus

Returns a new instance of OptPlus.



79
80
81
82
83
84
85
# 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

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



46
47
48
# File 'lib/optparse_plus.rb', line 46

def callbacks
  @callbacks
end

#configObject (readonly)

Returns the value of attribute config.



46
47
48
# File 'lib/optparse_plus.rb', line 46

def config
  @config
end

#config_sourceObject (readonly)

Returns the value of attribute config_source.



46
47
48
# File 'lib/optparse_plus.rb', line 46

def config_source
  @config_source
end

#opt=(value) ⇒ Object (writeonly)

Sets the attribute opt

Parameters:

  • value

    the value to set the attribute opt to.



47
48
49
# File 'lib/optparse_plus.rb', line 47

def opt=(value)
  @opt = value
end

Class Method Details

.create_opt(config_yaml_source) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Method Details

#config_to_args(label, config = @config) ⇒ Object



97
98
99
100
101
# 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) ⇒ Object



103
104
105
106
107
108
109
110
# 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_onObject



87
88
89
# File 'lib/optparse_plus.rb', line 87

def opt_on
  @callbacks.keys.each {|label| reflect_callback(label) }
end

#reflect_callback(label) ⇒ Object



91
92
93
94
95
# File 'lib/optparse_plus.rb', line 91

def reflect_callback(label)
  callback = @callbacks[label]
  args = config_to_args(label)
  @opt.on(*args, callback)
end