class TTY::Option::Parameter::Option

Constants

LONG_ARGUMENT_OPTIONAL_RE

Matches “–foo [string]”

LONG_ARGUMENT_REQUIRED_RE

Matches “–foo string”

SHORT_ARGUMENT_OPTIONAL_RE

Matches “-f [string]”

SHORT_ARGUMENT_REQUIRED_RE

Matches “-f string”

Public Instance Methods

<=>(other) click to toggle source

Compare this option short and long names

@api public

# File lib/tty/option/parameter/option.rb, line 91
def <=>(other)
  left = long? ? long_name : short_name
  right = other.long? ? other.long_name : other.short_name
  left <=> right
end
argument_optional?() click to toggle source

Check if argument is optional

@return [Boolean]

@api public

# File lib/tty/option/parameter/option.rb, line 83
def argument_optional?
  !short.to_s.match(SHORT_ARGUMENT_OPTIONAL_RE).nil? ||
    !long.to_s.match(LONG_ARGUMENT_OPTIONAL_RE).nil?
end
argument_required?() click to toggle source

Check if argument is required

@return [Boolean]

@api public

# File lib/tty/option/parameter/option.rb, line 73
def argument_required?
  !short.to_s.match(SHORT_ARGUMENT_REQUIRED_RE).nil? ||
    !long.to_s.match(LONG_ARGUMENT_REQUIRED_RE).nil?
end
default_long() click to toggle source
# File lib/tty/option/parameter/option.rb, line 53
def default_long
  "--#{key.to_s.gsub("_", "-")}" unless short?
end
default_name() click to toggle source

Return long name if present, otherwise short name

@api private

# File lib/tty/option/parameter/option.rb, line 22
def default_name
  [long_name, short_name].reject(&:empty?).first
end
long(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter/option.rb, line 45
def long(value = (not_set = true))
  if not_set
    @settings.fetch(:long) { default_long }
  else
    @settings[:long] = value
  end
end
long?() click to toggle source
# File lib/tty/option/parameter/option.rb, line 57
def long?
  !long.nil?
end
long_name() click to toggle source

Extract long flag name

@api public

# File lib/tty/option/parameter/option.rb, line 64
def long_name
  long.to_s.sub(/^(--.+?)(\s+|\=|\[).*$/, "\\1")
end
short(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter/option.rb, line 26
def short(value = (not_set = true))
  if not_set
    @settings[:short]
  else
    @settings[:short] = value
  end
end
short?() click to toggle source
# File lib/tty/option/parameter/option.rb, line 34
def short?
  @settings.key?(:short) && !@settings[:short].nil?
end
short_name() click to toggle source

Extract short flag name

@api public

# File lib/tty/option/parameter/option.rb, line 41
def short_name
  short.to_s.sub(/^(-.).*$/, "\\1")
end