Class: Cl::Opt::Validator

Inherits:
Struct
  • Object
show all
Defined in:
lib/cl/opt.rb

Constant Summary collapse

SHORT =
/^-\w( \w+)?$/
LONG =
/^--[\w\-\[\]]+( \[?\w+\]?)?$/
MSGS =
{
  missing_strs: 'No option strings given. Pass one short -s and/or one --long option string.',
  wrong_strs:   'Wrong option strings given. Pass one short -s and/or one --long option string.',
  invalid_strs: 'Invalid option strings given: %p',
  unknown_opts: 'Unknown options: %s'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



232
233
234
# File 'lib/cl/opt.rb', line 232

def opts
  @opts
end

#strsObject

Returns the value of attribute strs

Returns:

  • (Object)

    the current value of strs



232
233
234
# File 'lib/cl/opt.rb', line 232

def strs
  @strs
end

Instance Method Details

#applyObject



243
244
245
246
247
248
249
# File 'lib/cl/opt.rb', line 243

def apply
  error :missing_strs if strs.empty?
  error :wrong_strs if short.size > 1 || long.size > 1
  error :invalid_strs, invalid unless invalid.empty?
  error :unknown_opts, unknown.map(&:inspect).join(', ') unless unknown.empty?
  [short.first, long.first]
end

#error(key, *args) ⇒ Object

Raises:



271
272
273
# File 'lib/cl/opt.rb', line 271

def error(key, *args)
  raise Cl::Error, MSGS[key] % args
end

#invalidObject



255
256
257
# File 'lib/cl/opt.rb', line 255

def invalid
  @invalid ||= strs.-(valid).join(', ')
end

#longObject



267
268
269
# File 'lib/cl/opt.rb', line 267

def long
  strs.grep(LONG)
end

#shortObject



263
264
265
# File 'lib/cl/opt.rb', line 263

def short
  strs.grep(SHORT)
end

#unknownObject



251
252
253
# File 'lib/cl/opt.rb', line 251

def unknown
  @unknown ||= opts.keys - Opt::OPTS
end

#validObject



259
260
261
# File 'lib/cl/opt.rb', line 259

def valid
  strs.grep(Regexp.union(SHORT, LONG))
end