Class: Cl::Parser
- Inherits:
-
OptionParser
- Object
- OptionParser
- Cl::Parser
- Defined in:
- lib/cl/parser.rb,
lib/cl/parser/format.rb
Defined Under Namespace
Classes: Format
Constant Summary collapse
- DASHERIZE =
/^--([^= ])*/
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #dasherize(strs) ⇒ Object
-
#initialize(cmd, args) ⇒ Parser
constructor
A new instance of Parser.
- #long?(str) ⇒ Boolean
- #negation(opts, arg) ⇒ Object
- #noize(opts, args) ⇒ Object
- #normalize(opts, args) ⇒ Object
- #opt_name(str) ⇒ Object
-
#set(opt, str, value) ⇒ Object
should consider negative arities (e.g. |one, *two|).
Constructor Details
#initialize(cmd, args) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cl/parser.rb', line 8 def initialize(cmd, args) @cmd = cmd @opts = {} opts = cmd.class.opts super do opts.each do |opt| Format.new(opt).strs.each do |str| on(str) { |value| set(opt, str, value) } end end end @args = parse!(normalize(opts, args)) end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
6 7 8 |
# File 'lib/cl/parser.rb', line 6 def args @args end |
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
6 7 8 |
# File 'lib/cl/parser.rb', line 6 def cmd @cmd end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/cl/parser.rb', line 6 def opts @opts end |
Instance Method Details
#dasherize(strs) ⇒ Object
55 56 57 58 59 |
# File 'lib/cl/parser.rb', line 55 def dasherize(strs) strs.map do |str| str.is_a?(String) ? str.gsub(DASHERIZE) { |opt| opt.gsub('_', '-') } : str end end |
#long?(str) ⇒ Boolean
61 62 63 |
# File 'lib/cl/parser.rb', line 61 def long?(str) str.start_with?('--') end |
#negation(opts, arg) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/cl/parser.rb', line 46 def negation(opts, arg) opts.select(&:flag?).detect do |opt| str = opt.negate.detect { |str| arg =~ /^--#{str}[-_]+#{opt.name}/ } break str if str end end |
#noize(opts, args) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/cl/parser.rb', line 39 def noize(opts, args) args.map do |arg| str = negation(opts, arg) str ? arg.sub(/^--#{str}[-_]+/, '--no-') : arg end end |
#normalize(opts, args) ⇒ Object
34 35 36 37 |
# File 'lib/cl/parser.rb', line 34 def normalize(opts, args) args = noize(opts, args) dasherize(args) end |
#opt_name(str) ⇒ Object
65 66 67 |
# File 'lib/cl/parser.rb', line 65 def opt_name(str) str.split(' ').first.sub(/--(\[no[_\-]\])?/, '').to_sym end |
#set(opt, str, value) ⇒ Object
should consider negative arities (e.g. |one, *two|)
25 26 27 28 29 30 31 32 |
# File 'lib/cl/parser.rb', line 25 def set(opt, str, value) name = long?(str) ? opt_name(str) : opt.name value = true if value.nil? && opt.flag? args = [opts, opt.type, name, value] args = args[-opt.block.arity, opt.block.arity] instance_exec(*args, &opt.block) cmd.deprecations.update([opt.deprecated].to_h) if opt.deprecated?(name) end |