Class: Clin::CommandOptionsMixin
- Inherits:
-
Object
- Object
- Clin::CommandOptionsMixin
- Defined in:
- lib/clin/command_options_mixin.rb
Overview
Template class for reusable options and commands It provide the method to add options to a command
Direct Known Subclasses
Class Method Summary collapse
- .add_option(option) ⇒ Object
-
.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.
-
.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.
Class Method Details
.add_option(option) ⇒ Object
55 56 57 58 |
# File 'lib/clin/command_options_mixin.rb', line 55 def self.add_option(option) # Need to use += instead of << otherwise the parent class will also be changed self. += [option] 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
“‘
51 52 53 |
# File 'lib/clin/command_options_mixin.rb', line 51 def self.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
63 64 65 66 |
# File 'lib/clin/command_options_mixin.rb', line 63 def self.general_option(option_cls, config = {}) option_cls = option_cls.constantize if option_cls.is_a? String self. = self..merge(option_cls => option_cls.new(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
“‘
25 26 27 |
# File 'lib/clin/command_options_mixin.rb', line 25 def self.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
“‘
42 43 44 |
# File 'lib/clin/command_options_mixin.rb', line 42 def self.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.
79 80 81 82 83 84 85 86 87 |
# File 'lib/clin/command_options_mixin.rb', line 79 def self.(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.
70 71 72 73 |
# File 'lib/clin/command_options_mixin.rb', line 70 def self.remove_general_option(option_cls) option_cls = option_cls.constantize if option_cls.is_a? String self. = self..except(option_cls) end |