Class: Application

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

Direct Known Subclasses

ApplicationFan

Defined Under Namespace

Classes: Done, OptionError

Constant Summary collapse

APPL_VERSION =
"1.17".freeze
OPTIONS_ENV =
nil
STOPOPT =
"stop option processing"
UNKNOWN =
"Unknown option: `%s'."
UNPROCA =
"Warning: unprocessed arguments: %s"
ALIASES =
[].freeze
W_OPTS =
10
W_ARGS =
16

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, args = nil) ⇒ Application

Returns a new instance of Application.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/appl.rb', line 24

def initialize name = nil, args = nil
  @name, @args = name, args
  self.class.each_option { |opt,desc,arg,dfl,act|
    begin
      send act, dfl if dfl
    rescue NoMethodError
      raise OptionError, "Option action `#{act}' is not defined."
    end
  }
  while @args.first =~ /\A-/ do
    opt = $'
    @args.shift
    if opt =~ /\A-/ then
      break if !$' or $'.empty?
      opt = $'
      if opt =~ /^=/ then opt = $` ; @args.unshift $' end
      act = self.class.option_act @args, opt, nil
      send *act
    else
      until not opt or opt.empty? do
        c = opt.slice! 0, 1
        if opt =~ /^=/ then opt = nil ; @args.unshift $' end
        act = self.class.option_act @args, c, opt
        send *act
      end
    end
  end
end

Class Method Details

.alias_option(orig, opt) ⇒ Object



152
153
154
155
156
# File 'lib/appl.rb', line 152

def alias_option orig, opt
  unalias_option opt
  @aliases[ opt.to_s] = orig.to_s
  nil
end

.all_namesObject



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

def all_names
  [ self::NAME, *self::ALIASES].join "|"
end

.cmdline_argumentsObject



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/appl.rb', line 292

def cmdline_arguments
  r = []
  oe = self::OPTIONS_ENV
  eo = ENV[ oe] if oe
  if eo then
    eo.scan /"((?:\\.|[^"])*")|[^" \t]+/ do
      r.push $1 ? (eval $1) : $&
    end
  end
  r.concat $*
  $*.clear
  r
end

.define_option(opt, *param) ⇒ Object Also known as: def_option



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/appl.rb', line 130

def define_option opt, *param
  delete_option opt
  act = param.shift
  desc = param.pop
  arg = param.shift
  if arg then
    if param.empty? then
      arg, dfl = arg.split /:/, 2
      if dfl =~ /\A:/ then
        dfl = $'.to_sym
      end
    else
      dfl = param.shift
    end
  end
  d = param.map { |x| "#{x}#$/" }.join
  desc.insert 0, d
  @options[ opt.to_s] = [ desc, arg, dfl, act]
  nil
end

.delete_option(opt) ⇒ Object



158
159
160
161
162
# File 'lib/appl.rb', line 158

def delete_option opt
  @aliases.reject! { |k,v| v == opt }
  @options.delete opt
  nil
end

.each_optionObject



191
192
193
194
195
196
197
198
# File 'lib/appl.rb', line 191

def each_option
  @options.each { |opt,(desc,arg,dfl,act)|
    case dfl
      when Symbol then dfl = const_get dfl
    end
    yield opt, desc, arg, dfl, act
  }
end

.full_nameObject



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/appl.rb', line 232

def full_name
  n = []
  s = self
  loop do
    l = s.all_names
    n.unshift l
    break if s == root
    s = s.root
  end
  n.join ' '
end

.helpObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/appl.rb', line 244

def help
  fn = full_name
  puts "#{fn}  --  #{self::SUMMARY}"
  puts
  d = self::DESCRIPTION.gsub /#(\w+?)?#/ do
    case $1
      when "NAME" then fn
    end
  end
  puts d
  puts
  show_options
  if block_given? then
    puts
    yield
  end
end

.is_cmd?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/appl.rb', line 91

def is_cmd?
  const_defined? :NAME
end

.option_act(args, opt, rest) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/appl.rb', line 200

def option_act args, opt, rest
  dada = find_option_act opt
  dada or raise OptionError, root::UNKNOWN % opt
  desc, arg, dfl, act = *dada
  r = [ act]
  if arg then
    p = rest.slice! 0, rest.length if rest and not rest.empty?
    r.push p||args.shift
  end
  r
end

.rootObject



224
225
226
# File 'lib/appl.rb', line 224

def root
  self
end

.run(args = nil) ⇒ Object



95
96
97
98
# File 'lib/appl.rb', line 95

def run args = nil
  e = execute args
  exit e
end

.show_message(msg, extra = nil) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/appl.rb', line 279

def show_message msg, extra = nil
  if $stderr.tty? then
    msg = "\e[31;1m#{msg}\e[m"
    if extra then
      extra = "\e[31m#{extra}\e[m"
    end
  end
  if extra then
    msg = [ msg, extra].join " "
  end
  $stderr.puts msg
end

.show_optionsObject



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/appl.rb', line 212

def show_options
  options_desc do |opt,arg,dfl,desc|
    opt = opt.length == 1 ? "-#{opt}" : "--#{opt}"
    arg &&= "#{arg}"
    dfl &&= "[#{dfl}]"
    arg << dfl if arg && dfl
    l = "  %-*s  %-*s  %s" % [ root::W_OPTS, opt, root::W_ARGS, arg, desc]
    l.rstrip!
    puts l
  end
end

.unalias_option(opt) ⇒ Object



164
165
166
167
# File 'lib/appl.rb', line 164

def unalias_option opt
  @aliases.delete opt
  nil
end

.versionObject



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

def version
  puts "#{self::NAME} #{self::VERSION}  --  #{self::SUMMARY}"
  puts self::COPYRIGHT if const_defined? :COPYRIGHT
  puts "License: #{self::LICENSE}" if const_defined? :LICENSE
  a = []
  a.push   self::AUTHOR  if const_defined? :AUTHOR
  a.concat self::AUTHORS if const_defined? :AUTHORS
  if a.any? then
    a.flatten!
    a.uniq!
    j = a.join ", "
    h = a.length == 1 ? "Author" : "Authors"
    puts "#{h}: #{j}"
  end
  puts "Ruby version: #{RUBY_VERSION}"
end

Instance Method Details

#executeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/appl.rb', line 72

def execute
  r = run
  warn_unprocessed
  case r
    when Integer then r & 0xff
    else              0
  end
rescue SignalException
  raise if @debug
  self.class.show_message $!.inspect
  128 + $!.signo
rescue
  raise if @debug
  self.class.show_message "Error: #$!", "(#{$!.class})"
  $!.to_i rescue 1
end

#helpObject

Raises:



53
54
55
56
# File 'lib/appl.rb', line 53

def help
  self.class.help
  raise Done
end

#runObject



63
64
# File 'lib/appl.rb', line 63

def run
end

#versionObject

Raises:



58
59
60
61
# File 'lib/appl.rb', line 58

def version
  self.class.version
  raise Done
end

#warn_unprocessedObject



66
67
68
69
70
# File 'lib/appl.rb', line 66

def warn_unprocessed
  if @args.any? then
    $stderr.puts self.class.root::UNPROCA % (@args.join " ")
  end
end