diff options
Diffstat (limited to 'lib/optparse')
| -rw-r--r-- | lib/optparse/ac.rb | 70 | ||||
| -rw-r--r-- | lib/optparse/date.rb | 3 | ||||
| -rw-r--r-- | lib/optparse/kwargs.rb | 27 | ||||
| -rw-r--r-- | lib/optparse/optparse.gemspec | 34 | ||||
| -rw-r--r-- | lib/optparse/shellwords.rb | 3 | ||||
| -rw-r--r-- | lib/optparse/time.rb | 3 | ||||
| -rw-r--r-- | lib/optparse/uri.rb | 3 | ||||
| -rw-r--r-- | lib/optparse/version.rb | 58 |
8 files changed, 174 insertions, 27 deletions
diff --git a/lib/optparse/ac.rb b/lib/optparse/ac.rb new file mode 100644 index 0000000000..23fc740d10 --- /dev/null +++ b/lib/optparse/ac.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: false +require_relative '../optparse' + +# +# autoconf-like options. +# +class OptionParser::AC < OptionParser + # :stopdoc: + 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} + private_constant :ARG_CONV + + 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 + + # :startdoc: + + public + + # Define <tt>--enable</tt> / <tt>--disable</tt> style option + # + # Appears as <tt>--enable-<i>name</i></tt> in help message. + def ac_arg_enable(name, help_string, &block) + _ac_arg_enable("enable", name, help_string, block) + end + + # Define <tt>--enable</tt> / <tt>--disable</tt> style option + # + # Appears as <tt>--disable-<i>name</i></tt> in help message. + def ac_arg_disable(name, help_string, &block) + _ac_arg_enable("disable", name, help_string, block) + end + + # Define <tt>--with</tt> / <tt>--without</tt> style option + # + # Appears as <tt>--with-<i>name</i></tt> in help message. + 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/optparse/date.rb b/lib/optparse/date.rb index d680559f37..7bbf12b77f 100644 --- a/lib/optparse/date.rb +++ b/lib/optparse/date.rb @@ -1,4 +1,5 @@ -require 'optparse' +# frozen_string_literal: false +require_relative '../optparse' require 'date' OptionParser.accept(DateTime) do |s,| diff --git a/lib/optparse/kwargs.rb b/lib/optparse/kwargs.rb new file mode 100644 index 0000000000..59a2f61544 --- /dev/null +++ b/lib/optparse/kwargs.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true +require_relative '../optparse' + +class OptionParser + # :call-seq: + # define_by_keywords(options, method, **params) + # + # :include: ../../doc/optparse/creates_option.rdoc + # + # Defines options which set in to _options_ for keyword parameters + # of _method_. + # + # Parameters for each keywords are given as elements of _params_. + # + def define_by_keywords(options, method, **params) + method.parameters.each do |type, name| + case type + when :key, :keyreq + op, cl = *(type == :key ? %w"[ ]" : ["", ""]) + define("--#{name}=#{op}#{name.upcase}#{cl}", *params[name]) do |o| + options[name] = o + end + end + end + options + end +end diff --git a/lib/optparse/optparse.gemspec b/lib/optparse/optparse.gemspec new file mode 100644 index 0000000000..885b0ec380 --- /dev/null +++ b/lib/optparse/optparse.gemspec @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +name = File.basename(__FILE__, ".gemspec") +version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir| + break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line| + /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1 + end rescue nil +end + +Gem::Specification.new do |spec| + spec.name = name + spec.version = version + spec.authors = ["Nobu Nakada"] + spec.email = ["nobu@ruby-lang.org"] + + spec.summary = %q{OptionParser is a class for command-line option analysis.} + spec.description = File.open(File.join(__dir__, "README.md")) do |readme| + readme.gets("") # heading + readme.gets("").chomp + end rescue spec.summary + spec.homepage = "https://github.com/ruby/optparse" + spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0") + spec.licenses = ["Ruby", "BSD-2-Clause"] + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + + dir, gemspec = File.split(__FILE__) + excludes = %W[#{gemspec} rakelib test/ Gemfile Rakefile .git* .editor*].map {|n| ":^"+n} + spec.files = IO.popen(%w[git ls-files -z --] + excludes, chdir: dir, &:read).split("\x0") + spec.bindir = "exe" + spec.executables = [] + spec.require_paths = ["lib"] +end diff --git a/lib/optparse/shellwords.rb b/lib/optparse/shellwords.rb index 0422d7c887..4feb1993d9 100644 --- a/lib/optparse/shellwords.rb +++ b/lib/optparse/shellwords.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: false # -*- ruby -*- require 'shellwords' -require 'optparse' +require_relative '../optparse' OptionParser.accept(Shellwords) {|s,| Shellwords.shellwords(s)} diff --git a/lib/optparse/time.rb b/lib/optparse/time.rb index 402cadcf16..0ce651f6f6 100644 --- a/lib/optparse/time.rb +++ b/lib/optparse/time.rb @@ -1,4 +1,5 @@ -require 'optparse' +# frozen_string_literal: false +require_relative '../optparse' require 'time' OptionParser.accept(Time) do |s,| diff --git a/lib/optparse/uri.rb b/lib/optparse/uri.rb index 024dc69eac..31d10593b1 100644 --- a/lib/optparse/uri.rb +++ b/lib/optparse/uri.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: false # -*- ruby -*- -require 'optparse' +require_relative '../optparse' require 'uri' OptionParser.accept(URI) {|s,| URI.parse(s) if s} diff --git a/lib/optparse/version.rb b/lib/optparse/version.rb index 8e99836878..b5ed695146 100644 --- a/lib/optparse/version.rb +++ b/lib/optparse/version.rb @@ -1,55 +1,65 @@ +# frozen_string_literal: false # OptionParser internal utility class << OptionParser - def show_version(*pkg) + # + # Shows version string in packages if Version is defined. + # + # +pkgs+:: package list + # + def show_version(*pkgs) progname = ARGV.options.program_name - show = proc do |klass, version| - version = version.join(".") if Array === version + result = false + show = proc do |klass, cname, 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)})" + 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 pkg.size == 1 and pkg[0] == "all" + 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, version) + show.call(klass, cname.intern, version) end end else - pkg.each do |pkg| - /\A[A-Z]\w*((::|\/)[A-Z]\w*)*\z/ni =~ pkg or next + pkgs.each do |pkg| begin - pkg = eval(pkg) + pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)} v = case when pkg.const_defined?(:Version) - pkg.const_get(:Version) + pkg.const_get(n = :Version) when pkg.const_defined?(:VERSION) - pkg.const_get(:VERSION) + pkg.const_get(n = :VERSION) else + n = nil "unknown" end - show.call(pkg, v) + show.call(pkg, n, v) rescue NameError - puts "#{progname}: #$!" end end end - exit + result end - def each_const(path, klass = ::Object) - path.split(/::|\//).inject(klass) do |klass, name| + # :stopdoc: + + 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 - c = klass.const_get(c) + klass.const_get(c) end end end @@ -65,4 +75,6 @@ class << OptionParser end end end + + # :startdoc: end |
