diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2021-11-16 20:19:13 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2021-11-16 20:19:13 +0900 |
commit | f3bda8987ecf78aa260e697232876b35f83b67c3 (patch) | |
tree | 4031690093d2cb9ca6f0b25e723771bacf76e872 /lib/rubygems/optparse/lib/optparse | |
parent | 84fdaaab4605020103c77df7665556de0a02dad2 (diff) |
Merge the master branch of rubygems repo
Picked from https://github.com/rubygems/rubygems/commit/4b498709a015a94e14a3852a1841a7a3e669133d
Diffstat (limited to 'lib/rubygems/optparse/lib/optparse')
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/ac.rb | 54 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/date.rb | 18 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/kwargs.rb | 22 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/shellwords.rb | 7 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/time.rb | 11 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/uri.rb | 7 | ||||
-rw-r--r-- | lib/rubygems/optparse/lib/optparse/version.rb | 71 |
7 files changed, 190 insertions, 0 deletions
diff --git a/lib/rubygems/optparse/lib/optparse/ac.rb b/lib/rubygems/optparse/lib/optparse/ac.rb new file mode 100644 index 0000000000..ff2f4c2dc3 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/ac.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: false +require 'rubygems/optparse/lib/optparse' + +class Gem::OptionParser::AC < Gem::OptionParser + private + + def _check_ac_args(name, block) + unless /\A\w[-\w]*\z/ =~ name + raise ArgumentError, name + end + unless block + raise ArgumentError, "no block given", ParseError.filter_backtrace(caller) + end + end + + ARG_CONV = proc {|val| val.nil? ? true : val} + + def _ac_arg_enable(prefix, name, help_string, block) + _check_ac_args(name, block) + + sdesc = [] + ldesc = ["--#{prefix}-#{name}"] + desc = [help_string] + q = name.downcase + ac_block = proc {|val| block.call(ARG_CONV.call(val))} + enable = Switch::PlacedArgument.new(nil, ARG_CONV, sdesc, ldesc, nil, desc, ac_block) + disable = Switch::NoArgument.new(nil, proc {false}, sdesc, ldesc, nil, desc, ac_block) + top.append(enable, [], ["enable-" + q], disable, ['disable-' + q]) + enable + end + + public + + def ac_arg_enable(name, help_string, &block) + _ac_arg_enable("enable", name, help_string, block) + end + + def ac_arg_disable(name, help_string, &block) + _ac_arg_enable("disable", name, help_string, block) + end + + def ac_arg_with(name, help_string, &block) + _check_ac_args(name, block) + + sdesc = [] + ldesc = ["--with-#{name}"] + desc = [help_string] + q = name.downcase + with = Switch::PlacedArgument.new(*search(:atype, String), sdesc, ldesc, nil, desc, block) + without = Switch::NoArgument.new(nil, proc {}, sdesc, ldesc, nil, desc, block) + top.append(with, [], ["with-" + q], without, ['without-' + q]) + with + end +end diff --git a/lib/rubygems/optparse/lib/optparse/date.rb b/lib/rubygems/optparse/lib/optparse/date.rb new file mode 100644 index 0000000000..11131e92c2 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/date.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: false +require 'rubygems/optparse/lib/optparse' +require 'date' + +Gem::OptionParser.accept(DateTime) do |s,| + begin + DateTime.parse(s) if s + rescue ArgumentError + raise Gem::OptionParser::InvalidArgument, s + end +end +Gem::OptionParser.accept(Date) do |s,| + begin + Date.parse(s) if s + rescue ArgumentError + raise Gem::OptionParser::InvalidArgument, s + end +end diff --git a/lib/rubygems/optparse/lib/optparse/kwargs.rb b/lib/rubygems/optparse/lib/optparse/kwargs.rb new file mode 100644 index 0000000000..9290344c39 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/kwargs.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +require 'rubygems/optparse/lib/optparse' + +class Gem::OptionParser + # :call-seq: + # define_by_keywords(options, method, **params) + # + # :include: ../../doc/optparse/creates_option.rdoc + # + def define_by_keywords(options, meth, **opts) + meth.parameters.each do |type, name| + case type + when :key, :keyreq + op, cl = *(type == :key ? %w"[ ]" : ["", ""]) + define("--#{name}=#{op}#{name.upcase}#{cl}", *opts[name]) do |o| + options[name] = o + end + end + end + options + end +end diff --git a/lib/rubygems/optparse/lib/optparse/shellwords.rb b/lib/rubygems/optparse/lib/optparse/shellwords.rb new file mode 100644 index 0000000000..60dd91990c --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/shellwords.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: false +# -*- ruby -*- + +require 'shellwords' +require 'rubygems/optparse/lib/optparse' + +Gem::OptionParser.accept(Shellwords) {|s,| Shellwords.shellwords(s)} diff --git a/lib/rubygems/optparse/lib/optparse/time.rb b/lib/rubygems/optparse/lib/optparse/time.rb new file mode 100644 index 0000000000..cb19f6e998 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/time.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: false +require 'rubygems/optparse/lib/optparse' +require 'time' + +Gem::OptionParser.accept(Time) do |s,| + begin + (Time.httpdate(s) rescue Time.parse(s)) if s + rescue + raise Gem::OptionParser::InvalidArgument, s + end +end diff --git a/lib/rubygems/optparse/lib/optparse/uri.rb b/lib/rubygems/optparse/lib/optparse/uri.rb new file mode 100644 index 0000000000..088f309992 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/uri.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: false +# -*- ruby -*- + +require 'rubygems/optparse/lib/optparse' +require 'uri' + +Gem::OptionParser.accept(URI) {|s,| URI.parse(s) if s} diff --git a/lib/rubygems/optparse/lib/optparse/version.rb b/lib/rubygems/optparse/lib/optparse/version.rb new file mode 100644 index 0000000000..5d79e9db44 --- /dev/null +++ b/lib/rubygems/optparse/lib/optparse/version.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: false +# Gem::OptionParser internal utility + +class << Gem::OptionParser + def show_version(*pkgs) + progname = ARGV.options.program_name + result = false + show = proc do |klass, cname, version| + str = "#{progname}" + unless klass == ::Object and cname == :VERSION + version = version.join(".") if Array === version + str << ": #{klass}" unless klass == Object + str << " version #{version}" + end + [:Release, :RELEASE].find do |rel| + if klass.const_defined?(rel) + str << " (#{klass.const_get(rel)})" + end + end + puts str + result = true + end + if pkgs.size == 1 and pkgs[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, cname.intern, version) + end + end + else + pkgs.each do |pkg| + begin + pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)} + v = case + when pkg.const_defined?(:Version) + pkg.const_get(n = :Version) + when pkg.const_defined?(:VERSION) + pkg.const_get(n = :VERSION) + else + n = nil + "unknown" + end + show.call(pkg, n, v) + rescue NameError + end + end + end + result + end + + def each_const(path, base = ::Object) + path.split(/::|\//).inject(base) do |klass, name| + raise NameError, path unless Module === klass + klass.constants.grep(/#{name}/i) do |c| + klass.const_defined?(c) or next + klass.const_get(c) + end + end + 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, cname, const if name === cname + klasses << const if Module === const and const != ::Object + end + end + end +end |