diff options
author | Nobuyoshi Nakada <[email protected]> | 2019-10-18 17:39:16 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2019-10-18 17:39:16 +0900 |
commit | b4d308b41939659fe8a4df28afc82eb5205709d1 (patch) | |
tree | f72746f7a12d7515ffe6eb4925e1776eb7892bb1 | |
parent | e29e49abf018730d8419b4797ff17942e00d810b (diff) |
Defer adding additional info until getting the message of an error
-rw-r--r-- | lib/optparse.rb | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index c6e72d611f..67beeb9ee3 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1770,21 +1770,25 @@ XXX 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 # 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 # 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 |