Class: TTY::Option::Parameter

Inherits:
Object
  • Object
show all
Includes:
Comparable, DSL::Arity, DSL::Conversion
Defined in:
lib/tty/option/parameter.rb,
lib/tty/option/parameter/option.rb,
lib/tty/option/parameter/keyword.rb,
lib/tty/option/parameter/argument.rb,
lib/tty/option/parameter/environment.rb

Direct Known Subclasses

Argument, Environment, Keyword, Option

Defined Under Namespace

Classes: Argument, Environment, Keyword, Option

Constant Summary collapse

ZERO_OR_MORE_ARITY =

Zero or more parameter arity pattern

/\*|any/.freeze
ONE_OR_MORE_ARITY =

One or more parameter arity pattern

/\+/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize(key, **settings, &block) ⇒ Parameter

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.

Create a parameter

Parameters:

  • key (Symbol)

    the key to register this param under



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tty/option/parameter.rb', line 38

def initialize(key, **settings, &block)
  @key = key
  @settings = {}
  settings.each do |name, val|
    case name.to_sym
    when :arity
      val = check_arity(val)
    when :default
      val = check_default(val)
    when :optional
      name, val = :required, check_required(!val)
    when :permit
      val = check_permitted(val)
    when :validate
      val = check_validation(val)
    end
    @settings[name.to_sym] = val
  end

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#keyObject (readonly)

The key under which this parameter is registered



30
31
32
# File 'lib/tty/option/parameter.rb', line 30

def key
  @key
end

Class Method Details

.create(key, **settings, &block) ⇒ Object

A parameter factory



23
24
25
# File 'lib/tty/option/parameter.rb', line 23

def self.create(key, **settings, &block)
  new(key, **settings, &block)
end

Instance Method Details

#<=>(other) ⇒ Object

Compare this parameter name with the other



203
204
205
# File 'lib/tty/option/parameter.rb', line 203

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Object

Compare parameters for equality based on type and name



210
211
212
213
214
# File 'lib/tty/option/parameter.rb', line 210

def ==(other)
  return false unless instance_of?(other.class)

  name == other.name && to_h == other.to_h
end

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



60
61
62
63
64
65
66
# File 'lib/tty/option/parameter.rb', line 60

def arity(value = (not_set = true))
  if not_set
    @settings.fetch(:arity) { default_arity }
  else
    @settings[:arity] = check_arity(value)
  end
end

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



88
89
90
91
92
93
94
# File 'lib/tty/option/parameter.rb', line 88

def convert(value = (not_set = true))
  if not_set
    @settings[:convert]
  else
    @settings[:convert] = value
  end
end

#convert?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tty/option/parameter.rb', line 96

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

#default(value = (not_set = true)) ⇒ Object Also known as: defaults



100
101
102
103
104
105
106
# File 'lib/tty/option/parameter.rb', line 100

def default(value = (not_set = true))
  if not_set
    @settings[:default]
  else
    @settings[:default] = check_default(value)
  end
end

#default?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/tty/option/parameter.rb', line 109

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

#default_arityObject



68
69
70
# File 'lib/tty/option/parameter.rb', line 68

def default_arity
  1
end

#default_nameObject



180
181
182
# File 'lib/tty/option/parameter.rb', line 180

def default_name
  key.to_s.tr("_", "-")
end

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



113
114
115
116
117
118
119
# File 'lib/tty/option/parameter.rb', line 113

def desc(value = (not_set = true))
  if not_set
    @settings[:desc]
  else
    @settings[:desc] = value
  end
end

#desc?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/tty/option/parameter.rb', line 121

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

#display?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/tty/option/parameter.rb', line 156

def display?
  desc? && !hidden?
end

#dupObject

Make a duplicate of this parameter



244
245
246
247
248
249
# File 'lib/tty/option/parameter.rb', line 244

def dup
  super.tap do |param|
    param.instance_variable_set(:@key, DeepDup.deep_dup(@key))
    param.instance_variable_set(:@settings, DeepDup.deep_dup(@settings))
  end
end

#eql?(other) ⇒ Boolean

Compare parameters for equality based on type and name

Returns:

  • (Boolean)


219
220
221
222
223
# File 'lib/tty/option/parameter.rb', line 219

def eql?(other)
  return false unless instance_of?(other.class)

  name.eql?(other.name) && to_h.eql?(other.to_h)
end

#hiddenObject



148
149
150
# File 'lib/tty/option/parameter.rb', line 148

def hidden
  @settings[:hidden] = true
end

#hidden?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/tty/option/parameter.rb', line 152

def hidden?
  @settings.fetch(:hidden, false)
end

#min_arityObject

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.

Determine minimum boundary for arity parameter



75
76
77
# File 'lib/tty/option/parameter.rb', line 75

def min_arity
  arity < 0 ? arity.abs - 1 : arity
end

#multi_argument?Boolean

Check if this options is multi argument

Returns:

  • (Boolean)


128
129
130
# File 'lib/tty/option/parameter.rb', line 128

def multi_argument?
  !convert.to_s.match(/list|array|map|hash|\w+s/).nil?
end

#multiple?Boolean

Check if multiple occurrences are allowed

Returns:

  • (Boolean)


84
85
86
# File 'lib/tty/option/parameter.rb', line 84

def multiple?
  arity < 0 || 1 < arity.abs
end

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



172
173
174
175
176
177
178
# File 'lib/tty/option/parameter.rb', line 172

def name(value = (not_set = true))
  if not_set
    @settings.fetch(:name) { default_name }
  else
    @settings[:name] = value
  end
end

#optionalObject



132
133
134
# File 'lib/tty/option/parameter.rb', line 132

def optional
  @settings[:required] = false
end

#optional?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/tty/option/parameter.rb', line 136

def optional?
  !required?
end

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



160
161
162
163
164
165
166
# File 'lib/tty/option/parameter.rb', line 160

def permit(value = (not_set = true))
  if not_set
    @settings[:permit]
  else
    @settings[:permit] = check_permitted(value)
  end
end

#permit?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/tty/option/parameter.rb', line 168

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

#requiredObject



140
141
142
# File 'lib/tty/option/parameter.rb', line 140

def required
  @settings[:required] = check_required(true)
end

#required?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/tty/option/parameter.rb', line 144

def required?
  @settings.fetch(:required, false)
end

#to_h(&block) ⇒ Hash

Return a hash of this parameter settings

Returns:

  • (Hash)

    the names and values of this parameter



230
231
232
233
234
235
236
237
238
239
# File 'lib/tty/option/parameter.rb', line 230

def to_h(&block)
  if block_given?
    @settings.each_with_object({}) do |(key, val), acc|
      k, v = *block.(key, val)
      acc[k] = v
    end
  else
    DeepDup.deep_dup(@settings)
  end
end

#to_symObject



196
197
198
# File 'lib/tty/option/parameter.rb', line 196

def to_sym
  self.class.name.to_s.split(/::/).last.downcase.to_sym
end

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



184
185
186
187
188
189
190
# File 'lib/tty/option/parameter.rb', line 184

def validate(value = (not_set = true))
  if not_set
    @settings[:validate]
  else
    @settings[:validate] = check_validation(value)
  end
end

#validate?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/tty/option/parameter.rb', line 192

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