Class: OptionParshie

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionParshie

Returns a new instance of OptionParshie.



7
8
9
10
# File 'lib/optparshie.rb', line 7

def initialize()
  super;
  hash_init();
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/optparshie.rb', line 139

def method_missing(name,*args)
  name = name.to_s();
  subst = !(name.gsub!(/=$/,"").nil?);
  raise if (!@opt_key.key?(name));

  if (!subst)
    return @opt_hash[@opt_key[name]];
  else
    return @opt_hash[@opt_key[name]] = args.first;
  end
end

Instance Attribute Details

#opt_hashObject (readonly)

Returns the value of attribute opt_hash.



151
152
153
# File 'lib/optparshie.rb', line 151

def opt_hash
  @opt_hash
end

Instance Method Details

#parse(*argv) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/optparshie.rb', line 34

def parse(*argv)
  hash_init();
  top.list.each { |sw| hash_sw_add(sw,nil); }
  x = super;
  
  return x;
end

#parse!(*argv) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/optparshie.rb', line 42

def parse!(*argv)
  hash_init();
  top.list.each { |sw| hash_sw_add(sw,nil); }
  x = super;
  
  return x;
end

#parse_in_order(argv = default_argv, setter = nil, &nonopt) ⇒ Object


Borrowed from optparse.rb

(author of optparse.rb is Nobu Nakada-san, thanks!)



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/optparshie.rb', line 54

def parse_in_order(argv = default_argv, setter = nil, &nonopt)  # :nodoc:
  opt, arg, val, rest = nil
  nonopt ||= proc {|a| throw :terminate, a}
  argv.unshift(arg) if arg = catch(:terminate) {
    while arg = argv.shift
      case arg
      # long option
      when /\A--([^=]*)(?:=(.*))?/m
        opt, rest = $1, $2
        begin
          sw, = complete(:long, opt, true)
        rescue ParseError
          raise $!.set_option(arg, true)
        end
        begin
          opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
          
          # ------------------------------------------------------------------
          # optparshie code by l.p.m.11
          # ------------------------------------------------------------------
          hash_sw_add(sw, val);
          
          val = cb.call(val) if cb
          setter.call(sw.switch_name, val) if setter
        rescue ParseError
          raise $!.set_option(arg, rest)
        end

      # short option
      when /\A-(.)((=).*|.+)?/m
        opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
        begin
          sw, = search(:short, opt)
          unless sw
            begin
              sw, = complete(:short, opt)
              # short option matched.
              val = arg.sub(/\A-/, '')
              has_arg = true
            rescue InvalidOption
              # if no short options match, try completion with long
              # options.
              sw, = complete(:long, opt)
              eq ||= !rest
            end
          end
        rescue ParseError
          raise $!.set_option(arg, true)
        end
        begin
          opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
          raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"

          # ------------------------------------------------------------------
          # optparshie code by l.p.m.11
          # ------------------------------------------------------------------
          hash_sw_add(sw, val);
          
          argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
          val = cb.call(val) if cb
          setter.call(sw.switch_name, val) if setter
        rescue ParseError
          raise $!.set_option(arg, arg.length > 2)
        end

      # non-option argument
      else
        catch(:prune) do
          visit(:each_option) do |sw0|
            sw = sw0
            sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
          end
          nonopt.call(arg)
        end
      end
    end

    nil
  }

  visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}

  argv
end