Class: Raph::Raph

Inherits:
Object
  • Object
show all
Defined in:
lib/raph.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeRaph

Initializes Raph.


44
45
46
47
# File 'lib/raph.rb', line 44

def initialize
  @parsed = {}
  @parsers = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object


75
76
77
78
79
80
81
82
83
# 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

Instance Method Details

#add_parser(parser) ⇒ Object

Adds new external parser to parser list.

+parser+:: external parser.


71
72
73
# File 'lib/raph.rb', line 71

def add_parser(parser)
  @parsers.push parser
end

#allObject

Returns all arguments.


63
64
65
# File 'lib/raph.rb', line 63

def all
  @all.dup
end

#parse(args) ⇒ Object

Parses arguments using external parsers.

+args+:: arguments to be parsed.


53
54
55
56
57
58
59
# 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