summaryrefslogtreecommitdiff
path: root/lib/optparse
diff options
context:
space:
mode:
Diffstat (limited to 'lib/optparse')
-rw-r--r--lib/optparse/ac.rb51
-rw-r--r--lib/optparse/date.rb1
-rw-r--r--lib/optparse/kwargs.rb17
-rw-r--r--lib/optparse/shellwords.rb1
-rw-r--r--lib/optparse/time.rb1
-rw-r--r--lib/optparse/uri.rb1
-rw-r--r--lib/optparse/version.rb13
7 files changed, 79 insertions, 6 deletions
diff --git a/lib/optparse/ac.rb b/lib/optparse/ac.rb
new file mode 100644
index 0000000000..fb0883f97a
--- /dev/null
+++ b/lib/optparse/ac.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: false
+require 'optparse'
+
+class OptionParser::AC < 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
+
+ def _ac_arg_enable(prefix, name, help_string, block)
+ _check_ac_args(name, block)
+
+ sdesc = []
+ ldesc = ["--#{prefix}-#{name}"]
+ desc = [help_string]
+ q = name.downcase
+ enable = Switch::NoArgument.new(nil, proc {true}, sdesc, ldesc, nil, desc, block)
+ disable = Switch::NoArgument.new(nil, proc {false}, sdesc, ldesc, nil, desc, 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/optparse/date.rb b/lib/optparse/date.rb
index d680559f37..d6649c83f1 100644
--- a/lib/optparse/date.rb
+++ b/lib/optparse/date.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'optparse'
require 'date'
diff --git a/lib/optparse/kwargs.rb b/lib/optparse/kwargs.rb
new file mode 100644
index 0000000000..ed58cc142b
--- /dev/null
+++ b/lib/optparse/kwargs.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+require 'optparse'
+
+class OptionParser
+ 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/optparse/shellwords.rb b/lib/optparse/shellwords.rb
index 0422d7c887..bf31701b96 100644
--- a/lib/optparse/shellwords.rb
+++ b/lib/optparse/shellwords.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
# -*- ruby -*-
require 'shellwords'
diff --git a/lib/optparse/time.rb b/lib/optparse/time.rb
index 402cadcf16..ffc6ff000d 100644
--- a/lib/optparse/time.rb
+++ b/lib/optparse/time.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'optparse'
require 'time'
diff --git a/lib/optparse/uri.rb b/lib/optparse/uri.rb
index 024dc69eac..51550cf91b 100644
--- a/lib/optparse/uri.rb
+++ b/lib/optparse/uri.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
# -*- ruby -*-
require 'optparse'
diff --git a/lib/optparse/version.rb b/lib/optparse/version.rb
index 558d9d710b..b869d8fe51 100644
--- a/lib/optparse/version.rb
+++ b/lib/optparse/version.rb
@@ -1,7 +1,8 @@
+# frozen_string_literal: false
# OptionParser internal utility
class << OptionParser
- def show_version(*pkg)
+ def show_version(*pkgs)
progname = ARGV.options.program_name
result = false
show = proc do |klass, cname, version|
@@ -19,14 +20,14 @@ class << OptionParser
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, cname.intern, version)
end
end
else
- pkg.each do |pkg|
+ pkgs.each do |pkg|
begin
pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)}
v = case
@@ -46,12 +47,12 @@ class << OptionParser
result
end
- def each_const(path, klass = ::Object)
- path.split(/::|\//).inject(klass) do |klass, name|
+ 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