Class: Cl::Parser
- Inherits:
-
OptionParser
- Object
- OptionParser
- Cl::Parser
- Defined in:
- lib/cl/parser.rb
Constant Summary collapse
- DASHERIZE =
/^--([^= ])*/
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #aliased(opt, name) ⇒ Object
- #args_for(opt, strs) ⇒ Object
- #dasherize(strs) ⇒ Object
- #flagerize(strs) ⇒ Object
-
#initialize(opts, args) ⇒ Parser
constructor
A new instance of Parser.
- #negation(opts, arg) ⇒ Object
- #noize(opts, args) ⇒ Object
- #normalize(opts, args) ⇒ Object
-
#set(opt, value) ⇒ Object
should consider negative arities (e.g. |one, *two|).
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(*args_for(opt, opt.strs)) do |value| set(opt, value) end opt.aliases.each do |name| on(*args_for(opt, [aliased(opt, name)])) do |value| @opts[name] = set(opt, value) end end end end args.replace(normalize(opts, 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
34 35 36 37 38 |
# File 'lib/cl/parser.rb', line 34 def aliased(opt, name) str = opt.strs.detect { |str| str.start_with?('--') } || raise str = str.sub(opt.name.to_s, name.to_s) str.sub(opt.name.to_s.gsub('_', '-'), name.to_s) end |
#args_for(opt, strs) ⇒ Object
28 29 30 31 32 |
# File 'lib/cl/parser.rb', line 28 def args_for(opt, strs) args = dasherize(strs) args = flagerize(args) if opt.flag? args end |
#dasherize(strs) ⇒ Object
69 70 71 72 73 |
# File 'lib/cl/parser.rb', line 69 def dasherize(strs) strs.map do |str| str.is_a?(String) ? str.gsub(DASHERIZE) { |opt| opt.gsub('_', '-') } : str end end |
#flagerize(strs) ⇒ Object
75 76 77 78 |
# File 'lib/cl/parser.rb', line 75 def flagerize(strs) strs = strs.map { |str| str.include?(' ') ? str : "#{str} [true|false|yes|no]" } strs << TrueClass end |
#negation(opts, arg) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/cl/parser.rb', line 60 def negation(opts, arg) opts.detect do |opt| str = opt.negate.detect { |str| arg =~ /^--#{str}[-_]+#{opt.name}/ } break str if str end end |
#noize(opts, args) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/cl/parser.rb', line 53 def noize(opts, args) args.map do |arg| str = negation(opts, arg) str ? arg.sub(/^--#{str}[-_]+/, '--no-') : arg end end |
#normalize(opts, args) ⇒ Object
48 49 50 51 |
# File 'lib/cl/parser.rb', line 48 def normalize(opts, args) args = noize(opts, args) dasherize(args) end |
#set(opt, value) ⇒ Object
should consider negative arities (e.g. |one, *two|)
41 42 43 44 45 46 |
# File 'lib/cl/parser.rb', line 41 def set(opt, value) value = true if value.nil? && opt.flag? args = [opts, opt.type, opt.name, value] args = args[-opt.block.arity, opt.block.arity] instance_exec(*args, &opt.block) end |