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



230
231
232
# File 'lib/cl/opt.rb', line 230

def opts
  @opts
end

#strsObject

Returns the value of attribute strs

Returns:

  • (Object)

    the current value of strs



230
231
232
# File 'lib/cl/opt.rb', line 230

def strs
  @strs
end

Instance Method Details

#applyObject



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

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:



269
270
271
# File 'lib/cl/opt.rb', line 269

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

#invalidObject



253
254
255
# File 'lib/cl/opt.rb', line 253

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

#longObject



265
266
267
# File 'lib/cl/opt.rb', line 265

def long
  strs.grep(LONG)
end

#shortObject



261
262
263
# File 'lib/cl/opt.rb', line 261

def short
  strs.grep(SHORT)
end

#unknownObject



249
250
251
# File 'lib/cl/opt.rb', line 249

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

#validObject



257
258
259
# File 'lib/cl/opt.rb', line 257

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