ruby-changes:58310
From: Nobuyoshi <ko1@a...>
Date: Fri, 18 Oct 2019 17:42:32 +0900 (JST)
Subject: [ruby-changes:58310] b4d308b419 (master): Defer adding additional info until getting the message of an error
https://git.ruby-lang.org/ruby.git/commit/?id=b4d308b419 From b4d308b41939659fe8a4df28afc82eb5205709d1 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 18 Oct 2019 17:39:16 +0900 Subject: Defer adding additional info until getting the message of an error diff --git a/lib/optparse.rb b/lib/optparse.rb index c6e72d6..67beeb9 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1770,21 +1770,25 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1770 end raise AmbiguousOption, catch(:ambiguous) { visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw} - if defined? DidYouMean::SpellChecker - all_candidates = [] - visit(:get_candidates, typ) do |candidates| - all_candidates.concat(candidates) - end - all_candidates.select! {|cand| cand.is_a?(String) } - suggestions = DidYouMean::SpellChecker.new(dictionary: all_candidates).correct(opt) - raise InvalidOption.new(opt, DidYouMean.formatter.message_for(suggestions)) - else - raise InvalidOption, opt - end + raise InvalidOption.new(opt, additional: self.:additional_message.curry[typ]) } end private :complete + # + # Returns additional info. + # + def additional_message(typ, opt) + return unless typ and opt and defined?(DidYouMean::SpellChecker) + all_candidates = [] + visit(:get_candidates, typ) do |candidates| + all_candidates.concat(candidates) + end + all_candidates.select! {|cand| cand.is_a?(String) } + checker = DidYouMean::SpellChecker.new(dictionary: all_candidates) + DidYouMean.formatter.message_for(checker.correct(opt)) + end + def candidate(word) list = [] case word @@ -2011,13 +2015,16 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L2015 # Reason which caused the error. Reason = 'parse error' - def initialize(*args) + def initialize(*args, additional: nil) + @additional = additional + @arg0, = args @args = args @reason = nil end attr_reader :args attr_writer :reason + attr_accessor :additional # # Pushes back erred argument(s) to +argv+. @@ -2062,7 +2069,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L2069 # Default stringizing method to emit standard error message. # def message - reason + ': ' + args.join(" ").gsub(/\s+$/, "") + "#{reason}: #{args.join(' ')}#{additional[@arg0] if additional}" end alias to_s message -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/