Module: Clin::CommandMixin::Options::ClassMethods
- Defined in:
- lib/clin/command_mixin/options.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#general_options ⇒ Object
Returns the value of attribute general_options.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#add_option(option) ⇒ Object
Add a new option.
- #auto_option(name, usage, &block) ⇒ Object
-
#execute_general_options(options) ⇒ Object
Call #execute on each of the general options.
-
#flag_option(name, description, **config, &block) ⇒ Object
For an option that does not have an argument Same as .option except it will default argument to false “‘ option :verbose, ’Use verbose’ #=> -v –verbose will be added to the option of this command “‘.
-
#general_option(option_cls, config = {}) ⇒ Object
Add a general option.
-
#list_flag_option(name, description, **config) ⇒ Object
Add a list options that don’t take arguments Same as .list_option but set
argumentto false. -
#list_option(name, description, **config) ⇒ Object
Add a list option.
-
#opt_option(*args, &block) ⇒ Object
Add an option “‘ option :require, ’-r’, ‘–require [LIBRARY]’, ‘Require the library’ option ‘-h’, ‘–helper’, ‘Show the help’ do puts opts exit end “‘.
-
#option(name, description, **config, &block) ⇒ Object
Add an option.
-
#register_options(opts, out) ⇒ Object
To be called inside OptionParser block Extract the option in the command line using the OptionParser and map it to the out map.
-
#remove_general_option(option_cls) ⇒ Object
Remove a general option Might be useful if a parent added the option but is not needed in this child.
Instance Attribute Details
#general_options ⇒ Object
Returns the value of attribute general_options.
23 24 25 |
# File 'lib/clin/command_mixin/options.rb', line 23 def end |
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/clin/command_mixin/options.rb', line 22 def end |
Instance Method Details
#add_option(option) ⇒ Object
Add a new option.
86 87 88 89 |
# File 'lib/clin/command_mixin/options.rb', line 86 def add_option(option) # Need to use += instead of << otherwise the parent class will also be changed << option end |
#auto_option(name, usage, &block) ⇒ Object
80 81 82 |
# File 'lib/clin/command_mixin/options.rb', line 80 def auto_option(name, usage, &block) add_option Clin::Option.parse(name, usage, &block) end |
#execute_general_options(options) ⇒ Object
Call #execute on each of the general options. This is called during the command initialization e.g. A verbose general option execute would be: “‘ def execute(params)
MyApp.verbose = true if params[:verbose]
end “‘
128 129 130 131 132 |
# File 'lib/clin/command_mixin/options.rb', line 128 def () .each do |_cls, gopts| gopts.execute() end end |
#flag_option(name, description, **config, &block) ⇒ Object
For an option that does not have an argument Same as .option except it will default argument to false “‘
option :verbose, 'Use verbose' #=> -v --verbose will be added to the option of this command
“‘
63 64 65 |
# File 'lib/clin/command_mixin/options.rb', line 63 def flag_option(name, description, **config, &block) add_option Clin::Option.new(name, description, **config.merge(argument: false), &block) end |
#general_option(option_cls, config = {}) ⇒ Object
Add a general option
94 95 96 97 |
# File 'lib/clin/command_mixin/options.rb', line 94 def general_option(option_cls, config = {}) option_cls = option_cls.constantize if option_cls.is_a? String [option_cls] = option_cls.new(config) end |
#list_flag_option(name, description, **config) ⇒ Object
Add a list options that don’t take arguments Same as .list_option but set argument to false
76 77 78 |
# File 'lib/clin/command_mixin/options.rb', line 76 def list_flag_option(name, description, **config) add_option Clin::OptionList.new(name, description, **config.merge(argument: false)) end |
#list_option(name, description, **config) ⇒ Object
Add a list option.
69 70 71 |
# File 'lib/clin/command_mixin/options.rb', line 69 def list_option(name, description, **config) add_option Clin::OptionList.new(name, description, **config) end |
#opt_option(*args, &block) ⇒ Object
Add an option “‘
option :require, '-r', '--require [LIBRARY]', 'Require the library'
option '-h', '--helper', 'Show the help' do
puts opts
exit
end
“‘
37 38 39 |
# File 'lib/clin/command_mixin/options.rb', line 37 def opt_option(*args, &block) add_option Clin::Option.new(*args, &block) end |
#option(name, description, **config, &block) ⇒ Object
Add an option. Helper method that just create a new Clin::Option with the argument then call add_option “‘
option :show, 'Show some message'
# => -s --show SHOW Show some message
option :require, 'Require a library', short: false, optional: true, argument: 'LIBRARY'
# => --require [LIBRARY] Require a library
option :help, 'Show the help', argument: false do
puts opts
exit
end
# => -h --help Show the help
“‘
54 55 56 |
# File 'lib/clin/command_mixin/options.rb', line 54 def option(name, description, **config, &block) add_option Clin::Option.new(name, description, **config, &block) end |
#register_options(opts, out) ⇒ Object
To be called inside OptionParser block Extract the option in the command line using the OptionParser and map it to the out map.
110 111 112 113 114 115 116 117 118 |
# File 'lib/clin/command_mixin/options.rb', line 110 def (opts, out) .each do |option| option.register(opts, out) end .each do |_cls, option| option.class.(opts, out) end end |
#remove_general_option(option_cls) ⇒ Object
Remove a general option Might be useful if a parent added the option but is not needed in this child.
101 102 103 104 |
# File 'lib/clin/command_mixin/options.rb', line 101 def remove_general_option(option_cls) option_cls = option_cls.constantize if option_cls.is_a? String .delete(option_cls) end |