From 87bb4b54689c6d90d168ea1b7afda33a8f47e5e2 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 13 Nov 2003 02:31:17 +0000 Subject: [PATCH] * lib/optparse.rb (OptionParser::Completion#complete): prior shorter name to containing longer name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4947 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/optparse.rb | 32 +++++++++++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 569212f3c5..830c5a5fd8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Nov 13 11:31:14 2003 Nobuyoshi Nakada + + * lib/optparse.rb (OptionParser::Completion#complete): prior shorter + name to containing longer name. + Thu Nov 13 06:08:54 2003 Hidetoshi NAGAI * ext/tk/lib/tk.rb: stop freezing some classes diff --git a/lib/optparse.rb b/lib/optparse.rb index 96609b219d..c00b152a21 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -82,18 +82,28 @@ Keyword completion module. def complete(key, pat = nil) pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+(?=.)/, '\&\w*'), ignore_case?) - canon, sw, k, v = nil + canon, sw, k, v, cn = nil each do |k, *v| (if Regexp === k + kn = nil k === key else - pat === (defined?(k.id2name) ? k.id2name : k) + kn = defined?(k.id2name) ? k.id2name : k + pat === kn end) or next v << k if v.empty? - unless canon - canon, sw = k, v - else - throw :ambiguous, key unless sw == v + if !canon + canon, sw, cn = k, v, kn + elsif sw != v + if String === cn and String === kn + if cn.rindex(kn, 0) + canon, sw, cn = k, v, kn + next + elsif kn.rindex(canon, 0) + next + end + end + throw :ambiguous, key end end if canon @@ -564,7 +574,7 @@ summary feature. def match(key) return key, *fetch(key) { raise AmbiguousArgument, catch(:ambiguous) {return complete(key)} - }.to_a + } end end @@ -995,11 +1005,7 @@ Default options, which never appear in option summary. else raise ArgumentError, "argument pattern given twice" end - if Array === o - o.each {|o| pattern[(Array === o ? o[0] : o)] = o} - else - pattern.update(o) - end + o.each {|(o, *v)| pattern[o] = v.fetch(0) {o}} when Module raise ArgumentError, "unsupported argument type: #{o}" when *ArgumentStyle.keys @@ -1191,7 +1197,7 @@ Default options, which never appear in option summary. argv.unshift(opt) if opt and (opt = opt.sub(/\A-*/, '-')) != '-' sw.call(val) if sw rescue ParseError - raise $!.set_option(arg, has_arg) + raise $!.set_option(arg, arg.length > 2) end # non-option argument