Module: EverydayCliUtils::OptionUtil
- Defined in:
- lib/everyday-cli-utils/option.rb
Instance Method Summary collapse
- #apply_options(layer, opts = {}) ⇒ Object
- #banner(banner) ⇒ Object
- #default_options(opts = {}) ⇒ Object
- #default_settings(settings = {}) ⇒ Object
- #defaults_option(file_path, names, settings = {}) ⇒ Object
- #defaults_options_helper(file_path, names, settings, order, opt_name, print_on_exit_string, composite_name) ⇒ Object
- #file_path_nil_or_exists?(file_path) ⇒ Boolean
- #global_defaults_option(file_path, names, settings = {}) ⇒ Object
- #help ⇒ Object
- #help_option(names, settings = {}) ⇒ Object
- #help_str=(str) ⇒ Object
- #key_absent_or_true(settings, key) ⇒ Object
- #option(opt_name, names, settings = {}, &block) ⇒ Object
- #option_list ⇒ Object
- #option_with_param(opt_name, names, settings = {}, &block) ⇒ Object
- #options ⇒ Object
- #opts ⇒ Object
- #parse!(argv = ARGV) ⇒ Object
- #read_defaults_proc(composite_name) ⇒ Object
- #show_defaults_option(names, settings = {}) ⇒ Object
- #show_info_helper(names, settings, order, opt_name, exit_on_sym, &block) ⇒ Object
- #to_s ⇒ Object
- #write_defaults_proc(composite_name) ⇒ Object
Instance Method Details
#apply_options(layer, opts = {}) ⇒ Object
408 409 410 411 |
# File 'lib/everyday-cli-utils/option.rb', line 408 def (layer, opts = {}) ||= OptionList.new .update_all(layer, opts) end |
#banner(banner) ⇒ Object
413 414 415 416 |
# File 'lib/everyday-cli-utils/option.rb', line 413 def () ||= OptionList.new . = end |
#default_options(opts = {}) ⇒ Object
403 404 405 406 |
# File 'lib/everyday-cli-utils/option.rb', line 403 def (opts = {}) ||= OptionList.new .set_all(opts) end |
#default_settings(settings = {}) ⇒ Object
398 399 400 401 |
# File 'lib/everyday-cli-utils/option.rb', line 398 def default_settings(settings = {}) ||= OptionList.new .default_settings = settings end |
#defaults_option(file_path, names, settings = {}) ⇒ Object
347 348 349 |
# File 'lib/everyday-cli-utils/option.rb', line 347 def defaults_option(file_path, names, settings = {}) (file_path, names, settings, 4, :defaults, 'Defaults set', :local) end |
#defaults_options_helper(file_path, names, settings, order, opt_name, print_on_exit_string, composite_name) ⇒ Object
355 356 357 358 359 360 |
# File 'lib/everyday-cli-utils/option.rb', line 355 def (file_path, names, settings, order, opt_name, print_on_exit_string, composite_name) ||= OptionList.new settings[:file_path] = File.(file_path) .register_special(order, opt_name, names, key_absent_or_true(settings, :exit_on_save), print_on_exit_string, settings, write_defaults_proc(composite_name), read_defaults_proc(composite_name)) end |
#file_path_nil_or_exists?(file_path) ⇒ Boolean
373 374 375 |
# File 'lib/everyday-cli-utils/option.rb', line 373 def file_path_nil_or_exists?(file_path) file_path.nil? || !File.exist?(file_path) end |
#global_defaults_option(file_path, names, settings = {}) ⇒ Object
351 352 353 |
# File 'lib/everyday-cli-utils/option.rb', line 351 def global_defaults_option(file_path, names, settings = {}) (file_path, names, settings, 3, :global_defaults, 'Global defaults set', :global) end |
#help ⇒ Object
430 431 432 433 |
# File 'lib/everyday-cli-utils/option.rb', line 430 def help ||= OptionList.new .help end |
#help_option(names, settings = {}) ⇒ Object
387 388 389 390 391 |
# File 'lib/everyday-cli-utils/option.rb', line 387 def help_option(names, settings = {}) show_info_helper(names, settings, 1, :help, :exit_on_print) { |_, | puts .help } end |
#help_str=(str) ⇒ Object
440 441 442 |
# File 'lib/everyday-cli-utils/option.rb', line 440 def help_str=(str) .help_str = str end |
#key_absent_or_true(settings, key) ⇒ Object
377 378 379 |
# File 'lib/everyday-cli-utils/option.rb', line 377 def key_absent_or_true(settings, key) !settings.has_key?(key) || settings[key] end |
#option(opt_name, names, settings = {}, &block) ⇒ Object
337 338 339 340 |
# File 'lib/everyday-cli-utils/option.rb', line 337 def option(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option, opt_name, names, settings, &block) end |
#option_list ⇒ Object
426 427 428 |
# File 'lib/everyday-cli-utils/option.rb', line 426 def option_list end |
#option_with_param(opt_name, names, settings = {}, &block) ⇒ Object
342 343 344 345 |
# File 'lib/everyday-cli-utils/option.rb', line 342 def option_with_param(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option_with_param, opt_name, names, settings, &block) end |
#options ⇒ Object
422 423 424 |
# File 'lib/everyday-cli-utils/option.rb', line 422 def .composite(:global, :local, :arg) end |
#opts ⇒ Object
418 419 420 |
# File 'lib/everyday-cli-utils/option.rb', line 418 def opts .opts end |
#parse!(argv = ARGV) ⇒ Object
444 445 446 447 448 449 |
# File 'lib/everyday-cli-utils/option.rb', line 444 def parse!(argv = ARGV) ||= OptionList.new .run_special_pre_parse .parse!(argv) .run_special end |
#read_defaults_proc(composite_name) ⇒ Object
366 367 368 369 370 371 |
# File 'lib/everyday-cli-utils/option.rb', line 366 def read_defaults_proc(composite_name) ->(opt, ) { file_path = opt.settings[:file_path] .update_all composite_name, YAML::load_file(file_path) unless file_path_nil_or_exists?(file_path) } end |
#show_defaults_option(names, settings = {}) ⇒ Object
381 382 383 384 385 |
# File 'lib/everyday-cli-utils/option.rb', line 381 def show_defaults_option(names, settings = {}) show_info_helper(names, settings, 2, :show_defaults, :exit_on_show) { |_, | puts .show_defaults } end |
#show_info_helper(names, settings, order, opt_name, exit_on_sym, &block) ⇒ Object
393 394 395 396 |
# File 'lib/everyday-cli-utils/option.rb', line 393 def show_info_helper(names, settings, order, opt_name, exit_on_sym, &block) ||= OptionList.new .register_special(order, opt_name, names, key_absent_or_true(settings, exit_on_sym), nil, settings, block) end |
#to_s ⇒ Object
435 436 437 438 |
# File 'lib/everyday-cli-utils/option.rb', line 435 def to_s ||= OptionList.new .to_s end |
#write_defaults_proc(composite_name) ⇒ Object
362 363 364 |
# File 'lib/everyday-cli-utils/option.rb', line 362 def write_defaults_proc(composite_name) ->(opt, ) { IO.write(opt.settings[:file_path], .composite(composite_name, :arg).to_yaml) } end |