1
0
Fork 0
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:
nobu 2004-12-05 10:39:58 +00:00
parent 174052749c
commit af3da7d124
3 changed files with 23 additions and 20 deletions

View file

@ -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> Sun Dec 5 00:54:32 2004 WATANABE Hirofumi <eban@ruby-lang.org>
* mkconfig.rb: setup library paths before requiring library. * 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 # :section: Dave's Section
# comment material # 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. also removed if present.
Fri Dec 3 12:25:21 2004 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Dec 3 12:25:21 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>

View file

@ -207,9 +207,9 @@ class OptionParser
# and resolved against a list of acceptable values. # and resolved against a list of acceptable values.
# #
module Completion 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*'), pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'),
ignore_case?) icase)
canon, sw, k, v, cn = nil canon, sw, k, v, cn = nil
candidates = [] candidates = []
each do |k, *v| each do |k, *v|
@ -250,10 +250,6 @@ class OptionParser
def convert(opt = nil, val = nil, *) def convert(opt = nil, val = nil, *)
val val
end end
def ignore_case?
false
end
end end
@ -263,11 +259,6 @@ class OptionParser
class OptionMap < Hash class OptionMap < Hash
include Completion include Completion
end end
class OptionCaseMap < OptionMap
def ignore_case?
true
end
end
# #
@ -523,7 +514,7 @@ class OptionParser
def initialize def initialize
@atype = {} @atype = {}
@short = OptionMap.new @short = OptionMap.new
@long = OptionCaseMap.new @long = OptionMap.new
@list = [] @list = []
end end
@ -632,13 +623,15 @@ class OptionParser
# searching list. # searching list.
# : ((|opt|)) # : ((|opt|))
# searching key. # searching key.
# : ((|icase|))
# search case insensitive if true.
# : ((|*pat|)) # : ((|*pat|))
# optional pattern for completion. # optional pattern for completion.
# : (({block})) # : (({block}))
# yielded with the found value when succeeded. # yielded with the found value when succeeded.
# #
def complete(id, opt, *pat, &block) def complete(id, opt, icase = false, *pat, &block)
__send__(id).complete(opt, *pat, &block) __send__(id).complete(opt, icase, *pat, &block)
end end
# #
@ -1277,7 +1270,7 @@ class OptionParser
when /\A--([^=]*)(?:=(.*))?/ when /\A--([^=]*)(?:=(.*))?/
opt, rest = $1, $2 opt, rest = $1, $2
begin begin
sw, = complete(:long, opt) sw, = complete(:long, opt, true)
rescue ParseError rescue ParseError
raise $!.set_option(arg, true) raise $!.set_option(arg, true)
end end
@ -1431,17 +1424,19 @@ class OptionParser
searching table. searching table.
: ((|opt|)) : ((|opt|))
searching key. searching key.
: ((|icase|))
search case insensitive if true.
: ((|*pat|)) : ((|*pat|))
optional pattern for completion. optional pattern for completion.
: (({block})) : (({block}))
yielded with the found value when succeeded. yielded with the found value when succeeded.
=end #'#"#`# =end #'#"#`#
def complete(typ, opt, *pat) def complete(typ, opt, icase = false, *pat)
if pat.empty? if pat.empty?
search(typ, opt) {|sw| return [sw, opt]} # exact match or... search(typ, opt) {|sw| return [sw, opt]} # exact match or...
end end
raise AmbiguousOption, catch(:ambiguous) { 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 raise InvalidOption, opt
} }
end end

View file

@ -31,8 +31,8 @@ module TestOptionParser::NoArg
assert_equal(%w"", no_error {@opt.parse!(%w"-o")}) assert_equal(%w"", no_error {@opt.parse!(%w"-o")})
assert_equal(true, @flag) assert_equal(true, @flag)
@flag = nil @flag = nil
no_error {@opt.parse!(%w"-O")} assert_raises(OptionParser::InvalidOption) {@opt.parse!(%w"-O")}
assert_equal(true, @flag) assert_nil(@flag)
@flag = nil @flag = nil
assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")}) assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")})
assert_equal(true, @flag) assert_equal(true, @flag)