Class: Cl::Opt::Validator
- Inherits:
-
Struct
- Object
- Struct
- Cl::Opt::Validator
- 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
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#strs ⇒ Object
Returns the value of attribute strs.
Instance Method Summary collapse
- #apply ⇒ Object
- #error(key, *args) ⇒ Object
- #invalid ⇒ Object
- #long ⇒ Object
- #short ⇒ Object
- #unknown ⇒ Object
- #valid ⇒ Object
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts
232 233 234 |
# File 'lib/cl/opt.rb', line 232 def opts @opts end |
#strs ⇒ Object
Returns the value of attribute strs
232 233 234 |
# File 'lib/cl/opt.rb', line 232 def strs @strs end |
Instance Method Details
#apply ⇒ Object
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
271 272 273 |
# File 'lib/cl/opt.rb', line 271 def error(key, *args) raise Cl::Error, MSGS[key] % args end |
#invalid ⇒ Object
255 256 257 |
# File 'lib/cl/opt.rb', line 255 def invalid @invalid ||= strs.-(valid).join(', ') end |
#long ⇒ Object
267 268 269 |
# File 'lib/cl/opt.rb', line 267 def long strs.grep(LONG) end |
#short ⇒ Object
263 264 265 |
# File 'lib/cl/opt.rb', line 263 def short strs.grep(SHORT) end |