Class: Ki::SimpleOptionParser

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

Overview

simplified OptionParser, which supports unknown options by ignoring them

  • supports options in two formats: “-f file.txt” and “-f=file.txt”

  • supports short and long form: “–file” and “-f”

  • supports multiple parameters for options

Does not support

  • parameters with spaces

  • type conversions

  • optional parameter values

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ SimpleOptionParser

Returns a new instance of SimpleOptionParser.



31
32
33
# File 'lib/util/simple_optparse.rb', line 31

def initialize(&block)
  block.call(self)
end

Instance Method Details

#find_option(a) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/util/simple_optparse.rb', line 89

def find_option(a)
  options.each do |o|
    if a.start_with?(o[:opt] + "=")
      return o, a[o[:opt].size+1..-1]
    elsif a.start_with?(o[:opt])
      return o,nil
    end
  end
  nil
end

#on(*args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/util/simple_optparse.rb', line 35

def on(*args, &block)
  if block.nil?
    raise "Option without parser block: " + args.join(", ")
  end
  if args.size == 2 || args.size == 3
    short = args.size == 3 ? args.delete_at(0) : nil
    long, *params = args.delete_at(0).split(" ")
    comment = args.delete_at(0)
    options_for_to_s << {short: short, long: long, comment: comment, params: params, block: block }
    if short
      options << {opt: short, comment: comment, params: params, block: block }
    end
    options << {opt: long, comment: comment, params: params, block: block }
  else
    raise "unsupported option configuration size: " + args.join(", ")
  end
end

#parse(args) ⇒ Object



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
# File 'lib/util/simple_optparse.rb', line 53

def parse(args)
  ret = []
  open = nil
  collected_params = nil
  collect_count = nil
  args.each do |a|
    if open
      collected_params << a
      if collect_count == collected_params.size
        open[:block].call(*collected_params)
        open = nil
      end
    else
      found_option, rest_of_a = find_option(a)
      if found_option
        collect_count = found_option[:params].size
        if collect_count == 0
          # no parameters
          found_option[:block].call
        elsif collect_count == 1 && rest_of_a && rest_of_a.size > 0
          # single parameter was defined with opt=value
          found_option[:block].call rest_of_a
        else
          open = found_option
          collected_params = []
        end
      else
        ret << a
      end
    end
  end
  if open
    raise "requires #{collect_count} parameters for '#{open[:opt]}', found only #{collected_params.size}: #{collected_params.join(", ")}"
  end
  ret
end

#to_sObject



99
100
101
102
103
# File 'lib/util/simple_optparse.rb', line 99

def to_s
  options_for_to_s.map do |o|
  format("    %2s%s %-29s%s",o[:short], o[:short] && o[:long]? ",": " ", o[:long], o[:comment] )
  end.join("\n")
end