* 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
This commit is contained in:
parent
f11f59972f
commit
5cb7d10ee0
@ -1,3 +1,8 @@
|
|||||||
|
Wed Sep 13 01:14:02 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/optparse.rb (OptionParser#getopts): works with pre-registered
|
||||||
|
options. [ruby-core:08826]
|
||||||
|
|
||||||
Tue Sep 12 03:58:39 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Tue Sep 12 03:58:39 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* hash.c (rb_hash_compare_by_identity): rename Hash#identical to
|
* hash.c (rb_hash_compare_by_identity): rename Hash#identical to
|
||||||
|
@ -346,16 +346,12 @@ class OptionParser
|
|||||||
# exception.
|
# exception.
|
||||||
#
|
#
|
||||||
def conv_arg(arg, val = nil)
|
def conv_arg(arg, val = nil)
|
||||||
if block
|
if conv
|
||||||
if conv
|
val = conv.yield(val)
|
||||||
val = conv.yield(val)
|
|
||||||
else
|
|
||||||
val = *val
|
|
||||||
end
|
|
||||||
return arg, block, val
|
|
||||||
else
|
else
|
||||||
return arg, nil
|
val = *val
|
||||||
end
|
end
|
||||||
|
return arg, block, val
|
||||||
end
|
end
|
||||||
private :conv_arg
|
private :conv_arg
|
||||||
|
|
||||||
@ -414,6 +410,13 @@ class OptionParser
|
|||||||
@pattern =~ str unless @short or @long
|
@pattern =~ str unless @short or @long
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Main name of the switch.
|
||||||
|
#
|
||||||
|
def switch_name
|
||||||
|
(long.first || short.first).sub(/\A-+(?:\[no-\])?/, '').intern
|
||||||
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Switch that takes no arguments.
|
# Switch that takes no arguments.
|
||||||
#
|
#
|
||||||
@ -1236,6 +1239,11 @@ class OptionParser
|
|||||||
# Same as #order, but removes switches destructively.
|
# Same as #order, but removes switches destructively.
|
||||||
#
|
#
|
||||||
def order!(argv = default_argv, &nonopt)
|
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
|
opt, arg, sw, val, rest = nil
|
||||||
nonopt ||= proc {|arg| throw :terminate, arg}
|
nonopt ||= proc {|arg| throw :terminate, arg}
|
||||||
argv.unshift(arg) if arg = catch(:terminate) {
|
argv.unshift(arg) if arg = catch(:terminate) {
|
||||||
@ -1250,8 +1258,9 @@ class OptionParser
|
|||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
opt, sw, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
||||||
sw.call(*val) if sw
|
val = cb.call(*val) if cb
|
||||||
|
setter.call(sw.switch_name, val) if setter
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, rest)
|
raise $!.set_option(arg, rest)
|
||||||
end
|
end
|
||||||
@ -1278,10 +1287,11 @@ class OptionParser
|
|||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
begin
|
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}"
|
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
|
||||||
argv.unshift(opt) if opt and (opt = opt.sub(/\A-*/, '-')) != '-'
|
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
|
rescue ParseError
|
||||||
raise $!.set_option(arg, arg.length > 2)
|
raise $!.set_option(arg, arg.length > 2)
|
||||||
end
|
end
|
||||||
@ -1348,16 +1358,16 @@ class OptionParser
|
|||||||
#
|
#
|
||||||
# Wrapper method for getopts.rb.
|
# Wrapper method for getopts.rb.
|
||||||
#
|
#
|
||||||
def getopts(argv, single_options, *long_options)
|
def getopts(argv = default_argv, single_options = nil, *long_options)
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
single_options.scan(/(.)(:)?/) do |opt, val|
|
single_options.scan(/(.)(:)?/) do |opt, val|
|
||||||
if val
|
if val
|
||||||
result[opt] = nil
|
result[opt] = nil
|
||||||
define("-#{opt} VAL") {|val| result[opt] = val}
|
define("-#{opt} VAL")
|
||||||
else
|
else
|
||||||
result[opt] = false
|
result[opt] = false
|
||||||
define("-#{opt}") {result[opt] = true}
|
define("-#{opt}")
|
||||||
end
|
end
|
||||||
end if single_options
|
end if single_options
|
||||||
|
|
||||||
@ -1365,14 +1375,14 @@ class OptionParser
|
|||||||
opt, val = arg.split(':', 2)
|
opt, val = arg.split(':', 2)
|
||||||
if val
|
if val
|
||||||
result[opt] = val.empty? ? nil : val
|
result[opt] = val.empty? ? nil : val
|
||||||
define("--#{opt} VAL") {|val| result[opt] = val}
|
define("--#{opt} VAL")
|
||||||
else
|
else
|
||||||
result[opt] = false
|
result[opt] = false
|
||||||
define("--#{opt}") {result[opt] = true}
|
define("--#{opt}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
order!(argv)
|
parse_in_order(argv, result.method(:[]=))
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user