Class: Cl::Parser
- Inherits:
-
OptionParser
- Object
- OptionParser
- Cl::Parser
- Defined in:
- lib/cl/parser.rb
Constant Summary collapse
- PATTERN =
OptionParser has started accepting dasherized options in 2.4. We want to support them on any Ruby >= 2.0 version, so we’ll need to normalize things ourselves.
/^(-{1,2})(\[?no-\]?)?(.*)$/
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #aliased(opt, name) ⇒ Object
-
#initialize(opts, args) ⇒ Parser
constructor
A new instance of Parser.
-
#set(opt, value) ⇒ Object
should consider negative arities (e.g. |one, *two|).
- #underscore!(strs) ⇒ Object
Constructor Details
#initialize(opts, args) ⇒ Parser
Returns a new instance of Parser.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cl/parser.rb', line 7 def initialize(opts, args) @opts = {} super do opts.each do |opt| on(*underscore!(opt.strs)) do |value| set(opt, value) end opt.aliases.each do |name| on(aliased(opt, name)) do |value| @opts[name] = set(opt, value) end end end end underscore!(args) parse!(args) end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
5 6 7 |
# File 'lib/cl/parser.rb', line 5 def opts @opts end |
Instance Method Details
#aliased(opt, name) ⇒ Object
28 29 30 31 |
# File 'lib/cl/parser.rb', line 28 def aliased(opt, name) str = opt.strs.detect { |str| str.start_with?('--') } || raise str.sub(opt.name.to_s, name.to_s) end |
#set(opt, value) ⇒ Object
should consider negative arities (e.g. |one, *two|)
34 35 36 37 38 |
# File 'lib/cl/parser.rb', line 34 def set(opt, value) args = [opts, opt.type, opt.name, value] args = args[-opt.block.arity, opt.block.arity] instance_exec(*args, &opt.block) end |
#underscore!(strs) ⇒ Object
46 47 48 49 |
# File 'lib/cl/parser.rb', line 46 def underscore!(strs) return strs if RUBY_VERSION >= '2.4' strs.each { |str| str.gsub!(PATTERN) { "#{$1}#{$2}#{$3.tr('-', '_')}" } } end |