23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/optparse-simple.rb', line 23
def parse(args)
@options = XPath.match(@doc.root, 'records/optionx[summary/switch!="n/a"]')
switches = @options.map do |option|
puts 'option: ' + option.to_s.inspect if @debug
switch = option.text('summary/switch')
next if switch.nil?
switch[0] == '-' ? switch : nil
end
switches.compact!
args.map! do |arg|
if arg[/^\-[a-zA-Z]+$/] and switches.grep(/#{arg}/).empty? then
arg[1..-1].scan(/./).map {|x| '-' + x}
else
arg
end
end
args.flatten!
xpath = 'records/optionx/summary[switch != "n/a" and value != "n/a"]'
options = XPath.match(@doc.root, xpath)\
.map {|node| %w(switch alias)\
.map{|x| node.text(x)}.compact}\
.flatten
args.map!.with_index do |x,i|
next unless x or i < args.length - 1
(x += '=' + args[i+1]; args[i+1] = nil) if options.include?(x)
x
end
args.compact!
a1 = []
a1 = options_match(@options[0], args).flatten.each_slice(2).map {|x| x if x[0]}.compact unless @options.empty?
options_remaining = XPath.match(@doc.root, 'records/optionx/summary[switch="n/a"]/name/text()').map(&:to_s)
mandatory_remaining = XPath.match(@doc.root, 'records/optionx/summary[switch="n/a" and mandatory="true"]/name/text()').map(&:to_s)
if mandatory_remaining.length > args.length then
missing_arg = (mandatory_remaining - args).first
option = XPath.first(@doc.root, "records/optionx[summary/name='#{missing_arg}']")
raise option.text('records/errorx/summary/msg') || 'missing arg'
end
a2 = args.zip(options_remaining).map(&:reverse)
if a2.map(&:first).all? then
@h = Hash[*(a1+a2).map{|x,y| [x.to_s.strip.to_sym, y || true]}.flatten]
else
invalid_option = a2.detect {|x,y| x.nil? }.last
raise "invalid option: %s not recognised" % invalid_option
end
@h
end
|