diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/optparse.rb | 17 | ||||
-rw-r--r-- | lib/optparse/version.rb | 41 |
2 files changed, 54 insertions, 4 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index 50d72f8a2e..5281dbbbd7 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -609,11 +609,20 @@ Default options, which never appear in option summary. puts ARGV.options exit end - DefaultList.long['version'] = Switch::NoArgument.new do - if v = ARGV.options.ver - puts v - exit + DefaultList.long['version'] = Switch::OptionalArgument.new do |pkg| + if pkg + begin + require 'optparse/version' + rescue LoadError + pkg = nil + else + show_version(*pkg.split(/,/)) + end end + unless pkg + puts v if v = ARGV.options.ver + end + exit end =begin diff --git a/lib/optparse/version.rb b/lib/optparse/version.rb new file mode 100644 index 0000000000..35152181b0 --- /dev/null +++ b/lib/optparse/version.rb @@ -0,0 +1,41 @@ +# 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 + str = "#{progname}: #{klass} version #{version}" + if klass.const_defined?(:Release) + str << " (#{klass.const_get(:Release)})" + end + puts str + end + if pkg.size == 1 and pkg[0] == "all" + self.search_const(::Object, "Version", &show) + else + pkg.each do |pkg| + /\A[A-Z]\w*((::|\/)[A-Z]\w*)*\z/ni =~ pkg or next + begin + pkg = eval(pkg) + show.call(pkg, pkg.const_defined?(:Version) ? pkg.const_get(:Version) : "unknown") + rescue NameError + puts "#{progname}: #$!" + end + end + end + exit + end + + 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) + yield klass, const if cname == name + klasses << const if Module === const and const != ::Object + end + end + end +end |