Class: RArg::DefinitionContext

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

Instance Method Summary collapse

Constructor Details

#initializeDefinitionContext

Returns a new instance of DefinitionContext.



32
33
34
35
36
# File 'lib/rarg.rb', line 32

def initialize
	@required = []
	@defaults = {}
	@aliases = {}
end

Instance Method Details

#parse(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rarg.rb', line 38

def parse(args)
	base = {}

	@defaults.each_pair do |name, value|
		unless args.include?(name) then
			base[name] = value
		end
	end
	
	args.each_pair do |name, value|
		# solve aliases
		if @aliases.include?(name) then
			real_name = @aliases[name]
		else
			real_name = name
		end
		
		# check defined or not
		if not @required.include?(real_name) and not @defaults.include?(real_name) then
			raise Error, "'#{real_name}' is not defined"
		end
		
		base[real_name] = value
	end
	
	if (missing = @required.find{|x| not base.include?(x)}) then
		raise Error, "argument of '#{missing}' is required, but missing"
	end
	

	
	return ParseResult.new(base)
end