class EverydayCliUtils::OptionList

Attributes

default_settings[RW]
help_str[RW]
opts[R]
special_options[R]

Public Class Methods

new() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 223
def initialize
  @options          = {}
  @special_options  = {}
  @default_settings = {}
  @opts             = OptionParser.new
  @help_str         = nil
end

Public Instance Methods

[]=(opt_name, opt) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 231
def []=(opt_name, opt)
  @options[opt_name] = opt
end
banner=(banner) click to toggle source
build_option_str(v, indent, max_name_len) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 312
def build_option_str(v, indent, max_name_len)
  opt       = @options[v[0]]
  val       = v[1]
  names_str = opt.names.join(', ')
  "#{' ' * indent}#{names_str}#{' ' * ((max_name_len + 4) - names_str.length)}#{val_to_str(val)}\n"
end
composite(*layers) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 271
def composite(*layers)
  hash = {}
  @options.each { |v| hash[v[0]] = v[1].composite(*layers) }
  hash
end
help() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 277
def help
  @help_str.nil? ? @opts.help : @help_str
end
options_to_str(options, indent = 4) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 305
def options_to_str(options, indent = 4)
  str          = ''
  max_name_len = @options.values.map { |v| v.names.join(', ').length }.max
  options.each { |v| str << build_option_str(v, indent, max_name_len) }
  str
end
parse!(argv = ARGV) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 289
def parse!(argv = ARGV)
  @opts.parse!(argv)
end
register(type, opt_name, names, settings = {}, &block) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 251
def register(type, opt_name, names, settings = {}, &block)
  OptionDef.register(@opts, self, type, opt_name, names, settings, @default_settings, &block)
end
register_special(order, opt_name, names, exit_on_action, print_on_exit_str, settings, action_block, pre_parse_block = nil) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 255
def register_special(order, opt_name, names, exit_on_action, print_on_exit_str, settings, action_block, pre_parse_block = nil)
  SpecialOptionDef.register(order, @opts, self, opt_name, names, exit_on_action, print_on_exit_str, settings, @default_settings, action_block, pre_parse_block)
end
run_special() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 259
def run_special
  run_special_helper { |v| v[1].run(self) }
end
run_special_helper(&block) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 267
def run_special_helper(&block)
  @special_options.to_a.sort_by { |v| v[1].order }.each(&block)
end
run_special_pre_parse() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 263
def run_special_pre_parse
  run_special_helper { |v| v[1].run_pre_parse(self) }
end
set(opt_name, value) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 235
def set(opt_name, value)
  @options[opt_name].set(value) if @options.has_key?(opt_name)
end
set_all(opts) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 239
def set_all(opts)
  opts.each { |opt| set(opt[0], opt[1]) }
end
show_defaults() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 293
def show_defaults
  script_defaults = composite
  global_defaults = composite(:global)
  local_defaults  = composite(:global, :local)
  global_diff     = EverydayCliUtils::MapUtil.hash_diff(global_defaults, script_defaults)
  local_diff      = EverydayCliUtils::MapUtil.hash_diff(local_defaults, global_defaults)
  str             = "Script Defaults:\n#{options_to_str(script_defaults)}\n"
  str << "Script + Global Defaults:\n#{options_to_str(global_diff)}\n" unless global_diff.empty?
  str << "Script + Global + Local Defaults:\n#{options_to_str(local_diff)}\n" unless local_diff.empty?
  str
end
to_s() click to toggle source
# File lib/everyday-cli-utils/option.rb, line 281
def to_s
  @help_str.nil? ? @opts.to_s : @help_str
end
update(opt_name, value, layer) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 243
def update(opt_name, value, layer)
  @options[opt_name].update(value, layer) if @options.has_key?(opt_name)
end
update_all(layer, opts) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 247
def update_all(layer, opts)
  opts.each { |opt| update(opt[0], opt[1], layer) }
end
val_to_str(val) click to toggle source
# File lib/everyday-cli-utils/option.rb, line 319
def val_to_str(val)
  if val.nil?
    'nil'
  elsif val.is_a?(TrueClass)
    'true'
  elsif val.is_a?(FalseClass)
    'false'
  elsif val.is_a?(Enumerable)
    "[#{val.map { |v| val_to_str(v) }.join(', ')}]"
  elsif val.is_a?(Numeric)
    val.to_s
  else
    "'#{val.to_s}'"
  end
end