Class: TTY::Option::Parameter::Option

Inherits:
TTY::Option::Parameter show all
Defined in:
lib/tty/option/parameter/option.rb

Constant Summary collapse

SHORT_ARGUMENT_REQUIRED_RE =

Matches “-f string”

/^-.(\s*?|\=)[^\[]+$/.freeze
LONG_ARGUMENT_REQUIRED_RE =

Matches “–foo string”

/^--\S+(\s+|\=)([^\[])+?$/.freeze
SHORT_ARGUMENT_OPTIONAL_RE =

Matches “-f [string]”

/^-.\s*\[\S+\]\s*$/.freeze
LONG_ARGUMENT_OPTIONAL_RE =

Matches “–foo [string]”

/^--\S+\s*\[\S+\]\s*$/.freeze

Constants inherited from TTY::Option::Parameter

ONE_OR_MORE_ARITY, ZERO_OR_MORE_ARITY

Instance Attribute Summary

Attributes inherited from TTY::Option::Parameter

#key

Instance Method Summary collapse

Methods inherited from TTY::Option::Parameter

#==, #arity, #convert, #convert?, create, #default, #default?, #default_arity, #desc, #desc?, #display?, #dup, #eql?, #hidden, #hidden?, #initialize, #min_arity, #multi_argument?, #multiple?, #name, #optional, #optional?, #permit, #permit?, #required, #required?, #to_h, #to_sym, #validate, #validate?

Methods included from DSL::Conversion

#list_of, #map_of

Methods included from DSL::Arity

#at_least, #one, #one_or_more, #two, #two_or_more, #zero_or_more

Constructor Details

This class inherits a constructor from TTY::Option::Parameter

Instance Method Details

#<=>(other) ⇒ Object

Compare this option short and long names



91
92
93
94
95
# 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?Boolean

Check if argument is optional

Returns:

  • (Boolean)


83
84
85
86
# 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?Boolean

Check if argument is required

Returns:

  • (Boolean)


73
74
75
76
# 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_longObject



53
54
55
# File 'lib/tty/option/parameter/option.rb', line 53

def default_long
  "--#{key.to_s.gsub("_", "-")}" unless short?
end

#default_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return long name if present, otherwise short name



22
23
24
# 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)) ⇒ Object



45
46
47
48
49
50
51
# 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?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/tty/option/parameter/option.rb', line 57

def long?
  !long.nil?
end

#long_nameObject

Extract long flag name



64
65
66
# File 'lib/tty/option/parameter/option.rb', line 64

def long_name
  long.to_s.sub(/^(--.+?)(\s+|\=|\[).*$/, "\\1")
end

#short(value = (not_set = true)) ⇒ Object



26
27
28
29
30
31
32
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/tty/option/parameter/option.rb', line 34

def short?
  @settings.key?(:short) && !@settings[:short].nil?
end

#short_nameObject

Extract short flag name



41
42
43
# File 'lib/tty/option/parameter/option.rb', line 41

def short_name
  short.to_s.sub(/^(-.).*$/, "\\1")
end