Class: RbFind::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rbfind/appl.rb

Direct Known Subclasses

DummyAppl

Defined Under Namespace

Classes: Exit, OptError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Application

Returns a new instance of Application.



123
124
125
126
127
128
129
130
131
# File 'lib/rbfind/appl.rb', line 123

def initialize args
  @args = self.class.process_options args do |opt,arg|
    if arg then
      instance_exec arg, &opt
    else
      instance_exec &opt
    end
  end
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



26
27
28
# File 'lib/rbfind/appl.rb', line 26

def debug
  @debug
end

.optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/rbfind/appl.rb', line 25

def options
  @options
end

Class Method Details

.inherited(cls) ⇒ Object



21
22
23
# File 'lib/rbfind/appl.rb', line 21

def inherited cls
  cls.init_opts
end

.init_optsObject



28
29
30
# File 'lib/rbfind/appl.rb', line 28

def init_opts
  @options, @optdocs = {}, []
end

.option(names, doc, &block) ⇒ Object



32
33
34
35
# File 'lib/rbfind/appl.rb', line 32

def option names, doc, &block
  names.each { |n| @options[ n] = block }
  @optdocs.push [ names, doc]
end

.process_options(args) ⇒ Object



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
# File 'lib/rbfind/appl.rb', line 68

def process_options args
  rest = []
  loop do
    arg, *args = *args
    arg or break
    if arg =~ /^-/ then
      arg = $'
      if arg =~ /^-/ then
        arg = $'
        if arg.empty? then
          rest.concat args
          break
        end
        arg, val = arg.split "=", 2
        opt = @options[ arg] or raise OptError, "--#{arg}"
        if val then
          opt.parameters.empty? and raise OptError, "--#{args}=value"
          yield opt, val
        else
          unless opt.parameters.empty? then
            if opt.arity.nonzero? or args.first !~ /^-/ then
              arg, *args = *args
            else
              arg = nil
            end
          end
          yield opt, arg
        end
      else
        loop do
          a = arg.slice! 0, 1
          a.empty? and break
          opt = @options[ a] or raise OptError, "-#{a}"
          if opt.parameters.empty? then
            yield opt
          else
            if arg.empty? then
              if opt.arity.nonzero? or args.first !~ /^-/ then
                arg, *args = *args
              end
            end
            yield opt, arg
            break
          end
        end
      end
    else
      rest.push arg
    end
  end
  rest
end

.runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbfind/appl.rb', line 37

def run
  exit (new $*).run.to_i
rescue Exit
  exit 0
rescue OptError
  $stderr.puts $!
  usage
  exit 15
rescue
  raise if @debug
  $stderr.puts $!
  exit 1
end

.usageObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbfind/appl.rb', line 51

def usage
  omax, amax, l = 0, 0, []
  @optdocs.each { |names,desc|
    m = names.map { |n| (n.length > 1 ? "--" : "-") + n }.join " "
    omax = m.length if m.length > omax
    _, arg = @options[ names.first].parameters.first
    if arg then
      a = arg.upcase
      amax = a.length if a.length > amax
    end
    l.push [ m, a, desc]
  }
  l.push [ "--", nil, "stop option processing"]
  fmt = "    %%-%ds  %%-%ds  %%s" % [ omax, amax]
  l.each { |mad| puts fmt % mad }
end