summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-12 16:14:50 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-12 16:14:50 +0000
commit5cb7d10ee065d6141a6d6e796aecbb278b54f171 (patch)
tree0fc51e4a547428bd213f0db89e76d0197dbc874c /lib
parentf11f59972fa4940d36327640db1eb2997aa31046 (diff)
* lib/optparse.rb (OptionParser#getopts): works with pre-registered
options. [ruby-core:08826] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb46
1 files changed, 28 insertions, 18 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index e24d77e4e3..4f008f8b02 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -346,16 +346,12 @@ class OptionParser
# exception.
#
def conv_arg(arg, val = nil)
- if block
- if conv
- val = conv.yield(val)
- else
- val = *val
- end
- return arg, block, val
+ if conv
+ val = conv.yield(val)
else
- return arg, nil
+ val = *val
end
+ return arg, block, val
end
private :conv_arg
@@ -415,6 +411,13 @@ class OptionParser
end
#
+ # Main name of the switch.
+ #
+ def switch_name
+ (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '').intern
+ end
+
+ #
# Switch that takes no arguments.
#
class NoArgument < self
@@ -1236,6 +1239,11 @@ class OptionParser
# Same as #order, but removes switches destructively.
#
def order!(argv = default_argv, &nonopt)
+ parse_in_order(argv, &nonopt)
+ end
+
+ # :nodoc:
+ def parse_in_order(argv = default_argv, setter = nil, &nonopt)
opt, arg, sw, val, rest = nil
nonopt ||= proc {|arg| throw :terminate, arg}
argv.unshift(arg) if arg = catch(:terminate) {
@@ -1250,8 +1258,9 @@ class OptionParser
raise $!.set_option(arg, true)
end
begin
- opt, sw, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
- sw.call(*val) if sw
+ opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
+ val = cb.call(*val) if cb
+ setter.call(sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
@@ -1278,10 +1287,11 @@ class OptionParser
raise $!.set_option(arg, true)
end
begin
- opt, sw, *val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
+ opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
argv.unshift(opt) if opt and (opt = opt.sub(/\A-*/, '-')) != '-'
- sw.call(*val) if sw
+ val = cb.call(*val) if cb
+ setter.call(sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
end
@@ -1348,16 +1358,16 @@ class OptionParser
#
# Wrapper method for getopts.rb.
#
- def getopts(argv, single_options, *long_options)
+ def getopts(argv = default_argv, single_options = nil, *long_options)
result = {}
single_options.scan(/(.)(:)?/) do |opt, val|
if val
result[opt] = nil
- define("-#{opt} VAL") {|val| result[opt] = val}
+ define("-#{opt} VAL")
else
result[opt] = false
- define("-#{opt}") {result[opt] = true}
+ define("-#{opt}")
end
end if single_options
@@ -1365,14 +1375,14 @@ class OptionParser
opt, val = arg.split(':', 2)
if val
result[opt] = val.empty? ? nil : val
- define("--#{opt} VAL") {|val| result[opt] = val}
+ define("--#{opt} VAL")
else
result[opt] = false
- define("--#{opt}") {result[opt] = true}
+ define("--#{opt}")
end
end
- order!(argv)
+ parse_in_order(argv, result.method(:[]=))
result
end