Class: OptParseSimple

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/optparse-simple.rb

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ OptParseSimple

Returns a new instance of OptParseSimple.



13
14
15
16
17
18
19
# File 'lib/optparse-simple.rb', line 13

def initialize(s)    
  super()
  buffer = readx(s)

  @doc = Document.new(buffer)
  #@doc = Rexle.new(buffer)
end

Instance Method Details

#helpObject



97
98
99
100
101
102
103
104
# File 'lib/optparse-simple.rb', line 97

def help    
  a = XPath.match(@doc.root,  "records/optionx/summary[switch != 'n/a']").map do |summary|
  #a = @doc.root.xpath("records/optionx/summary[switch != 'n/a']").map do |summary|
    %w(switch alias).map {|x| summary.text x}
  end

  puts TableFormatter.new(source: a, border: false).to_s
end

#parse(args) ⇒ Object



21
22
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
# File 'lib/optparse-simple.rb', line 21

def parse(args)

  @options = XPath.match(@doc.root, 'records/optionx[summary/switch!="n/a"]')
  #@options = @doc.root.xpath('records/optionx[summary/switch!="n/a"]')

  switches = @options.map do |option|

    switch = option.text('summary/switch')
    switch[0] == '-' ? switch : nil
  end    

  switches.compact!
  
  # split the argument switches if grouped e.g. -ltr
  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!

  # -- bind any loose value to their argument
  xpath = 'records/optionx/summary[switch != "n/a" and value != "n/a"]'
  options = XPath.match(@doc.root, xpath)\
  #options = @doc.root.xpath(xpath)       .map {|node|  %w(switch alias)         .map{|x| node.text(x)}.compact}       .flatten
  puts 'after flatten 2'
  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!

  # -- end of bind any loose value to their argument

  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)
  #options_remaining = @doc.root.xpath('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)
  #mandatory_remaining  = @doc.root.xpath('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}']")
     #option = @doc.root.element("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

#to_hObject



93
94
95
# File 'lib/optparse-simple.rb', line 93

def to_h()
  @h
end