Class: RakeCommander::Option
- Inherits:
-
Object
- Object
- RakeCommander::Option
- Extended by:
- Base::ClassHelpers, RakeCommander::Options::Name
- 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::Type
RakeCommander::Options::Type::ALLOWED_TYPES
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.
-
#enum? ⇒ Boolean
Whether the option is an enum with fixed values.
-
#enum_options ⇒ Array, NilClass
The valid options when is
enum?. -
#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
optwith this option,. - #name ⇒ Symbol
- #name_hyphen ⇒ String
-
#required? ⇒ Boolean
Whether this option is required.
- #short ⇒ Symbol
- #short_hyphen ⇒ String
-
#short_implicit ⇒ Object
OptionParserinterprets 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.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rake-commander/option.rb', line 12 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.
9 10 11 |
# File 'lib/rake-commander/option.rb', line 9 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
9 10 11 |
# File 'lib/rake-commander/option.rb', line 9 def desc @desc end |
#name_full ⇒ Object (readonly)
Returns the value of attribute name_full.
9 10 11 |
# File 'lib/rake-commander/option.rb', line 9 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rake-commander/option.rb', line 128 def add_switch(opts_parser, where: :base, implicit_short: false, &middleware) msg = "Expecting OptionParser. Given: #{opts_parser.class}" raise msg 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
88 89 90 91 92 |
# File 'lib/rake-commander/option.rb', line 88 def argument return unless argument? self.class.name_argument(name_full) end |
#argument? ⇒ Boolean
83 84 85 |
# File 'lib/rake-commander/option.rb', line 83 def argument? self.class.name_argument?(name_full) end |
#argument_required? ⇒ Boolean
95 96 97 |
# File 'lib/rake-commander/option.rb', line 95 def argument_required? self.class.argument_required?(argument) end |
#boolean_name? ⇒ Boolean
78 79 80 |
# File 'lib/rake-commander/option.rb', line 78 def boolean_name? self.class.boolean_name?(name_full) end |
#default? ⇒ Boolean
120 121 122 |
# File 'lib/rake-commander/option.rb', line 120 def default? instance_variable_defined?(:@default) end |
#dup(**kargs, &block) ⇒ RakeCommander::Option Also known as: deep_dup
Makes a copy of this option
30 31 32 33 |
# File 'lib/rake-commander/option.rb', line 30 def dup(**kargs, &block) block ||= original_block self.class.new(**dup_key_arguments.merge(kargs), &block) end |
#enum? ⇒ Boolean
Returns whether the option is an enum with fixed values.
108 109 110 |
# File 'lib/rake-commander/option.rb', line 108 def enum? type_coercion.is_a?(Array) end |
#enum_options ⇒ Array, NilClass
Returns the valid options when is enum?.
113 114 115 116 117 |
# File 'lib/rake-commander/option.rb', line 113 def return unless enum? type_coercion end |
#merge(opt, **kargs) ⇒ RakeCommander::Option
Creates a new option, result of merging this opt with this option,
38 39 40 41 42 43 |
# File 'lib/rake-commander/option.rb', line 38 def merge(opt, **kargs) msg = "Expecting RakeCommander::Option. Given: #{opt.class}" raise msg unless opt.is_a?(RakeCommander::Option) dup(**opt.dup_key_arguments.merge(kargs), &opt.original_block) end |
#name ⇒ Symbol
68 69 70 |
# File 'lib/rake-commander/option.rb', line 68 def name self.class.name_word_sym(super) end |
#name_hyphen ⇒ String
73 74 75 |
# File 'lib/rake-commander/option.rb', line 73 def name_hyphen self.class.name_hyphen(name_full) end |
#required? ⇒ Boolean
Returns whether this option is required.
46 47 48 |
# File 'lib/rake-commander/option.rb', line 46 def required? !!@required end |
#short ⇒ Symbol
51 52 53 |
# File 'lib/rake-commander/option.rb', line 51 def short self.class.short_sym(super) end |
#short_hyphen ⇒ String
63 64 65 |
# File 'lib/rake-commander/option.rb', line 63 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]
58 59 60 |
# File 'lib/rake-commander/option.rb', line 58 def short_implicit self.class.short_sym(@name_full) end |
#type_coercion ⇒ Class, NilClass
100 101 102 103 104 105 |
# File 'lib/rake-commander/option.rb', line 100 def type_coercion value = @type_coercion || (default? && default.class) return unless allowed_type?(value) value end |