class Lab42::Rgxargs

Constants

Predefined
VERSION

Attributes

allowed[R]
args[R]
conversions[R]
defaults[R]
defined_rules[R]
errors[R]
options[R]
required[R]
syntaxes[R]

Public Class Methods

new(&blk) click to toggle source
# File lib/lab42/rgxargs.rb, line 69
def initialize &blk
  @args          = []
  @allowed       = nil
  @conversions   = {}
  @defaults      = {}
  @defined_rules = []
  @errors        = []
  @required      = ::Set.new
  @syntaxes      = []

  instance_exec(&blk) if blk
  @options       = ::OpenStruct.new(defaults)
end

Public Instance Methods

add_conversion(param, conversion=nil, required=nil, &block) click to toggle source
# File lib/lab42/rgxargs.rb, line 24
def add_conversion(param, conversion=nil, required=nil, &block)
  case conversion
  when Symbol
    _add_symbolic_conversion(param, conversion, required)
  when NilClass
    _add_simple_conversion(param, block)
  else
    _add_proc_conversion(param, conversion, block, required)
  end
end
add_syntax(rgx, parser=nil, as: nil) click to toggle source
# File lib/lab42/rgxargs.rb, line 35
def add_syntax(rgx, parser=nil, as: nil)
  case rgx
  when Symbol, Regexp
    syntaxes << ArgumentMatcher.new(rgx, parser, arg_name: as)
  when Array
    rgx.each do |rg1|
      add_syntax( rg1, parser, as: as)
    end
  end
end
allows(name, matcher=nil, &converter) click to toggle source
# File lib/lab42/rgxargs.rb, line 59
def allows name, matcher=nil, &converter
  add_conversion(name, matcher, &converter)
end
define_arg(name, &blk) click to toggle source
# File lib/lab42/rgxargs.rb, line 46
def define_arg name, &blk
  SyntaxDefiner.new(self, name).run(blk)
end
needs(name, matcher=nil, &converter) click to toggle source
# File lib/lab42/rgxargs.rb, line 63
def needs name, matcher=nil, &converter
  add_conversion(name, matcher, :required, &converter)
end
parse(argv) click to toggle source
# File lib/lab42/rgxargs.rb, line 50
def parse argv
  until argv.empty?
    argv = _parse_next argv
  end
  _check_required_kwds
  [options, args, errors]
end

Private Instance Methods

_add_proc_conversion(param, conversion, block, required) click to toggle source
# File lib/lab42/rgxargs.rb, line 102
def _add_proc_conversion(param, conversion, block, required)
  Array(param).each do |para|
    @required.add para if required == :required
    conversions[para] =  block ? [conversion, block] : conversion
  end
end
_add_simple_conversion(param, block) click to toggle source
# File lib/lab42/rgxargs.rb, line 109
def _add_simple_conversion(param, block)
  Array(param).each do |para|
    conversions[para] = block
  end
end
_add_symbolic_conversion(param, conversion, required) click to toggle source
# File lib/lab42/rgxargs.rb, line 115
def _add_symbolic_conversion(param, conversion, required)
  Array(param).each do |para|
    @required.add para if required == :required
    conversions[para] = Predefined.fetch(conversion, conversion)
  end
end
_convert(value, name:) click to toggle source
# File lib/lab42/rgxargs.rb, line 83
def _convert(value, name:)
  conv = conversions.fetch(name, nil)
  case conv
  when Symbol
    value.send conv
  when Proc
    conv.(value)
  when Array
    if (match = conv.first.match(value))
      conv[1].(*match.captures)
    else
      errors << [:syntax_error, name, "#{value} does not match #{conv.first}"]
      nil
    end
  else
    value
  end
end
_parse_next(argv) click to toggle source
# File lib/lab42/rgxargs.rb, line 122
def _parse_next argv
  first, *rest = argv
  if first == '--'
    @args += rest
    return []
  end
  _parse_symbolic first, rest
end
_parse_symbolic(first, rest) click to toggle source
# File lib/lab42/rgxargs.rb, line 131
def _parse_symbolic first, rest
  case first
  when %r{\A:(.*)}
    switch = $1.gsub('-','_').to_sym
    _check_switch(switch)
    options[switch]=true
    rest
  when %r{(.*):\z}
    kwd = $1.gsub('-', '_').to_sym
    _check_kwd(kwd)
    _parse_value kwd, rest
  when %r{\A\\(.*)}
    args << $1
    rest
  else
    _parse_syntax(first)
    rest
  end
end
_parse_syntax(first) click to toggle source
# File lib/lab42/rgxargs.rb, line 151
def _parse_syntax first
  value, as = syntaxes.find_value(first){ |matcher| matcher.match(first) }
  if as
    options[as] = value
  else
    args << value
  end
end
_parse_value(name, rest) click to toggle source
# File lib/lab42/rgxargs.rb, line 160
def _parse_value name, rest
  value, *rest1 = rest
  if value
    options[name] = _convert(value, name: name)
    return rest1
  end
  errors << [:missing_required_value, name]
  []
end