class Raph::Raph

Ruby Argument Parser for Humans. Parses arguments using external parsers.

class Parser1 < BaseParser
  def id
    'parser1_result'
  end

  def parse(arg)
    # return your parsed arguments
  end
end

class Parser2 < Base
  def id
    'parser2_result'
  end

  def parse(arg)
    # return your parsed arguments
  end
end

raph = Raph.new.tap do |r|
  r.add_parser(Parser1.new)
  r.add_parser(Parser2.new)
  # ...

  r.parse (arguments)
end

puts raph.parser1_result # parsed arguments by Parser1
puts raph.parser2_result # parsed arguments by Parser2

Public Class Methods

new() click to toggle source

Initializes Raph.

# File lib/raph.rb, line 44
def initialize
  @parsed = {}
  @parsers = []
end

Public Instance Methods

add_parser(parser) click to toggle source

Adds new external parser to parser list.

parser

external parser.

# File lib/raph.rb, line 71
def add_parser(parser)
  @parsers.push parser
end
all() click to toggle source

Returns all arguments.

# File lib/raph.rb, line 63
def all
  @all.dup
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/raph.rb, line 75
def method_missing(method_sym, *arguments, &block)
  if has_attribute? method_sym
    raise 'Arguments not applicable' if arguments.length > 0
    raise 'Block not applicable' if block_given?
    get_attribute_value method_sym
  else
    super
  end
end
parse(args) click to toggle source

Parses arguments using external parsers.

args

arguments to be parsed.

# File lib/raph.rb, line 53
def parse(args)
  @all = args.dup

  @parsers.each do |p|
    @parsed[p.id.to_sym] = p.parse(@all)
  end
end

Private Instance Methods

get_attribute_value(arg) click to toggle source

Returns value of dynamic argument arg

# File lib/raph.rb, line 95
def get_attribute_value(arg)
  @parsed[arg]
end
has_attribute?(arg) click to toggle source

Returns true if this class has dynamic argument arg.

# File lib/raph.rb, line 89
def has_attribute?(arg)
  @parsed.include? arg
end