class Cl::Parser

Attributes

args[R]
cmd[R]
opts[R]

Public Class Methods

new(cmd, args) click to toggle source
Calls superclass method
# 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

Public Instance Methods

long?(str) click to toggle source

DASHERIZE = /^–([^= ])*/

def dasherize(strs)

strs.map do |str|
  str.is_a?(String) ? str.gsub(DASHERIZE) { |opt| opt.gsub('_', '-') } : str
end

end

# File lib/cl/parser.rb, line 62
def long?(str)
  str.start_with?('--')
end
negation(opts, arg) click to toggle source
# File lib/cl/parser.rb, line 47
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) click to toggle source
# File lib/cl/parser.rb, line 40
def noize(opts, args)
  args.map do |arg|
    str = negation(opts, arg)
    str ? arg.sub(/^--#{str}[-_]+/, '--no-') : arg
  end
end
normalize(opts, args) click to toggle source
# File lib/cl/parser.rb, line 34
def normalize(opts, args)
  args = noize(opts, args)
  # dasherize(args)
  args
end
opt_name(str) click to toggle source
# File lib/cl/parser.rb, line 66
def opt_name(str)
  str.split(' ').first.sub(/--(\[no[_\-]\])?/, '').to_sym
end
set(opt, str, value) click to toggle source

should consider negative arities (e.g. |one, *two|)

# 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