Class: RakeCommander::Option
- Inherits:
-
Object
- Object
- RakeCommander::Option
- Extended by:
- Base::ClassHelpers, RakeCommander::Options::Name
- Includes:
- RakeCommander::Options::Description
- Defined in:
- lib/rake-commander/option.rb
Constant Summary
Constants included from Base::ClassHelpers
Constants included from RakeCommander::Options::Name
RakeCommander::Options::Name::BOOLEAN_NAME_REGEX, RakeCommander::Options::Name::BOOLEAN_TOKEN, RakeCommander::Options::Name::DOUBLE_HYPHEN_REGEX, RakeCommander::Options::Name::HYPEN_REGEX, RakeCommander::Options::Name::HYPHEN_START_REGEX, RakeCommander::Options::Name::OPTIONAL_REGEX, RakeCommander::Options::Name::SINGLE_HYPHEN_REGEX, RakeCommander::Options::Name::UNDERSCORE_REGEX, RakeCommander::Options::Name::WORD_DELIMITER
Constants included from RakeCommander::Options::Description
RakeCommander::Options::Description::DESC_MAX_LENGTH
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name_full ⇒ Object
readonly
Returns the value of attribute name_full.
Instance Method Summary collapse
-
#add_switch(opts_parser, where: :base, implicit_short: false, &middleware) ⇒ Object
Adds this option's switch to the
OptionParser
. - #argument ⇒ Object
- #argument? ⇒ Boolean
- #argument_required? ⇒ Boolean
- #boolean_name? ⇒ Boolean
- #default? ⇒ Boolean
-
#dup(**kargs, &block) ⇒ RakeCommander::Option
(also: #deep_dup)
Makes a copy of this option.
-
#initialize(*args, sample: false, **kargs, &block) ⇒ Option
constructor
A new instance of Option.
-
#merge(opt, **kargs) ⇒ RakeCommander::Option
Creates a new option, result of merging this
opt
with this option,. - #name ⇒ Symbol
- #name_hyphen ⇒ String
-
#required? ⇒ Boolean
Whether this option is required.
- #short ⇒ Symbol
- #short_hyphen ⇒ String
-
#short_implicit ⇒ Object
OptionParser
interprets free shorts that match the first letter of an option name as an invocation of that option. - #type_coercion ⇒ Class, NilClass
Methods included from Base::ClassHelpers
class_resolver, descendants, descendants?, new_class, redef_without_warning, resolve_class, sort_classes, to_constant, used_param?
Methods included from RakeCommander::Options::Name
argument_optional?, capture_argument_with!, double_hyphen?, name_argument, name_argument?, name_sym, name_word_sym, short_sym, single_hyphen?, valid_name?, valid_short?
Constructor Details
#initialize(*args, sample: false, **kargs, &block) ⇒ Option
Returns a new instance of Option.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rake-commander/option.rb', line 11 def initialize(*args, sample: false, **kargs, &block) short, name = capture_arguments_short_n_name!(args, kargs, sample: sample) @name_full = name.freeze super(short.freeze, @name_full) @default = kargs[:default] if kargs.key?(:default) @desc = kargs[:desc] if kargs.key?(:desc) @required = kargs[:required] if kargs.key?(:required) @type_coercion = kargs[:type] if kargs.key?(:type) @other_args = args @original_block = block configure_other end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
8 9 10 |
# File 'lib/rake-commander/option.rb', line 8 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
8 9 10 |
# File 'lib/rake-commander/option.rb', line 8 def desc @desc end |
#name_full ⇒ Object (readonly)
Returns the value of attribute name_full.
8 9 10 |
# File 'lib/rake-commander/option.rb', line 8 def name_full @name_full end |
Instance Method Details
#add_switch(opts_parser, where: :base, implicit_short: false, &middleware) ⇒ Object
it allows to add a middleware
block that will be called at parse
runtime
Adds this option's switch to the OptionParser
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rake-commander/option.rb', line 109 def add_switch(opts_parser, where: :base, implicit_short: false, &middleware) raise "Expecting OptionParser. Given: #{opts_parser.class}" unless opts_parser.is_a?(OptionParser) args = switch_args(implicit_short: implicit_short) block = option_block(&middleware) case where when :head, :top opts_parser.on_head(*args, &block) when :tail, :end opts_parser.on_tail(*args, &block) else # :base opts_parser.on(*args, &block) end opts_parser end |
#argument ⇒ Object
83 84 85 86 |
# File 'lib/rake-commander/option.rb', line 83 def argument return nil unless argument? self.class.name_argument(name_full) end |
#argument? ⇒ Boolean
78 79 80 |
# File 'lib/rake-commander/option.rb', line 78 def argument? self.class.name_argument?(name_full) end |
#argument_required? ⇒ Boolean
89 90 91 |
# File 'lib/rake-commander/option.rb', line 89 def argument_required? self.class.argument_required?(argument) end |
#boolean_name? ⇒ Boolean
73 74 75 |
# File 'lib/rake-commander/option.rb', line 73 def boolean_name? self.class.boolean_name?(name_full) end |
#default? ⇒ Boolean
101 102 103 |
# File 'lib/rake-commander/option.rb', line 101 def default? instance_variable_defined?(:@default) end |
#dup(**kargs, &block) ⇒ RakeCommander::Option Also known as: deep_dup
Makes a copy of this option
27 28 29 30 |
# File 'lib/rake-commander/option.rb', line 27 def dup(**kargs, &block) block ||= original_block self.class.new(**dup_key_arguments.merge(kargs), &block) end |
#merge(opt, **kargs) ⇒ RakeCommander::Option
Creates a new option, result of merging this opt
with this option,
35 36 37 38 |
# File 'lib/rake-commander/option.rb', line 35 def merge(opt, **kargs) raise "Expecting RakeCommander::Option. Given: #{opt.class}" unless opt.is_a?(RakeCommander::Option) dup(**opt.dup_key_arguments.merge(kargs), &opt.original_block) end |
#name ⇒ Symbol
63 64 65 |
# File 'lib/rake-commander/option.rb', line 63 def name self.class.name_word_sym(super) end |
#name_hyphen ⇒ String
68 69 70 |
# File 'lib/rake-commander/option.rb', line 68 def name_hyphen self.class.name_hyphen(name_full) end |
#required? ⇒ Boolean
Returns whether this option is required.
41 42 43 |
# File 'lib/rake-commander/option.rb', line 41 def required? !!@required end |
#short ⇒ Symbol
46 47 48 |
# File 'lib/rake-commander/option.rb', line 46 def short self.class.short_sym(super) end |
#short_hyphen ⇒ String
58 59 60 |
# File 'lib/rake-commander/option.rb', line 58 def short_hyphen self.class.short_hyphen(short) end |
#short_implicit ⇒ Object
OptionParser
interprets free shorts that match the first letter of an option name
as an invocation of that option. This method allows to identify this.
return [Symbol]
53 54 55 |
# File 'lib/rake-commander/option.rb', line 53 def short_implicit self.class.short_sym(@name_full) end |
#type_coercion ⇒ Class, NilClass
94 95 96 97 98 |
# File 'lib/rake-commander/option.rb', line 94 def type_coercion value = @type_coercion || (default? && default.class) return nil unless value.is_a?(Class) value end |