Class: Efl::EcoreGetopt::REcoreGetopt

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

Instance Method Summary collapse

Constructor Details

#initialize(desc) ⇒ REcoreGetopt



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/efl/ecore_getopt.rb', line 88

def initialize desc
    @ecore_getopt = nil
    @desc = desc
    @options = [
        [ 0 ],
    ]
    @values = [
        [ :ptrp, FFI::Pointer::NULL ]
    ]
    @refs = [] # to prevent FFI::MemoryPointer.from_string from beeing GC'ed
end

Instance Method Details

#<<(o) ⇒ Object



106
107
108
# File 'lib/efl/ecore_getopt.rb', line 106

def << o
    @options.insert -2, o
end

#append(short, long, help, sub_type) ⇒ Object



225
226
227
# File 'lib/efl/ecore_getopt.rb', line 225

def append short, long, help, sub_type
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_append, [:append,sub_type] ]
end

#append_metavar(short, long, help, meta, sub_type) ⇒ Object



228
229
230
# File 'lib/efl/ecore_getopt.rb', line 228

def append_metavar short, long, help, meta, sub_type
    self << [ short, long, help, meta, :ecore_getopt_action_append, [:append,sub_type] ]
end

#callback_args(short, long, help, meta, cb, data = nil) ⇒ Object



240
241
242
# File 'lib/efl/ecore_getopt.rb', line 240

def callback_args short, long, help, meta, cb, data=nil
    callback_full short, long, help, meta, cb, data, :ecore_getopt_desc_arg_requirement_yes, FFI::Pointer::NULL
end

#callback_full(short, long, help, meta, cb, data, arg_req, def_val) ⇒ Object



234
235
236
# File 'lib/efl/ecore_getopt.rb', line 234

def callback_full short, long, help, meta, cb, data, arg_req, def_val
    self << [ short, long, help, meta, :ecore_getopt_action_callback, [:callback, [cb, data, arg_req, def_val] ] ]
end

#callback_noargs(short, long, help, cb, data = nil) ⇒ Object



237
238
239
# File 'lib/efl/ecore_getopt.rb', line 237

def callback_noargs short, long, help, cb, data=nil
    callback_full short, long, help, FFI::Pointer::NULL, cb, data, :ecore_getopt_desc_arg_requirement_no, FFI::Pointer::NULL
end

#choice(short, long, help, choices) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/efl/ecore_getopt.rb', line 209

def choice short, long, help, choices
    ptr = FFI::MemoryPointer.new(:pointer, choices.length+1)
    choices.each_with_index do |s, i|
        ptr[i].put_pointer 0, p_from_string(s)
    end
    ptr[choices.length].put_pointer 0, FFI::Pointer::NULL
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_choice, [:choices,ptr] ]
end

#choice_metavar(short, long, help, meta, choices) ⇒ Object



217
218
219
220
221
222
223
224
# File 'lib/efl/ecore_getopt.rb', line 217

def choice_metavar short, long, help, meta, choices
    ptr = FFI::MemoryPointer.new(:pointer, choices.length+1)
    choices.each_with_index do |s, i|
        ptr[i].put_pointer 0, p_from_string(s)
    end
    ptr[choices.length].put_pointer 0, FFI::Pointer::NULL
    self << [ short, long, help, meta, :ecore_getopt_action_choice, [:choices,ptr] ]
end


249
250
251
# File 'lib/efl/ecore_getopt.rb', line 249

def copyright short, long
    self << [ short, long, 'show copyright.', FFI::Pointer::NULL, :ecore_getopt_action_copyright, [:dummy,FFI::Pointer::NULL] ]
end

#count(short, long, help) ⇒ Object



231
232
233
# File 'lib/efl/ecore_getopt.rb', line 231

def count short, long, help
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_count, [:dummy,FFI::Pointer::NULL] ]
end

#createObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/efl/ecore_getopt.rb', line 115

def create
    @ecore_getopt = Native::EcoreGetopt.new( FFI::MemoryPointer.new( :uchar, Native::EcoreGetopt.size+Native::EcoreGetoptDesc.size*@options.length) )
    [:prog,:usage,:version,:copyright,:license,:description].each do |sym|
        @ecore_getopt[sym] = ( @desc.has_key?(sym) ? FFI::MemoryPointer.from_string(@desc[sym]) : FFI::Pointer::NULL )
    end
    @ecore_getopt[:strict] = @desc[:strict] if @desc.has_key? :strict
    @options.each_with_index do |o,i|
        d = @ecore_getopt.desc_ptr i
        if o[0]==0
            d[:shortname] = d[:longname] = d[:help] = d[:metavar] = d[:action] = d[:action_param][:dummy] = 0
            break
        end
        d[:shortname] = o[0].to_s.bytes.first
        d[:longname] = p_from_string o[1]
        d[:help] = p_from_string o[2]
        d[:metavar] = o[3]
        d[:action] = o[4]
        k, v = o[5]
        case k
        when :dummy
            d[:action_param][:dummy] = v
        when :callback
            cb = d[:action_param][:callback]
            cb[:func] = v[0]
            cb[:data] = v[1]
            cb[:arg_req] = v[2]
            cb[:def] = v[3]
        when :store
            st = d[:action_param][:store]
            st[:type] = v[0]
            st[:arg_req] = v[1]
            if not v[2].nil?
                if v[2][0]==:strv
                    st[:def][:strv] = p_from_string v[2][1]
                else
                    st[:def][v[2][0]] = v[2][1]
                end
            end
        when :store_const
            d[:action_param][:store_const] = v
        when :choices
            d[:action_param][:choices] = v
        when :append
            d[:action_param][:append_type] = v
        else
            d[:action_param][:dummy] = FFI::Pointer::NULL
        end
    end
    @values_p = FFI::MemoryPointer.new Native::EcoreGetoptValue, @values.length, false
    @values.each_with_index do |v,i|
        Native::EcoreGetoptValue.new(@values_p+(i*Native::EcoreGetoptValue.size))[v[0]] = v[1]
    end
end

#debugObject

def sentinel

    self << [ 0, FFI::Pointer::NULL, FFI::Pointer::NULL, FFI::Pointer::NULL, 0, {:dummy=>FFI::Pointer::NULL} ]
end


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/efl/ecore_getopt.rb', line 259

def debug
    r = ''
    r << "#{self.class} : #{@ecore_getopt.to_ptr}\n"
    [:prog,:usage,:version,:copyright,:license,:description].each do |sym|
        r<< "  #{sym.to_s} : #{@ecore_getopt[sym]==FFI::Pointer::NULL ? 'NULL' : @ecore_getopt[sym].read_string}\n"
    end
    r << "  strict : #{@ecore_getopt[:strict]}\n"
    i=0
    while true
        d = @ecore_getopt.desc_ptr i
        break if d[:shortname]==0 and d[:longname] == FFI::Pointer::NULL
        r << "    desc #{d.to_ptr}\n"
        r << "     short: #{d[:shortname].chr}\n" unless d[:shortname]==0
        r << "     long:  #{d[:longname].read_string}\n" unless d[:longname]==FFI::Pointer::NULL
        r << "     help:  #{d[:help].read_string}\n" unless d[:help]==FFI::Pointer::NULL
        i+=1
    end
    r
end

#help(short, long) ⇒ Object



243
244
245
# File 'lib/efl/ecore_getopt.rb', line 243

def help short, long
    self << [ short, long, 'show this message.', FFI::Pointer::NULL, :ecore_getopt_action_help, [:dummy,FFI::Pointer::NULL] ]
end

#license(short, long) ⇒ Object



252
253
254
# File 'lib/efl/ecore_getopt.rb', line 252

def license short, long
    self << [ short, long, 'show license.', FFI::Pointer::NULL, :ecore_getopt_action_license, [:dummy,FFI::Pointer::NULL] ]
end

#parse(argv) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/efl/ecore_getopt.rb', line 168

def parse argv
    ptr = FFI::MemoryPointer.new(:pointer, argv.length+1)
    argv.each_with_index do |s, i|
        ptr[i].put_pointer 0, p_from_string(s)
    end
    ptr[argv.length].put_pointer 0, FFI::Pointer::NULL
    Native.ecore_getopt_parse @ecore_getopt, @values_p, argv.length, ptr
end

#store(short, long, help, type) ⇒ Object



179
180
181
# File 'lib/efl/ecore_getopt.rb', line 179

def store short, long, help, type
    store_full short, long, help, FFI::Pointer::NULL, type, :ecore_getopt_desc_arg_requirement_yes, nil
end

#store_const(short, long, help, value) ⇒ Object



200
201
202
# File 'lib/efl/ecore_getopt.rb', line 200

def store_const short, long, help, value
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_store_const, [:store_const, value] ]
end

#store_def(short, long, help, type, def_val) ⇒ Object



191
192
193
# File 'lib/efl/ecore_getopt.rb', line 191

def store_def short, long, help, type, def_val
    store_full short, long, help, FFI::Pointer::NULL, type, :ecore_getopt_desc_arg_requirement_optional, def_val
end

#store_def_type(type, short, long, help, def_val) ⇒ Object



194
195
196
# File 'lib/efl/ecore_getopt.rb', line 194

def store_def_type type, short, long, help, def_val
    store_def short, long, help, ('ecore_getopt_type_'+type.to_s).to_sym, [ (type.to_s+'v').to_sym, def_val ]
end

#store_false(short, long, help) ⇒ Object



206
207
208
# File 'lib/efl/ecore_getopt.rb', line 206

def store_false short, long, help
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_store_false, [:dummy,FFI::MemoryPointer::NULL] ]
end

#store_full(short, long, help, meta, type, arg_req, def_val) ⇒ Object



176
177
178
# File 'lib/efl/ecore_getopt.rb', line 176

def store_full short, long, help, meta, type, arg_req, def_val
    self << [ short, long, help, meta, :ecore_getopt_action_store, [:store, [type,arg_req, def_val] ] ]
end

#store_full_type(type, short, long, help, meta, arg_req, def_val) ⇒ Object



197
198
199
# File 'lib/efl/ecore_getopt.rb', line 197

def store_full_type type, short, long, help, meta, arg_req, def_val
    store_full short, long, help, meta, ('ecore_getopt_type_'+type.to_s).to_sym, arg_req, [ (type.to_s+'v').to_sym, def_val ]
end

#store_meta_type(type, short, long, help, meta) ⇒ Object



188
189
190
# File 'lib/efl/ecore_getopt.rb', line 188

def store_meta_type type, short, long, help, meta
    store_metavar short, long, help, meta, ('ecore_getopt_type_'+type.to_s).to_sym
end

#store_metavar(short, long, help, meta, type) ⇒ Object



185
186
187
# File 'lib/efl/ecore_getopt.rb', line 185

def store_metavar short, long, help, meta, type
    store_full short, long, help, meta, type, :ecore_getopt_desc_arg_requirement_yes, nil
end

#store_true(short, long, help) ⇒ Object



203
204
205
# File 'lib/efl/ecore_getopt.rb', line 203

def store_true short, long, help
    self << [ short, long, help, FFI::Pointer::NULL, :ecore_getopt_action_store_true, [:dummy,FFI::MemoryPointer::NULL] ]
end

#store_type(type, short, long, help) ⇒ Object



182
183
184
# File 'lib/efl/ecore_getopt.rb', line 182

def store_type type, short, long, help
    store short, long, help, ('ecore_getopt_type_'+type.to_s).to_sym
end

#to_ptrObject



112
113
114
# File 'lib/efl/ecore_getopt.rb', line 112

def to_ptr
    @ecore_getopt.to_ptr
end

#value(type, ptr) ⇒ Object



109
110
111
# File 'lib/efl/ecore_getopt.rb', line 109

def value type, ptr
    @values.insert -2, [ type, ptr ]
end

#version(short, long) ⇒ Object



246
247
248
# File 'lib/efl/ecore_getopt.rb', line 246

def version short, long
    self << [ short, long, 'show program version.', FFI::Pointer::NULL, :ecore_getopt_action_version, [:dummy,FFI::Pointer::NULL] ]
end