mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/optparse.rb (OptionParser::Completion#complete): new parameter
to direct case insensitiveness. * lib/optparse.rb (OptionParser#order!): ignore case only for long option. [ruby-dev:25048] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7464 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
174052749c
commit
af3da7d124
3 changed files with 23 additions and 20 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,11 @@
|
|||
Sun Dec 5 19:39:17 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/optparse.rb (OptionParser::Completion#complete): new parameter
|
||||
to direct case insensitiveness.
|
||||
|
||||
* lib/optparse.rb (OptionParser#order!): ignore case only for long
|
||||
option. [ruby-dev:25048]
|
||||
|
||||
Sun Dec 5 00:54:32 2004 WATANABE Hirofumi <eban@ruby-lang.org>
|
||||
|
||||
* mkconfig.rb: setup library paths before requiring library.
|
||||
|
@ -36,7 +44,7 @@ Sat Dec 4 14:28:56 2004 Dave Thomas <dave@pragprog.com>
|
|||
# :section: Dave's Section
|
||||
# comment material
|
||||
# -----------
|
||||
The lines before :section: are removed, and identical lines at the end are
|
||||
The lines before :section: are removed, and identical lines at the end are
|
||||
also removed if present.
|
||||
|
||||
Fri Dec 3 12:25:21 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
|
|
@ -207,9 +207,9 @@ class OptionParser
|
|||
# and resolved against a list of acceptable values.
|
||||
#
|
||||
module Completion
|
||||
def complete(key, pat = nil)
|
||||
def complete(key, icase = false, pat = nil)
|
||||
pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'),
|
||||
ignore_case?)
|
||||
icase)
|
||||
canon, sw, k, v, cn = nil
|
||||
candidates = []
|
||||
each do |k, *v|
|
||||
|
@ -250,10 +250,6 @@ class OptionParser
|
|||
def convert(opt = nil, val = nil, *)
|
||||
val
|
||||
end
|
||||
|
||||
def ignore_case?
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
@ -263,11 +259,6 @@ class OptionParser
|
|||
class OptionMap < Hash
|
||||
include Completion
|
||||
end
|
||||
class OptionCaseMap < OptionMap
|
||||
def ignore_case?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
|
@ -523,7 +514,7 @@ class OptionParser
|
|||
def initialize
|
||||
@atype = {}
|
||||
@short = OptionMap.new
|
||||
@long = OptionCaseMap.new
|
||||
@long = OptionMap.new
|
||||
@list = []
|
||||
end
|
||||
|
||||
|
@ -632,13 +623,15 @@ class OptionParser
|
|||
# searching list.
|
||||
# : ((|opt|))
|
||||
# searching key.
|
||||
# : ((|icase|))
|
||||
# search case insensitive if true.
|
||||
# : ((|*pat|))
|
||||
# optional pattern for completion.
|
||||
# : (({block}))
|
||||
# yielded with the found value when succeeded.
|
||||
#
|
||||
def complete(id, opt, *pat, &block)
|
||||
__send__(id).complete(opt, *pat, &block)
|
||||
def complete(id, opt, icase = false, *pat, &block)
|
||||
__send__(id).complete(opt, icase, *pat, &block)
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -1277,7 +1270,7 @@ class OptionParser
|
|||
when /\A--([^=]*)(?:=(.*))?/
|
||||
opt, rest = $1, $2
|
||||
begin
|
||||
sw, = complete(:long, opt)
|
||||
sw, = complete(:long, opt, true)
|
||||
rescue ParseError
|
||||
raise $!.set_option(arg, true)
|
||||
end
|
||||
|
@ -1431,17 +1424,19 @@ class OptionParser
|
|||
searching table.
|
||||
: ((|opt|))
|
||||
searching key.
|
||||
: ((|icase|))
|
||||
search case insensitive if true.
|
||||
: ((|*pat|))
|
||||
optional pattern for completion.
|
||||
: (({block}))
|
||||
yielded with the found value when succeeded.
|
||||
=end #'#"#`#
|
||||
def complete(typ, opt, *pat)
|
||||
def complete(typ, opt, icase = false, *pat)
|
||||
if pat.empty?
|
||||
search(typ, opt) {|sw| return [sw, opt]} # exact match or...
|
||||
end
|
||||
raise AmbiguousOption, catch(:ambiguous) {
|
||||
visit(:complete, typ, opt, *pat) {|opt, *sw| return sw}
|
||||
visit(:complete, typ, opt, icase, *pat) {|opt, *sw| return sw}
|
||||
raise InvalidOption, opt
|
||||
}
|
||||
end
|
||||
|
|
|
@ -31,8 +31,8 @@ module TestOptionParser::NoArg
|
|||
assert_equal(%w"", no_error {@opt.parse!(%w"-o")})
|
||||
assert_equal(true, @flag)
|
||||
@flag = nil
|
||||
no_error {@opt.parse!(%w"-O")}
|
||||
assert_equal(true, @flag)
|
||||
assert_raises(OptionParser::InvalidOption) {@opt.parse!(%w"-O")}
|
||||
assert_nil(@flag)
|
||||
@flag = nil
|
||||
assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")})
|
||||
assert_equal(true, @flag)
|
||||
|
|
Loading…
Reference in a new issue