Class: ALib::AbstractMain

Inherits:
Object show all
Includes:
ALib, Constants, Logging
Defined in:
lib/alib-0.5.1/main.rb

Direct Known Subclasses

ConfigurableMain, SimpleMain

Defined Under Namespace

Modules: Constants Classes: Param

Constant Summary

Constants included from Constants

Constants::EXIT_FAILURE, Constants::EXIT_OK, Constants::EXIT_PSEUDO_SUCCESS, Constants::EXIT_SUCCESS

Constants included from Logging

Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3

Constants included from ALib

AUTOLOAD, BSearch, Configfile, Find, LIBDIR, Listfile, OrderedAutoHash, SINGLETON_CLASS, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

append_features

Constructor Details

#initializeAbstractMain

–}}}



452
# File 'lib/alib-0.5.1/main.rb', line 452

def initialize; end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



374
375
376
# File 'lib/alib-0.5.1/main.rb', line 374

def argv
  @argv
end

#cmdlineObject (readonly)

Returns the value of attribute cmdline.



376
377
378
# File 'lib/alib-0.5.1/main.rb', line 376

def cmdline
  @cmdline
end

#consoleObject (readonly) Also known as: console?

Returns the value of attribute console.



377
378
379
# File 'lib/alib-0.5.1/main.rb', line 377

def console
  @console
end

#envObject (readonly)

Returns the value of attribute env.



375
376
377
# File 'lib/alib-0.5.1/main.rb', line 375

def env
  @env
end

#listoptionsObject (readonly)

Returns the value of attribute listoptions.



379
380
381
# File 'lib/alib-0.5.1/main.rb', line 379

def listoptions
  @listoptions
end

#logdevObject (readonly)

Returns the value of attribute logdev.



381
382
383
# File 'lib/alib-0.5.1/main.rb', line 381

def logdev
  @logdev
end

#loggerObject (readonly)

Returns the value of attribute logger.



372
373
374
# File 'lib/alib-0.5.1/main.rb', line 372

def logger
  @logger
end

#opObject (readonly)

Returns the value of attribute op.



380
381
382
# File 'lib/alib-0.5.1/main.rb', line 380

def op
  @op
end

#options(*keys) ⇒ Object (readonly)

Returns the value of attribute options.



378
379
380
# File 'lib/alib-0.5.1/main.rb', line 378

def options
  @options
end

#programObject (readonly)

Returns the value of attribute program.



373
374
375
# File 'lib/alib-0.5.1/main.rb', line 373

def program
  @program
end

#verbosityObject (readonly)

Returns the value of attribute verbosity.



382
383
384
# File 'lib/alib-0.5.1/main.rb', line 382

def verbosity
  @verbosity
end

Class Method Details

.argumentObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/alib-0.5.1/main.rb', line 263

def required_arguments(*list)
#--{{{
  @required_arguments ||= []
  list.flatten.each do |arg| 
    return(optional_argument(arg)) if Hash === arg
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @required_arguments << "#{ arg }"
  end
  @required_arguments
#--}}}
end

.argumentsObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/alib-0.5.1/main.rb', line 262

def required_arguments(*list)
#--{{{
  @required_arguments ||= []
  list.flatten.each do |arg| 
    return(optional_argument(arg)) if Hash === arg
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @required_arguments << "#{ arg }"
  end
  @required_arguments
#--}}}
end

.class_attributes(*names) ⇒ Object Also known as: class_attribute, c_attrs, c_attr



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
# File 'lib/alib-0.5.1/main.rb', line 77

def class_attributes(*names)
#--{{{
  names = stringlist names
  names.each do |name|
    getter = "#{ name }"
    setter = "#{ name }="
    code = <<-code
      class << self
        def #{ name }(*a)
          unless a.empty?
            self.#{ name }= a.shift
          else
            @#{ name }
          end
        end
        def #{ name }= value
          @#{ name } = value
        end
        alias #{ name }? #{ name }
      end
    code
    module_eval code
  end
#--}}}
end

.class_initializeObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/alib-0.5.1/main.rb', line 309

def class_initialize
#--{{{
  version '0.0.0'

  author '[email protected]'

  program File.basename($0)

  name "#{ program } v#{ version }"

  optspec [
    [ '--help', '-h', 'this message' ],
    [ '--log=path','-l', 'set log file - (default stderr)' ],
    [ '--verbosity=verbostiy', '-v', '0|fatal < 1|error < 2|warn < 3|info < 4|debug - (default info)' ],
  ]

=begin
  usage <<-usage
    NAME
      #{ program } v#{ version }

    SYNOPSIS
      #{ program } [options]* [file]*
  usage
=end

  examples ''
#--}}}
end

.defaultObject

–}}}



364
365
366
367
368
369
370
371
372
373
374
# File 'lib/alib-0.5.1/main.rb', line 364

def defaults head = nil, *tail  
#--{{{
  @defaults ||= {}
  return @defaults if head.nil?
  return @defaults.update(head) if head.is_a?(Hash)
  return @defaults.values_at(*head) if head.is_a?(Array)
  return @defaults[head.to_s] if tail.empty?
  keys = [head, tail].flatten.map{|k| k.to_s}
  return @defaults.values_at(*keys)
#--}}}
end

.defaults(head = nil, *tail) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
# File 'lib/alib-0.5.1/main.rb', line 353

def defaults head = nil, *tail  
#--{{{
  @defaults ||= {}
  return @defaults if head.nil?
  return @defaults.update(head) if head.is_a?(Hash)
  return @defaults.values_at(*head) if head.is_a?(Array)
  return @defaults[head.to_s] if tail.empty?
  keys = [head, tail].flatten.map{|k| k.to_s}
  return @defaults.values_at(*keys)
#--}}}
end

.exit_failureObject



36
# File 'lib/alib-0.5.1/main.rb', line 36

def self.exit_failure() EXIT_FAILURE end

.exit_okObject



31
# File 'lib/alib-0.5.1/main.rb', line 31

def self.exit_ok() EXIT_OK end

.exit_pseudo_successObject



26
# File 'lib/alib-0.5.1/main.rb', line 26

def self.exit_pseudo_success() EXIT_PSEUDO_SUCCESS end

.exit_successObject



21
# File 'lib/alib-0.5.1/main.rb', line 21

def self.exit_success() EXIT_SUCCESS end

.get_param(name) ⇒ Object



886
887
888
# File 'lib/alib-0.5.1/main.rb', line 886

def get_param name
  params[ name.to_s ]
end

.indent_block(buf, n = 2) ⇒ Object



213
214
215
216
217
218
# File 'lib/alib-0.5.1/main.rb', line 213

def indent_block buf, n = 2
#--{{{
  space = ' ' * n.to_i
  unindent_block(buf).gsub %r/^/, space 
#--}}}
end

.inherited(klass, &b) ⇒ Object



339
340
341
342
343
344
345
# File 'lib/alib-0.5.1/main.rb', line 339

def inherited klass, &b 
#--{{{
  ret = super(klass, &b)
  klass.class_initialize
  ret
#--}}}
end

.instance_attributes(*names) ⇒ Object Also known as: instance_attribute, i_attrs, i_attr, attribute, attributes

–}}}



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/alib-0.5.1/main.rb', line 48

def instance_attributes(*names)
#--{{{
  names = stringlist names
  names.each do |name|
    getter = "#{ name }"
    setter = "#{ name }="
    code = <<-code
      def #{ name }(*a)
        unless a.empty?
          self.#{ name }= a.shift
        else
          @#{ name }
        end
      end
      def #{ name }= value
        @#{ name } = value
      end
      alias #{ name }? #{ name }
    code
    module_eval code
  end
#--}}}
end

.main(&block) ⇒ Object



927
928
929
# File 'lib/alib-0.5.1/main.rb', line 927

def self.main &block
  define_method :main, &block
end

.new(*a, &b) ⇒ Object



400
401
402
403
404
405
# File 'lib/alib-0.5.1/main.rb', line 400

def self.new *a, &b
#--{{{
  (obj = allocate).instance_eval{ __initialize__ *a, &b }
  obj
#--}}}
end

.optionObject

–}}}



228
229
230
231
232
233
234
235
# File 'lib/alib-0.5.1/main.rb', line 228

def options(*list)
#--{{{
  @optspec ||= []
  return @optspec if list.empty?
  list = [list] unless Array === list.first
  list.each{|spec| (@optspec ||= []) << spec}
#--}}}
end

.optional_argumentObject

–}}}



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/alib-0.5.1/main.rb', line 307

def optional_arguments(*list)
#--{{{
  @optional_arguments ||= []
  list.flatten.each do |arg| 
    arg, default =
      case arg
        when Hash
          arg.to_a.first
        else
          [arg, nil]
      end
    @do_not_gc ||= [] 
    @do_not_gc << default unless @do_not_gc.include? default
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          unless @#{ arg }
            @#{ arg } = ObjectSpace::_id2ref #{ default.object_id }
          end
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @optional_arguments << "#{ arg }"
  end
  @optional_arguments
#--}}}
end

.optional_arguments(*list) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/alib-0.5.1/main.rb', line 265

def optional_arguments(*list)
#--{{{
  @optional_arguments ||= []
  list.flatten.each do |arg| 
    arg, default =
      case arg
        when Hash
          arg.to_a.first
        else
          [arg, nil]
      end
    @do_not_gc ||= [] 
    @do_not_gc << default unless @do_not_gc.include? default
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          unless @#{ arg }
            @#{ arg } = ObjectSpace::_id2ref #{ default.object_id }
          end
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @optional_arguments << "#{ arg }"
  end
  @optional_arguments
#--}}}
end

.options(*list) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/alib-0.5.1/main.rb', line 220

def options(*list)
#--{{{
  @optspec ||= []
  return @optspec if list.empty?
  list = [list] unless Array === list.first
  list.each{|spec| (@optspec ||= []) << spec}
#--}}}
end

.param(name, this = self, &block) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
# File 'lib/alib-0.5.1/main.rb', line 862

def param name, this = self, &block 
#--{{{
  parm =
    if block or name.is_a?(Array)
      param = Param.new name, this, &block
      option *param.to_option
      default param => param.default
      params[ param.to_s ] = param
    else
      name = name.to_s
      defined = params[ name ]
      if defined
        defined
      else
        param = Param.new name, this, &block
        option *param.to_option
        default param => param.default
        params[ param.to_s ] = param
      end
    end
  parm.value
#--}}}
end

.paramsObject

–{{{



856
857
858
859
860
# File 'lib/alib-0.5.1/main.rb', line 856

def params
#--{{{
  @params ||= {} 
#--}}}
end

.parmObject

–}}}



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/alib-0.5.1/main.rb', line 885

def param name, this = self, &block 
#--{{{
  parm =
    if block or name.is_a?(Array)
      param = Param.new name, this, &block
      option *param.to_option
      default param => param.default
      params[ param.to_s ] = param
    else
      name = name.to_s
      defined = params[ name ]
      if defined
        defined
      else
        param = Param.new name, this, &block
        option *param.to_option
        default param => param.default
        params[ param.to_s ] = param
      end
    end
  parm.value
#--}}}
end

.parmsObject

––}}



861
862
863
864
865
# File 'lib/alib-0.5.1/main.rb', line 861

def params
#--{{{
  @params ||= {} 
#--}}}
end

.required_argumentObject

–}}}



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/alib-0.5.1/main.rb', line 261

def required_arguments(*list)
#--{{{
  @required_arguments ||= []
  list.flatten.each do |arg| 
    return(optional_argument(arg)) if Hash === arg
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @required_arguments << "#{ arg }"
  end
  @required_arguments
#--}}}
end

.required_arguments(*list) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/alib-0.5.1/main.rb', line 230

def required_arguments(*list)
#--{{{
  @required_arguments ||= []
  list.flatten.each do |arg| 
    return(optional_argument(arg)) if Hash === arg
    unless instance_methods.include? "#{ arg }"
      module_eval <<-code
        def #{ arg }(*__list)
          if __list.empty?
            @#{ arg }
          else
            send('#{ arg }=', *__list)
          end
        end
        def #{ arg }=(__arg, *__list)
          if __list.empty?
            @#{ arg } = __arg
          else
            @#{ arg } = ([__arg] + __list)
          end
        end
        def #{ arg }?
          defined? @#{ arg } and @#{ arg }
        end
      code
    end
    @required_arguments << "#{ arg }"
  end
  @required_arguments
#--}}}
end

.run(*a, &b) ⇒ Object



347
348
349
350
351
# File 'lib/alib-0.5.1/main.rb', line 347

def run(*a, &b)
#--{{{
  new(*a, &b).run
#--}}}
end

.status_failureObject



38
# File 'lib/alib-0.5.1/main.rb', line 38

def self.status_failure() EXIT_FAILURE end

.status_okObject



33
# File 'lib/alib-0.5.1/main.rb', line 33

def self.status_ok() EXIT_OK end

.status_pseudo_successObject



28
# File 'lib/alib-0.5.1/main.rb', line 28

def self.status_pseudo_success() EXIT_PSEUDO_SUCCESS end

.status_successObject



23
# File 'lib/alib-0.5.1/main.rb', line 23

def self.status_success() EXIT_SUCCESS end

.stringlist(*list) ⇒ Object

–{{{



43
44
45
46
47
# File 'lib/alib-0.5.1/main.rb', line 43

def stringlist(*list)
#--{{{
  list.flatten.compact.map{|item| "#{ item }".strip}
#--}}}
end

.unindent_block(buf) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/alib-0.5.1/main.rb', line 197

def unindent_block buf
#--{{{
  buf = "#{ buf }"
  lines = buf.to_a
  indent_pat = %r/^\s*[\s|]/
  indent = lines.first[ indent_pat ] rescue nil
  if indent
    lines.map do |line| 
      line[ indent.size..-1 ] || "\n"
    end.join 
  else
    buf 
  end
#--}}}
end

.usage(arg = nil) ⇒ Object

%w( usage examples ).each do |meth| #–{{{

module_eval <<-code
  def #{ meth }(*a)
    unless a.empty?
      self.#{ meth }= a.shift
    else
      @#{ meth }
    end
  end
  def #{ meth }= msg 
    @#{ meth } = unindent_block msg.to_s
  end
  def #{ meth }?
    defined? @#{ meth } and @#{ meth }
  end
code

#–}}}

end


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/alib-0.5.1/main.rb', line 152

def usage arg = nil
#--{{{
  if arg
    @usage = arg.to_s
  else
    if defined? @usage
      @usage
    else
      s = ""

      unless name.to_s.empty?
      #s << "\n"
      s << "NAME\n"
      s << indent_block("#{ name }\n", 2)
      end

      unless synopsis.to_s.empty?
      s << "\n"
      s << "SYNOPSIS\n"
      s << indent_block("#{ synopsis }\n", 2)
      end

      unless examples.to_s.empty?
      s << "\n"
      s << "EXAMPLES\n"
      s << indent_block("#{ examples }\n", 2)
      end

      s
    end
  end
#--}}}
end

Instance Method Details

#__initialize__(argv = ARGV, env = ENV) ⇒ Object



390
391
392
393
394
395
396
397
398
# File 'lib/alib-0.5.1/main.rb', line 390

def __initialize__ argv = ARGV, env = ENV
#--{{{
  @argv = Util::mcp(argv.to_a)
  @env = Util::mcp(env.to_hash)
  @program = File::expand_path $0
  @cmdline = ([@program] + @argv).join ' '
  @console = STDIN.tty?
#--}}}
end

#argument(arg) ⇒ Object

–}}}



679
680
681
682
683
# File 'lib/alib-0.5.1/main.rb', line 679

def argument arg
#--{{{
  arguments(arg).first
#--}}}
end

#arguments(*args) ⇒ Object

–}}}



674
675
676
677
678
# File 'lib/alib-0.5.1/main.rb', line 674

def arguments *args
#--{{{
  args.map{|arg| send arg}
#--}}}
end

#argv_plus_stdinObject



691
692
693
694
695
696
697
698
# File 'lib/alib-0.5.1/main.rb', line 691

def argv_plus_stdin
#--{{{
  stdin = argv.delete '-'
  STDIN.each{|line| argv << line} if stdin
  argv.map!{|arg| arg.strip}
  argv
#--}}}
end

#defaults(*a, &b) ⇒ Object Also known as: default

–}}}



684
685
686
687
688
# File 'lib/alib-0.5.1/main.rb', line 684

def defaults *a, &b
#--{{{
  self.class.defaults *a, &b
#--}}}
end

#die(opts = {}) ⇒ Object

–}}}



640
641
642
643
644
645
646
647
# File 'lib/alib-0.5.1/main.rb', line 640

def die opts = {} 
#--{{{
  msg = Util::getopt 'msg', opts, klass.usage
  errno = Util::getopt 'errno', opts, EXIT_FAILURE
  STDERR.puts("#{ msg }")
  exit(Integer(errno))
#--}}}
end

#exit_failureObject



37
# File 'lib/alib-0.5.1/main.rb', line 37

def exit_failure() EXIT_FAILURE end

#exit_okObject



32
# File 'lib/alib-0.5.1/main.rb', line 32

def exit_ok() EXIT_OK end

#exit_pseudo_successObject



27
# File 'lib/alib-0.5.1/main.rb', line 27

def exit_pseudo_success() EXIT_PSEUDO_SUCCESS end

#exit_status(e = nil) ⇒ Object Also known as: exit_status?



385
386
387
# File 'lib/alib-0.5.1/main.rb', line 385

def exit_status e = nil
  (e ? (@exit_success = e) : (defined?(@exit_success) and @exit_success))
end

#exit_successObject



22
# File 'lib/alib-0.5.1/main.rb', line 22

def exit_success() EXIT_SUCCESS end

#init_logging(opts = @options) ⇒ Object

–}}}



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/alib-0.5.1/main.rb', line 573

def init_logging opts = @options 
#--{{{
  log = Util::getopt 'log', opts
  log_age = Util::getopt 'log_age', opts
  log_size = Util::getopt 'log_size', opts
  verbosity = Util::getopt 'verbosity', opts
  log_age = Integer log_age rescue nil
  log_size = Integer log_size rescue nil
  $logger = self.logger = Logger::new(log || STDERR, log_age, log_size)
#
# hack to fix Logger sync bug
#
  begin
    class << @logger; attr :logdev unless @logger.respond_to?(:logdev); end
    @logdev = @logger.logdev.dev 
    @logdev.sync = true
  rescue
    nil
  end
  level = nil
  verbosity ||= 'info'
  verbosity =
    case verbosity
      when /^\s*(?:4|d|debug)\s*$/io
        level = 'Logging::DEBUG'
        4
      when /^\s*(?:3|i|info)\s*$/io
        level = 'Logging::INFO'
        3
      when /^\s*(?:2|w|warn)\s*$/io
        level = 'Logging::WARN'
        2
      when /^\s*(?:1|e|error)\s*$/io
        level = 'Logging::ERROR'
        1
      when /^\s*(?:0|f|fatal)\s*$/io
        level = 'Logging::FATAL'
        0
      else
        abort "illegal verbosity setting <#{ verbosity }>" 
    end
  logger.level = 2 - ((verbosity % 5) - 2) 
  logger
#--}}}
end

#invalid_option(e) ⇒ Object

–}}}



566
567
568
569
570
571
572
# File 'lib/alib-0.5.1/main.rb', line 566

def invalid_option e
#--{{{
  # e.recover argv
  fatal{ e.to_s }
  exit EXIT_FAILURE
#--}}}
end

#klassObject



407
# File 'lib/alib-0.5.1/main.rb', line 407

def klass; self.class; end

#logcatchObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/alib-0.5.1/main.rb', line 474

def logcatch 
#--{{{
  ret = nil
  # @logger ||= Logger::new STDERR
  logger!
  begin
    ret = yield
  rescue Exception => e
    unless SystemExit === e
      fatal{ e }
      exit EXIT_FAILURE
    if false
      if logger.debug?
        fatal{ e }
        exit EXIT_FAILURE
      else
        fatal{ emsg(e) }
        exit EXIT_FAILURE
      end
    end
    else
      exit e.status 
    end
  end
  ret
#--}}}
end

#mainObject

–}}}



648
649
650
651
652
# File 'lib/alib-0.5.1/main.rb', line 648

def main 
#--{{{
  42
#--}}}
end

#option(key) ⇒ Object

–}}}



669
670
671
672
673
# File 'lib/alib-0.5.1/main.rb', line 669

def option key
#--{{{
  options(key).first
#--}}}
end

#optional_argumentsObject

–}}}



414
415
416
417
418
# File 'lib/alib-0.5.1/main.rb', line 414

def optional_arguments
#--{{{
  klass::optional_arguments
#--}}}
end

#options?(*keys) ⇒ Boolean Also known as: option?

–}}}

Returns:

  • (Boolean)


653
654
655
656
657
658
# File 'lib/alib-0.5.1/main.rb', line 653

def options? *keys
#--{{{
  list = keys.map{|key| @options.has_key? key}
  list.size > 0 and list.all?
#--}}}
end

#param(name, *a) ⇒ Object Also known as: parm

–}}}



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/alib-0.5.1/main.rb', line 903

def param name, *a 
#--{{{
  key = name.to_s
  pm = params[key] or raise "no such param <#{ key }>!"

  value =
    if options.has_key? key 
      options[key] || default(key)
    else
      a.empty? ? default(key) : a.shift
    end

  #value = value.call if value.respond_to?('call')
  value = instance_eval &value if Proc === value

  #value = pm.cast[value] if(option?(key) and pm.cast?)
  value = pm.cast[value] if(value and pm.cast?)

  value
#--}}}
end

#param?(name) ⇒ Boolean

Returns:

  • (Boolean)


897
898
899
900
901
902
# File 'lib/alib-0.5.1/main.rb', line 897

def param? name
#--{{{
  key = name.to_s
  options.has_key? key
#--}}}
end

#paramsObject Also known as: parms

–}}}



891
892
893
894
895
# File 'lib/alib-0.5.1/main.rb', line 891

def params
#--{{{
  self.class.params
#--}}}
end

#parse_argvObject

–}}}



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/alib-0.5.1/main.rb', line 618

def parse_argv
#--{{{
  a, b = [], []
  klass::required_arguments.each do |arg|
    value = @argv.shift
    if value 
      send "#{ arg }=", value
    else
      die 'msg' => "required_argument <#{ arg }> not given"
    end
    a << send("#{ arg }")
  end
  klass::optional_arguments.each do |arg|
    value = @argv.shift
    if value 
      send "#{ arg }=", value
    end
    b << send("#{ arg }")
  end
  [a, b, @argv]
#--}}}
end

#parse_optionsObject

–}}}



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/alib-0.5.1/main.rb', line 544

def parse_options
#--{{{
  @op = OptionParser::new
  @options = {}
  @listoptions = Hash::new{|h,k| h[k] = []} 
  klass::optspec.each do |spec|
    k = spec.first.gsub(%r/(?:--)|(?:=.*$)|(?:\s+)/o,'')
    @op.def_option(*spec) do |v| 
      @options[k] = v
      @listoptions[k] << v
    end
  end
  begin
    op.parse! @argv 
  rescue OptionParser::InvalidOption => e
    # preverve unknown options
    #e.recover(argv)
    invalid_option e
  end
  @options
#--}}}
end

#post_init_loggingObject



456
# File 'lib/alib-0.5.1/main.rb', line 456

def post_init_logging; end

#post_mainObject



473
# File 'lib/alib-0.5.1/main.rb', line 473

def post_main; end

#post_parse_argvObject

–}}}



469
# File 'lib/alib-0.5.1/main.rb', line 469

def post_parse_argv; end

#post_parse_optionsObject



454
# File 'lib/alib-0.5.1/main.rb', line 454

def post_parse_options; end

#post_runObject



471
# File 'lib/alib-0.5.1/main.rb', line 471

def post_run; end

#pre_init_loggingObject



455
# File 'lib/alib-0.5.1/main.rb', line 455

def pre_init_logging; end

#pre_mainObject



472
# File 'lib/alib-0.5.1/main.rb', line 472

def pre_main; end

#pre_parse_argvObject



457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/alib-0.5.1/main.rb', line 457

def pre_parse_argv
#--{{{
  if(@options.has_key?('help') or (@argv.size == 1 and @argv.first =~ %r/help/i))
    usage STDOUT
    exit EXIT_SUCCESS
  end
  if(@options.has_key?('version') or @argv.first =~ %r/version/i)
    STDOUT.puts self.class.version
    exit EXIT_SUCCESS
  end
#--}}}
end

#pre_parse_optionsObject



453
# File 'lib/alib-0.5.1/main.rb', line 453

def pre_parse_options; end

#pre_runObject



470
# File 'lib/alib-0.5.1/main.rb', line 470

def pre_run; end

#required_argumentsObject



409
410
411
412
413
# File 'lib/alib-0.5.1/main.rb', line 409

def required_arguments
#--{{{
  klass::required_arguments
#--}}}
end

#runObject

–}}}



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/alib-0.5.1/main.rb', line 419

def run
#--{{{
  logcatch do
    begin
      pre_run

      pre_parse_options
      parse_options
      post_parse_options

      pre_parse_argv
      parse_argv
      post_parse_argv

      pre_init_logging
      init_logging
      post_init_logging

      initialize

      pre_main
      status = main
      post_main

      post_run

      exit(exit_status ? exit_status : (status ? EXIT_SUCCESS : EXIT_FAILURE))
    rescue Errno::EPIPE
      STDOUT.tty? ?  raise : exit(EXIT_FAILURE)
    end
  end
#--}}}
end

#status_failureObject



39
# File 'lib/alib-0.5.1/main.rb', line 39

def status_failure() EXIT_FAILURE end

#status_okObject



34
# File 'lib/alib-0.5.1/main.rb', line 34

def status_ok() EXIT_OK end

#status_pseudo_successObject



29
# File 'lib/alib-0.5.1/main.rb', line 29

def status_pseudo_success() EXIT_PSEUDO_SUCCESS end

#status_successObject



24
# File 'lib/alib-0.5.1/main.rb', line 24

def status_success() EXIT_SUCCESS end

#usage(port = STDERR) ⇒ Object

–}}}



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/alib-0.5.1/main.rb', line 501

def usage port = STDERR
#--{{{
  port << klass::usage << "\n" if(klass::usage and not klass::usage.empty?)
  port << klass::examples << "\n" if(klass::examples and not klass::examples.empty?)

  if klass::optspec 
    port << 'OPTIONS' << "\n"

    klass::optspec.each do |os| 
      a, b, c = os
      long, short, desc = nil
      [a,b,c].each do |word|
        next unless word
        word.strip!
        case word
          when %r/^--[^-]/o
            long = word
          when %r/^-[^-]/o
            short = word
          else
            desc = word
        end
      end

      spec = ((long and short) ? [long, short] : [long])

      if spec
        port << Util::columnize(spec.join(', '), :width => 80, :indent => 2)
        port << "\n"
      end

      if desc
        port << Util::columnize(desc, :width => 80, :indent => 8)
        port << "\n"
      end
    end

    port << "\n"
  end

  port
#--}}}
end