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
- #global_defaults_option(file_path, names, settings = {}) ⇒ Object
- #help ⇒ Object
- #help_option(names, settings = {}) ⇒ Object
- #help_str=(str) ⇒ 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
- #show_defaults_option(names, settings = {}) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#apply_options(layer, opts = {}) ⇒ Object
294 295 296 297 |
# File 'lib/everyday-cli-utils/option.rb', line 294 def (layer, opts = {}) ||= OptionList.new opts.each { |opt| .update(opt[0], opt[1], layer) } end |
#banner(banner) ⇒ Object
299 300 301 302 |
# File 'lib/everyday-cli-utils/option.rb', line 299 def () ||= OptionList.new . = end |
#default_options(opts = {}) ⇒ Object
289 290 291 292 |
# File 'lib/everyday-cli-utils/option.rb', line 289 def (opts = {}) ||= OptionList.new opts.each { |opt| .set(opt[0], opt[1]) } end |
#default_settings(settings = {}) ⇒ Object
284 285 286 287 |
# File 'lib/everyday-cli-utils/option.rb', line 284 def default_settings(settings = {}) ||= OptionList.new .default_settings = settings end |
#defaults_option(file_path, names, settings = {}) ⇒ Object
250 251 252 253 254 255 256 257 |
# File 'lib/everyday-cli-utils/option.rb', line 250 def defaults_option(file_path, names, settings = {}) ||= OptionList.new @set_defaults = false @defaults_file = File.(file_path) @exit_on_save = !settings.has_key?(:exit_on_save) || settings[:exit_on_save] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @set_defaults = true } end |
#global_defaults_option(file_path, names, settings = {}) ⇒ Object
259 260 261 262 263 264 265 266 |
# File 'lib/everyday-cli-utils/option.rb', line 259 def global_defaults_option(file_path, names, settings = {}) ||= OptionList.new @set_global_defaults = false @global_defaults_file = File.(file_path) @exit_on_global_save = !settings.has_key?(:exit_on_save) || settings[:exit_on_save] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @set_global_defaults = true } end |
#help ⇒ Object
316 317 318 319 |
# File 'lib/everyday-cli-utils/option.rb', line 316 def help ||= OptionList.new .help end |
#help_option(names, settings = {}) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/everyday-cli-utils/option.rb', line 276 def help_option(names, settings = {}) ||= OptionList.new @display_help = false @exit_on_print = !settings.has_key?(:exit_on_print) || settings[:exit_on_print] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @display_help = true } end |
#help_str=(str) ⇒ Object
326 327 328 |
# File 'lib/everyday-cli-utils/option.rb', line 326 def help_str=(str) .help_str = str end |
#option(opt_name, names, settings = {}, &block) ⇒ Object
240 241 242 243 |
# File 'lib/everyday-cli-utils/option.rb', line 240 def option(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option, opt_name, names, settings, &block) end |
#option_list ⇒ Object
312 313 314 |
# File 'lib/everyday-cli-utils/option.rb', line 312 def option_list end |
#option_with_param(opt_name, names, settings = {}, &block) ⇒ Object
245 246 247 248 |
# File 'lib/everyday-cli-utils/option.rb', line 245 def option_with_param(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option_with_param, opt_name, names, settings, &block) end |
#options ⇒ Object
308 309 310 |
# File 'lib/everyday-cli-utils/option.rb', line 308 def .composite(:global, :local, :arg) end |
#opts ⇒ Object
304 305 306 |
# File 'lib/everyday-cli-utils/option.rb', line 304 def opts .opts end |
#parse!(argv = ARGV) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/everyday-cli-utils/option.rb', line 330 def parse!(argv = ARGV) ||= OptionList.new :global, YAML::load_file(@global_defaults_file) unless @global_defaults_file.nil? || !File.exist?(@global_defaults_file) :local, YAML::load_file(@defaults_file) unless @defaults_file.nil? || !File.exist?(@defaults_file) .parse!(argv) if @display_help puts help exit 0 if @exit_on_print end if @show_defaults puts .show_defaults exit 0 if @exit_on_show_defaults end if @set_global_defaults IO.write(@global_defaults_file, .composite(:global, :arg).to_yaml) if @exit_on_global_save puts 'Global defaults set' exit 0 end elsif @set_defaults IO.write(@defaults_file, .composite(:local, :arg).to_yaml) if @exit_on_save puts 'Defaults set' exit 0 end end end |
#show_defaults_option(names, settings = {}) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/everyday-cli-utils/option.rb', line 268 def show_defaults_option(names, settings = {}) ||= OptionList.new @show_defaults = false @exit_on_show_defaults = !settings.has_key?(:exit_on_show) || settings[:exit_on_show] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @show_defaults = true } end |
#to_s ⇒ Object
321 322 323 324 |
# File 'lib/everyday-cli-utils/option.rb', line 321 def to_s ||= OptionList.new .to_s end |