2003-08-31 07:25:11 +04:00
|
|
|
# OptionParser internal utility
|
|
|
|
|
|
|
|
class << OptionParser
|
|
|
|
def show_version(*pkg)
|
|
|
|
progname = ARGV.options.program_name
|
|
|
|
show = proc do |klass, version|
|
|
|
|
version = version.join(".") if Array === version
|
2003-09-05 06:32:24 +04:00
|
|
|
str = "#{progname}"
|
|
|
|
str << ": #{klass}" unless klass == Object
|
|
|
|
str << " version #{version}"
|
2003-09-08 15:00:17 +04:00
|
|
|
case
|
|
|
|
when klass.const_defined?(:Release)
|
|
|
|
str << " (#{klass.const_get(:Release)})"
|
|
|
|
when klass.const_defined?(:RELEASE)
|
2003-08-31 07:25:11 +04:00
|
|
|
str << " (#{klass.const_get(:Release)})"
|
|
|
|
end
|
|
|
|
puts str
|
|
|
|
end
|
|
|
|
if pkg.size == 1 and pkg[0] == "all"
|
2003-09-08 15:00:17 +04:00
|
|
|
self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
|
|
|
|
unless cname[1] == ?e and klass.const_defined?(:Version)
|
|
|
|
show.call(klass, version)
|
|
|
|
end
|
|
|
|
end
|
2003-08-31 07:25:11 +04:00
|
|
|
else
|
|
|
|
pkg.each do |pkg|
|
|
|
|
/\A[A-Z]\w*((::|\/)[A-Z]\w*)*\z/ni =~ pkg or next
|
|
|
|
begin
|
|
|
|
pkg = eval(pkg)
|
2003-09-08 15:00:17 +04:00
|
|
|
v = case
|
|
|
|
when pkg.const_defined?(:Version)
|
|
|
|
pkg.const_get(:Version)
|
|
|
|
when pkg.const_defined?(:VERSION)
|
|
|
|
pkg.const_get(:VERSION)
|
|
|
|
else
|
|
|
|
"unknown"
|
|
|
|
end
|
|
|
|
show.call(pkg, v)
|
2003-08-31 07:25:11 +04:00
|
|
|
rescue NameError
|
|
|
|
puts "#{progname}: #$!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2003-09-05 06:32:24 +04:00
|
|
|
def each_const(path, klass = ::Object)
|
|
|
|
path.split(/::|\//).inject(klass) do |klass, name|
|
|
|
|
raise NameError, path unless Module === klass
|
|
|
|
klass.constants.grep(/#{name}/i) do |c|
|
|
|
|
klass.const_defined?(c) or next
|
|
|
|
c = klass.const_get(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-31 07:25:11 +04:00
|
|
|
def search_const(klass, name)
|
|
|
|
klasses = [klass]
|
|
|
|
while klass = klasses.shift
|
|
|
|
klass.constants.each do |cname|
|
|
|
|
klass.const_defined?(cname) or next
|
|
|
|
const = klass.const_get(cname)
|
2003-09-08 15:00:17 +04:00
|
|
|
yield klass, cname, const if name === cname
|
2003-08-31 07:25:11 +04:00
|
|
|
klasses << const if Module === const and const != ::Object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|