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

/^-+/
HYPEN_REGEX =
/-+/
UNDERSCORE_REGEX =
/_+/
WORD_DELIMITER =
/[\s=]+/
OPTIONAL_REGEX =

Checkers / Capturers

/\[\w+\]$/
SINGLE_HYPHEN_REGEX =
/^-(?<options>[^- ][^ ]*)/
DOUBLE_HYPHEN_REGEX =
/^(?:--\[?no-\]?|--)(?<option>[^- ][^ \r\n]*).*$/
BOOLEAN_NAME_REGEX =
/^[^ ]*#{Regexp.escape(BOOLEAN_TOKEN)}[^ ]{2,}/

Instance Method Summary collapse

Instance Method Details

#argument_optional?(value) ⇒ Boolean

Note:

when there is NO argument it evaluates true

Returns true if value ends with [String].

Examples:

* `"--there-we-go   [ARGUMENT]"` returns `true`
* `"--folder  FOLDER"` returns `false`
* `"--time"` returns `true`

Returns:

  • (Boolean)

    true if value ends with [String]



207
208
209
210
211
# File 'lib/rake-commander/options/name.rb', line 207

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].

Examples:

* `"--there-we-go   [ARGUMENT]"` returns `false`
* `"--folder  FOLDER"` returns `true`
* `"--time"` returns `false`

Returns:

  • (Boolean)

    true if value does NOT end with [String]



195
196
197
198
199
# File 'lib/rake-commander/options/name.rb', line 195

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-].

Returns:

  • (Boolean)

    whether the name has the boolean switch [no-]



31
32
33
34
35
# File 'lib/rake-commander/options/name.rb', line 31

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

Parameters:

  • args (Array<String, Symbol>)

Returns:

  • (String, Symbol, NilClass)

    the arg candidate

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rake-commander/options/name.rb', line 89

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

Parameters:

  • args (Array<String, Symbol>)

Returns:

  • (String, Symbol)

    the name candidate



77
78
79
80
81
82
83
84
# File 'lib/rake-commander/options/name.rb', line 77

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

Parameters:

  • args (Array<String, Symbol>)

Returns:

  • (String, Symbol)

    the short candidate



65
66
67
68
69
70
71
72
# File 'lib/rake-commander/options/name.rb', line 65

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

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/rake-commander/options/name.rb', line 24

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.

Examples:

* `"--there-we-go   ARGUMENT"` returns `"ARGUMENT"`

Returns:

  • (String, NilClass)

    the argument of value, if present



178
179
180
# File 'lib/rake-commander/options/name.rb', line 178

def name_argument(value)
  name_words(value)[1]
end

#name_argument?(value) ⇒ String, NilClass

Returns whether value is a name with argument.

Examples:

* `"--there-we-go   ARGUMENT"` returns `true`
* `"--time"` returns `false`

Returns:

  • (String, NilClass)

    whether value is a name with argument



186
187
188
# File 'lib/rake-commander/options/name.rb', line 186

def name_argument?(value)
  !!name_argument(value)
end

#name_hyphen(value) ⇒ String, NilClass

Gets the actual name of the option. First word.

Examples:

* `"--there-we-go   ARGUMENT"` becomes `"--there-we-go ARGUMENT"`
* `"there-we-go"` becomes `"--there-we-go"`
* `:there_we_go` becomes `"--there-we-go"`

Returns:

  • (String, NilClass)

    option name alone double hypened (--)



166
167
168
169
170
171
172
173
# File 'lib/rake-commander/options/name.rb', line 166

def name_hyphen(value)
  return unless value = name_sym(value)

  value = value.to_s.gsub(UNDERSCORE_REGEX, '-')
  return if value.empty?

  "--#{value}"
end

#name_sym(value) ⇒ Symbol, NilClass

Note:
  1. It removes the double hyphen start (--)
  2. Replaces any intermediate hyphen by underscore _
  3. Replaces any multi-spacing by single space

Converter.

Examples:

* `"--there-we-go   ARGUMENT"` becomes `:"there_we_go ARGUMENT"`

Returns:

  • (Symbol, NilClass)


126
127
128
129
130
131
132
133
134
135
# File 'lib/rake-commander/options/name.rb', line 126

def name_sym(value)
  return unless value

  value = value.to_s.gsub(HYPHEN_START_REGEX, '')
  value = value.gsub(HYPEN_REGEX, '_')
  value = value.gsub(WORD_DELIMITER, ' ')
  return if value.empty?

  value.to_sym
end

#name_word_sym(value) ⇒ Symbol, NilClass

Note:
  1. It also removes the boolean token [no-]

It's like #name_sym but it only gets the option name.

Examples:

* `"--there-we-go   ARGUMENT"` becomes `:there_we_go`
* `"--[no]-verbose"` becomes `:verbose`

Returns:

  • (Symbol, NilClass)

See Also:



145
146
147
148
149
150
151
# File 'lib/rake-commander/options/name.rb', line 145

def name_word_sym(value)
  value = value.to_s.gsub(BOOLEAN_TOKEN, '')
  return unless (value = name_sym(value))
  return 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.

Returns:

  • (String, NilClass)

    it returns the hyphened (-) version of a short value



154
155
156
157
158
# File 'lib/rake-commander/options/name.rb', line 154

def short_hyphen(value)
  return unless (value = short_sym(value))

  "-#{value}"
end

#short_sym(value) ⇒ Symbol, NilClass

Converter

Examples:

* `"-d"` becomes `:d`

Returns:

  • (Symbol, NilClass)


108
109
110
111
112
113
114
115
116
# File 'lib/rake-commander/options/name.rb', line 108

def short_sym(value)
  return unless value

  value = value.to_s.gsub(BOOLEAN_TOKEN, '')
  value = value.gsub(HYPHEN_START_REGEX, '')
  return unless value = value.chars.first

  value.to_sym
end

#single_hyphen?(value) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# 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

Parameters:

  • strict (Boolean) (defaults to: false)

    whether hyphen is required when declaring an option name

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/rake-commander/options/name.rb', line 52

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

Parameters:

  • strict (Boolean) (defaults to: false)

    whether hyphen is required when declaring an option short

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/rake-commander/options/name.rb', line 39

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