Class: Cl::Opt

Inherits:
Struct
  • Object
show all
Includes:
Cast, Regex
Defined in:
lib/cl/opt.rb

Defined Under Namespace

Classes: Validator

Constant Summary collapse

OPTS =
%i(
  alias default deprecated description downcase eg enum example format
  internal max min negate note required requires secret see sep type upcase
)
OPT =
/^--(?:\[.*\])?(.*)$/
TYPES =
{
  int: :integer,
  str: :string,
  bool: :flag,
  boolean: :flag
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cast

#cast

Methods included from Regex

#format_regex

Constructor Details

#initialize(strs) ⇒ Opt

Returns a new instance of Opt.



24
25
26
27
# File 'lib/cl/opt.rb', line 24

def initialize(strs, *)
  super
  @short, @long = Validator.new(strs, opts).apply
end

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



5
6
7
# File 'lib/cl/opt.rb', line 5

def block
  @block
end

#longObject (readonly)

Returns the value of attribute long.



22
23
24
# File 'lib/cl/opt.rb', line 22

def long
  @long
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



5
6
7
# File 'lib/cl/opt.rb', line 5

def opts
  @opts
end

#shortObject (readonly)

Returns the value of attribute short.



22
23
24
# File 'lib/cl/opt.rb', line 22

def short
  @short
end

#strsObject

Returns the value of attribute strs

Returns:

  • (Object)

    the current value of strs



5
6
7
# File 'lib/cl/opt.rb', line 5

def strs
  @strs
end

Instance Method Details

#aliasesObject



71
72
73
# File 'lib/cl/opt.rb', line 71

def aliases
  Array(opts[:alias])
end

#aliases?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/cl/opt.rb', line 67

def aliases?
  !!opts[:alias]
end

#array?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/cl/opt.rb', line 63

def array?
  type == :array
end

#assign(opts, type, _, value) ⇒ Object



217
218
219
220
221
222
223
224
225
226
# File 'lib/cl/opt.rb', line 217

def assign(opts, type, _, value)
  [name, *aliases].each do |name|
    if array?
      opts[name] ||= []
      opts[name] << value
    else
      opts[name] = value
    end
  end
end

#defaultObject



101
102
103
# File 'lib/cl/opt.rb', line 101

def default
  opts[:default]
end

#default?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/cl/opt.rb', line 97

def default?
  opts.key?(:default)
end

#define(const) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cl/opt.rb', line 29

def define(const)
  return unless __key__ = name
  const.send :include, Module.new {
    define_method (__key__) { opts[__key__] }
    define_method (:"#{__key__}?") { !!opts[__key__] }
  }
end

#deprecatedObject



85
86
87
88
89
90
91
# File 'lib/cl/opt.rb', line 85

def deprecated
  # If it's a string then it's a deprecation message and the option itself
  # is considered deprecated. If it's a symbol it refers to a deprecated
  # alias, and the option's name is the deprecation message.
  return [name, opts[:deprecated]] unless opts[:deprecated].is_a?(Symbol)
  opts[:deprecated] ? [opts[:deprecated], name] : []
end

#deprecated?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/cl/opt.rb', line 79

def deprecated?(name = nil)
  return !!opts[:deprecated] if name.nil?
  names = [name.to_s.gsub('_', '-').to_sym, name.to_s.gsub('-', '_').to_sym]
  deprecated? && names.include?(deprecated.first)
end

#descriptionObject



75
76
77
# File 'lib/cl/opt.rb', line 75

def description
  opts[:description]
end

#downcase?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/cl/opt.rb', line 93

def downcase?
  !!opts[:downcase]
end

#enumObject



109
110
111
# File 'lib/cl/opt.rb', line 109

def enum
  Array(opts[:enum])
end

#enum?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/cl/opt.rb', line 105

def enum?
  !!opts[:enum]
end

#exampleObject



127
128
129
# File 'lib/cl/opt.rb', line 127

def example
  opts[:example]
end

#example?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/cl/opt.rb', line 123

def example?
  !!opts[:example]
end

#flag?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cl/opt.rb', line 55

def flag?
  type == :flag
end

#formatObject



135
136
137
# File 'lib/cl/opt.rb', line 135

def format
  format_regex(opts[:format])
end

#format?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/cl/opt.rb', line 131

def format?
  !!opts[:format]
end

#formatted?(value) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
# File 'lib/cl/opt.rb', line 139

def formatted?(value)
  return value.all? { |value| formatted?(value) } if value.is_a?(Array)
  opts[:format] =~ value
end

#help?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cl/opt.rb', line 51

def help?
  name == :help
end

#infer_typeObject



47
48
49
# File 'lib/cl/opt.rb', line 47

def infer_type
  strs.any? { |str| str.split(' ').size > 1 } ? :string : :flag
end

#int?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cl/opt.rb', line 59

def int?
  type == :int || type == :integer
end

#internal?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/cl/opt.rb', line 144

def internal?
  !!opts[:internal]
end

#known?(value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/cl/opt.rb', line 113

def known?(value)
  return value.all? { |value| known?(value) } if value.is_a?(Array)
  enum.any? { |obj| obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value }
end

#long?(str) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/cl/opt.rb', line 228

def long?(str)
  str.start_with?('--')
end

#maxObject



160
161
162
# File 'lib/cl/opt.rb', line 160

def max
  opts[:max]
end

#max?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/cl/opt.rb', line 156

def max?
  int? && !!opts[:max]
end

#minObject



152
153
154
# File 'lib/cl/opt.rb', line 152

def min
  opts[:min]
end

#min?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/cl/opt.rb', line 148

def min?
  int? && !!opts[:min]
end

#nameObject



37
38
39
40
41
# File 'lib/cl/opt.rb', line 37

def name
  return @name if instance_variable_defined?(:@name)
  name = long.split(' ').first.match(OPT)[1] if long
  @name = name.sub('-', '_').to_sym if name
end

#negateObject



168
169
170
# File 'lib/cl/opt.rb', line 168

def negate
  ['no'] + Array(opts[:negate]) if flag?
end

#negate?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/cl/opt.rb', line 164

def negate?
  !!negate
end

#noteObject



176
177
178
# File 'lib/cl/opt.rb', line 176

def note
  opts[:note]
end

#note?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/cl/opt.rb', line 172

def note?
  !!opts[:note]
end

#required?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/cl/opt.rb', line 180

def required?
  !!opts[:required]
end

#requiresObject



188
189
190
# File 'lib/cl/opt.rb', line 188

def requires
  Array(opts[:requires])
end

#requires?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/cl/opt.rb', line 184

def requires?
  !!opts[:requires]
end

#secret?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/cl/opt.rb', line 192

def secret?
  !!opts[:secret]
end

#seeObject



200
201
202
# File 'lib/cl/opt.rb', line 200

def see
  opts[:see]
end

#see?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/cl/opt.rb', line 196

def see?
  !!opts[:see]
end

#separatorObject



204
205
206
# File 'lib/cl/opt.rb', line 204

def separator
  opts[:sep]
end

#typeObject



43
44
45
# File 'lib/cl/opt.rb', line 43

def type
  @type ||= TYPES[opts[:type]] || opts[:type] || infer_type
end

#unknown(value) ⇒ Object



118
119
120
121
# File 'lib/cl/opt.rb', line 118

def unknown(value)
  return value.reject { |value| known?(value) } if value.is_a?(Array)
  known?(value) ? [] : Array(value)
end

#upcase?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/cl/opt.rb', line 208

def upcase?
  !!opts[:upcase]
end