# OptionParser (Class)

In: | optparse.rb
Parent: | Object

## Methods

each_const search_const show_version

## Constants

RCSID | = | %w$Id: optparse.rb,v 1.38 2003/11/17 01:50:31 nobu Exp $[1..-1].each {|s| s.freeze}.freeze
Version | = | (RCSID[1].split('.').collect {|s| s.to_i}.extend(Comparable).freeze if RCSID[1])
LastModified | = | (Time.gm(*RCSID[2, 2].join('-').scan(/\d+/).collect {|s| s.to_i}) if RCSID[2])
Release | = | RCSID[2]
NoArgument | = | [NO_ARGUMENT = :NONE, nil].freeze
RequiredArgument | = | [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
OptionalArgument | = | [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
Version | = | OptionParser::Version

## Classes and Modules

Module OptionParser::Completion
Class OptionParser::OptionCaseMap
Class OptionParser::OptionMap
Class OptionParser::Switch
::Module OptionParser::Switch::Acceptables
::Module OptionParser::Switch::Arguable
::Class OptionParser::Switch::AmbiguousArgument
::Class OptionParser::Switch::AmbiguousOption
::Class OptionParser::Switch::CompletingHash
::Class OptionParser::Switch::InvalidArgument
::Class OptionParser::Switch::InvalidOption
::Class OptionParser::Switch::List
::Class OptionParser::Switch::MissingArgument
::Class OptionParser::Switch::NeedlessArgument
::Class OptionParser::Switch::NoArgument
::Class OptionParser::Switch::OptionalArgument
::Class OptionParser::Switch::ParseError
::Class OptionParser::Switch::PlacedArgument
::Class OptionParser::Switch::RequiredArgument

## Public Class methods

show_version(*pkg)

[Source]

    # File optparse/version.rb, line 4
      def show_version(*pkg)
        progname = ARGV.options.program_name
        show = proc do |klass, version|
          version = version.join(".") if Array === version
          str = "#{progname}"
          str << ": #{klass}" unless klass == Object
          str << " version #{version}"
          case
          when klass.const_defined?(:Release)
            str << " (#{klass.const_get(:Release)})"
          when 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, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
            unless cname[1] == ?e and klass.const_defined?(:Version)
              show.call(klass, version)
            end
          end
        else
          pkg.each do |pkg|
            /\A[A-Z]\w*((::|\/)[A-Z]\w*)*\z/ni =~ pkg or next
            begin
              pkg = eval(pkg)
              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)
            rescue NameError
              puts "#{progname}: #$!"
            end
          end
        end
        exit
      end

each_const(path, klass = ::Object)

[Source]

    # File optparse/version.rb, line 47
      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

search_const(klass, name) {|klass, cname, const if name === cname| ...}

[Source]

    # File optparse/version.rb, line 57
      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, cname, const if name === cname
            klasses << const if Module === const and const != ::Object
          end
        end
      end

[Validate]