Module: RakeCommander::Options::Name
- Included in:
- RakeCommander::Option, Arguments::ClassMethods, Error::Base
- Defined in:
- lib/rake-commander/options/name.rb
Constant Summary collapse
- BOOLEAN_TOKEN =
'[no-]'.freeze
- HYPHEN_START_REGEX =
Substitions
/^-+/.freeze
- HYPEN_REGEX =
/-+/.freeze
- UNDERSCORE_REGEX =
/_+/.freeze
- WORD_DELIMITER =
/[\s=]+/.freeze
- OPTIONAL_REGEX =
Checkers / Capturers
/\[\w+\]$/.freeze
- SINGLE_HYPHEN_REGEX =
/^-(?<options>[^- ][^ ]*)/.freeze
- DOUBLE_HYPHEN_REGEX =
/^(?:--\[?no-\]?|--)(?<option>[^- ][^ \r\n]*).*$/.freeze
- BOOLEAN_NAME_REGEX =
/^[^ ]*#{Regexp.escape(BOOLEAN_TOKEN)}[^ ]{2,}/.freeze
Instance Method Summary collapse
-
#argument_optional?(value) ⇒ Boolean
true
ifvalue
ends with[String]
. -
#argument_required?(value) ⇒ Boolean
true
ifvalue
does NOT end with[String]
. -
#boolean_name?(value) ⇒ Boolean
Whether the name has the boolean switch
[no-]
. -
#capture_argument_with!(args) ⇒ String, ...
Modifies
args
and returns the arg candidate. -
#capture_arguments_name!(args, strict: true, symbol: false) ⇒ String, Symbol
Modifies
args
and returns the name candidate. -
#capture_arguments_short!(args, strict: true, symbol: false) ⇒ String, Symbol
Modifies
args
and returns the short candidate. - #double_hyphen?(value) ⇒ Boolean
-
#name_argument(value) ⇒ String, NilClass
The argument of
value
, if present. -
#name_argument?(value) ⇒ String, NilClass
Whether
value
is a name with argument. -
#name_hyphen(value) ⇒ String, NilClass
Gets the actual name of the option.
-
#name_sym(value) ⇒ Symbol, NilClass
Converter.
-
#name_word_sym(value) ⇒ Symbol, NilClass
It's like
#name_sym
but it only gets the option name. -
#short_hyphen(value) ⇒ String, NilClass
It returns the hyphened (
-
) version of a shortvalue
. -
#short_sym(value) ⇒ Symbol, NilClass
Converter.
- #single_hyphen?(value) ⇒ Boolean
- #valid_name?(value, strict: false) ⇒ Boolean
- #valid_short?(value, strict: false) ⇒ Boolean
Instance Method Details
#argument_optional?(value) ⇒ Boolean
when there is NO argument it evaluates true
Returns true
if value
ends with [String]
.
186 187 188 189 |
# File 'lib/rake-commander/options/name.rb', line 186 def argument_optional?(value) return true unless value !!value.match(OPTIONAL_REGEX) end |
#argument_required?(value) ⇒ Boolean
Returns true
if value
does NOT end with [String]
.
175 176 177 178 |
# File 'lib/rake-commander/options/name.rb', line 175 def argument_required?(value) return false unless value !argument_optional?(value) end |
#boolean_name?(value) ⇒ Boolean
Returns whether the name has the boolean switch [no-]
.
29 30 31 32 |
# File 'lib/rake-commander/options/name.rb', line 29 def boolean_name?(value) return false unless value.respond_to?(:to_s) !!value.to_s.match(BOOLEAN_NAME_REGEX) end |
#capture_argument_with!(args) ⇒ String, ...
Modifies args
and returns the arg candidate
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rake-commander/options/name.rb', line 80 def capture_argument_with!(args) raise ArgumentError, "Expecting Array. Given: #{args.class}" unless args.is_a?(Array) args.dup.find.with_index do |arg, i| yield(arg).tap do |valid| next unless valid args.slice!(i) return arg end end nil end |
#capture_arguments_name!(args, strict: true, symbol: false) ⇒ String, Symbol
Modifies args
and returns the name candidate
69 70 71 72 73 74 75 |
# File 'lib/rake-commander/options/name.rb', line 69 def capture_arguments_name!(args, strict: true, symbol: false) capture_argument_with!(args) do |arg| next false unless arg.is_a?(String) || arg.is_a?(Symbol) next false if symbol && !arg.is_a?(Symbol) valid_name?(arg, strict: strict) end end |
#capture_arguments_short!(args, strict: true, symbol: false) ⇒ String, Symbol
Modifies args
and returns the short candidate
58 59 60 61 62 63 64 |
# File 'lib/rake-commander/options/name.rb', line 58 def capture_arguments_short!(args, strict: true, symbol: false) capture_argument_with!(args) do |arg| next false unless arg.is_a?(String) || arg.is_a?(Symbol) next false if symbol && !arg.is_a?(Symbol) valid_short?(arg, strict: strict) end end |
#double_hyphen?(value) ⇒ Boolean
23 24 25 26 |
# File 'lib/rake-commander/options/name.rb', line 23 def double_hyphen?(value) return false unless value.respond_to?(:to_s) !!value.to_s.match(DOUBLE_HYPHEN_REGEX) end |
#name_argument(value) ⇒ String, NilClass
Returns the argument of value
, if present.
158 159 160 |
# File 'lib/rake-commander/options/name.rb', line 158 def name_argument(value) name_words(value)[1] end |
#name_argument?(value) ⇒ String, NilClass
Returns whether value
is a name with argument.
166 167 168 |
# File 'lib/rake-commander/options/name.rb', line 166 def name_argument?(value) !!name_argument(value) end |
#name_hyphen(value) ⇒ String, NilClass
Gets the actual name of the option. First word.
148 149 150 151 152 153 |
# File 'lib/rake-commander/options/name.rb', line 148 def name_hyphen(value) return nil unless value = name_sym(value) value = value.to_s.gsub(UNDERSCORE_REGEX, '-') return nil if value.empty? "--#{value}" end |
#name_sym(value) ⇒ Symbol, NilClass
- It removes the double hyphen start (
--
) - Replaces any intermediate hyphen by underscore
_
- Replaces any multi-spacing by single space
Converter.
112 113 114 115 116 117 118 119 |
# File 'lib/rake-commander/options/name.rb', line 112 def name_sym(value) return nil unless value value = value.to_s.gsub(HYPHEN_START_REGEX, '') value = value.gsub(HYPEN_REGEX, '_') value = value.gsub(WORD_DELIMITER, ' ') return nil if value.empty? value.to_sym end |
#name_word_sym(value) ⇒ Symbol, NilClass
- It also removes the boolean token
[no-]
It's like #name_sym
but it only gets the option name.
129 130 131 132 133 134 |
# File 'lib/rake-commander/options/name.rb', line 129 def name_word_sym(value) value = value.to_s.gsub(BOOLEAN_TOKEN, '') return nil unless value = name_sym(value) return nil unless value = name_words(value).first value.downcase.to_sym end |
#short_hyphen(value) ⇒ String, NilClass
Returns it returns the hyphened (-
) version of a short value
.
137 138 139 140 |
# File 'lib/rake-commander/options/name.rb', line 137 def short_hyphen(value) return nil unless value = short_sym(value) "-#{value}" end |
#short_sym(value) ⇒ Symbol, NilClass
Converter
96 97 98 99 100 101 102 |
# File 'lib/rake-commander/options/name.rb', line 96 def short_sym(value) return nil unless value value = value.to_s.gsub(BOOLEAN_TOKEN, '') value = value.gsub(HYPHEN_START_REGEX, '') return nil unless value = value.chars.first value.to_sym end |
#single_hyphen?(value) ⇒ Boolean
17 18 19 20 |
# File 'lib/rake-commander/options/name.rb', line 17 def single_hyphen?(value) return false unless value.respond_to?(:to_s) !!value.to_s.match(SINGLE_HYPHEN_REGEX) end |
#valid_name?(value, strict: false) ⇒ Boolean
47 48 49 50 51 52 53 |
# File 'lib/rake-commander/options/name.rb', line 47 def valid_name?(value, strict: false) return false unless value.respond_to?(:to_s) value = value.to_s.strip return false if value.empty? return false if strict && !double_hyphen?(value) name_sym(value).to_s.length > 1 end |
#valid_short?(value, strict: false) ⇒ Boolean
36 37 38 39 40 41 42 43 |
# File 'lib/rake-commander/options/name.rb', line 36 def valid_short?(value, strict: false) return false unless value.respond_to?(:to_s) value = value.to_s.strip return false if value.empty? return false if strict && !single_hyphen?(value) value = value.gsub(HYPHEN_START_REGEX, '') value.length == 1 end |