Module: RakeCommander::Options::Name

Included in:
RakeCommander::Option, Arguments, ErrorRely
Defined in:
lib/rake-commander/options/name.rb

Constant Summary collapse

HYPHEN_START_REGEX =

Substitions

/^-+/.freeze
HYPEN_REGEX =
/-+/.freeze
UNDERSCORE_REGEX =
/_+/.freeze
SPACE_REGEX =
/\s+/.freeze
OPTIONAL_REGEX =

Checkers

/\[\w+\]$/.freeze
SINGLE_HYPHEN_REGEX =
/^-(?<options>[^- ][^ ]*)/.freeze
DOUBLE_HYPHEN_REGEX =
/^--(?<option>[^- ][^ ]*)/.freeze

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]



143
144
145
146
# File 'lib/rake-commander/options/name.rb', line 143

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]



132
133
134
135
# File 'lib/rake-commander/options/name.rb', line 132

def argument_required?(value)
  return false unless value
  !argument_optional?(value)
end

#double_hyphen?(value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/rake-commander/options/name.rb', line 21

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



115
116
117
# File 'lib/rake-commander/options/name.rb', line 115

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



123
124
125
# File 'lib/rake-commander/options/name.rb', line 123

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"`
* `"there-we-go"` becomes `"--there-we-go"`
* `:there_we_go` becomes `"--there-we-go"`

Returns:

  • (String, NilClass)

    option name alone double hypened (--)



105
106
107
108
109
110
# File 'lib/rake-commander/options/name.rb', line 105

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_hyphen?(value, strict: false) ⇒ Boolean

Returns whether value is an hyphened option name.

Parameters:

  • strict (Boolean) (defaults to: false)

    whether hyphen is required when declaring an option name

Returns:

  • (Boolean)

    whether value is an hyphened option name



50
51
52
# File 'lib/rake-commander/options/name.rb', line 50

def name_hyphen?(value, strict: false)
  name?(value, strict: strict) && double_hyphen?(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)


73
74
75
76
77
78
79
80
# File 'lib/rake-commander/options/name.rb', line 73

def name_sym(value)
  return nil unless value
  value = value.to_s.gsub(HYPHEN_START_REGEX, '')
  value = value.gsub(HYPEN_REGEX, '_')
  value = value.gsub(SPACE_REGEX, ' ')
  return nil if value.empty?
  value.to_sym
end

#name_word_sym(value) ⇒ Symbol, NilClass

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

Examples:

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

Returns:

  • (Symbol, NilClass)

See Also:



87
88
89
90
91
# File 'lib/rake-commander/options/name.rb', line 87

def name_word_sym(value)
  return nil unless value = name_sym(value)
  return nil unless value = name_words(value).first
  value.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



94
95
96
97
# File 'lib/rake-commander/options/name.rb', line 94

def short_hyphen(value)
  return nil unless value = short_sym(value)
  "-#{value}"
end

#short_hyphen?(value, strict: false) ⇒ Boolean

Returns whether value is an hyphened option short.

Parameters:

  • strict (Boolean) (defaults to: false)

    whether hyphen is required when declaring an option short

Returns:

  • (Boolean)

    whether value is an hyphened option short



44
45
46
# File 'lib/rake-commander/options/name.rb', line 44

def short_hyphen?(value, strict: false)
  short?(value, strict: strict) && single_hypen(value)
end

#short_sym(value) ⇒ Symbol, NilClass

Converter

Examples:

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

Returns:

  • (Symbol, NilClass)


58
59
60
61
62
63
# File 'lib/rake-commander/options/name.rb', line 58

def short_sym(value)
  return nil unless value
  value = value.to_s.gsub(HYPHEN_START_REGEX, '')
  return nil unless value = value.chars.first
  value.to_sym
end

#single_hyphen?(value) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/rake-commander/options/name.rb', line 15

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)


36
37
38
39
40
# File 'lib/rake-commander/options/name.rb', line 36

def valid_name?(value, strict: false)
  return false unless value.respond_to?(:to_s) && !value.to_s.empty?
  return false unless !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)


28
29
30
31
32
# File 'lib/rake-commander/options/name.rb', line 28

def valid_short?(value, strict: false)
  return false unless value.respond_to?(:to_s) && !value.to_s.empty?
  return false unless !strict || single_hypen(value)
  short_sym(value).to_s.length == 1
end