Class: EverydayCliUtils::OptionList

Inherits:
Object
  • Object
show all
Defined in:
lib/everyday-cli-utils/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionList

Returns a new instance of OptionList.



223
224
225
226
227
228
229
# File 'lib/everyday-cli-utils/option.rb', line 223

def initialize
  @options          = {}
  @special_options  = {}
  @default_settings = {}
  @opts             = OptionParser.new
  @help_str         = nil
end

Instance Attribute Details

#default_settingsObject

Returns the value of attribute default_settings.



221
222
223
# File 'lib/everyday-cli-utils/option.rb', line 221

def default_settings
  @default_settings
end

#help_strObject

Returns the value of attribute help_str.



221
222
223
# File 'lib/everyday-cli-utils/option.rb', line 221

def help_str
  @help_str
end

#optsObject (readonly)

Returns the value of attribute opts.



220
221
222
# File 'lib/everyday-cli-utils/option.rb', line 220

def opts
  @opts
end

#special_optionsObject (readonly)

Returns the value of attribute special_options.



220
221
222
# File 'lib/everyday-cli-utils/option.rb', line 220

def special_options
  @special_options
end

Instance Method Details

#[]=(opt_name, opt) ⇒ Object



231
232
233
# File 'lib/everyday-cli-utils/option.rb', line 231

def []=(opt_name, opt)
  @options[opt_name] = opt
end

#banner=(banner) ⇒ Object



285
286
287
# File 'lib/everyday-cli-utils/option.rb', line 285

def banner=(banner)
  @opts.banner = banner
end

#build_option_str(v, indent, max_name_len) ⇒ Object



312
313
314
315
316
317
# 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) ⇒ Object



271
272
273
274
275
# 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

#helpObject



277
278
279
# 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) ⇒ Object



305
306
307
308
309
310
# 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) ⇒ Object



289
290
291
# File 'lib/everyday-cli-utils/option.rb', line 289

def parse!(argv = ARGV)
  @opts.parse!(argv)
end

#register(type, opt_name, names, settings = {}, &block) ⇒ Object



251
252
253
# 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) ⇒ Object



255
256
257
# 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_specialObject



259
260
261
# 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) ⇒ Object



267
268
269
# 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_parseObject



263
264
265
# 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) ⇒ Object



235
236
237
# 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) ⇒ Object



239
240
241
# File 'lib/everyday-cli-utils/option.rb', line 239

def set_all(opts)
  opts.each { |opt| set(opt[0], opt[1]) }
end

#show_defaultsObject



293
294
295
296
297
298
299
300
301
302
303
# 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_sObject



281
282
283
# 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) ⇒ Object



243
244
245
# 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) ⇒ Object



247
248
249
# 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) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# 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