Skip to content

Commit b4d308b

Browse files
committed
Defer adding additional info until getting the message of an error
1 parent e29e49a commit b4d308b

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/optparse.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,21 +1770,25 @@ def complete(typ, opt, icase = false, *pat)
17701770
end
17711771
raise AmbiguousOption, catch(:ambiguous) {
17721772
visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
1773-
if defined? DidYouMean::SpellChecker
1774-
all_candidates = []
1775-
visit(:get_candidates, typ) do |candidates|
1776-
all_candidates.concat(candidates)
1777-
end
1778-
all_candidates.select! {|cand| cand.is_a?(String) }
1779-
suggestions = DidYouMean::SpellChecker.new(dictionary: all_candidates).correct(opt)
1780-
raise InvalidOption.new(opt, DidYouMean.formatter.message_for(suggestions))
1781-
else
1782-
raise InvalidOption, opt
1783-
end
1773+
raise InvalidOption.new(opt, additional: self.:additional_message.curry[typ])
17841774
}
17851775
end
17861776
private :complete
17871777

1778+
#
1779+
# Returns additional info.
1780+
#
1781+
def additional_message(typ, opt)
1782+
return unless typ and opt and defined?(DidYouMean::SpellChecker)
1783+
all_candidates = []
1784+
visit(:get_candidates, typ) do |candidates|
1785+
all_candidates.concat(candidates)
1786+
end
1787+
all_candidates.select! {|cand| cand.is_a?(String) }
1788+
checker = DidYouMean::SpellChecker.new(dictionary: all_candidates)
1789+
DidYouMean.formatter.message_for(checker.correct(opt))
1790+
end
1791+
17881792
def candidate(word)
17891793
list = []
17901794
case word
@@ -2011,13 +2015,16 @@ class ParseError < RuntimeError
20112015
# Reason which caused the error.
20122016
Reason = 'parse error'
20132017

2014-
def initialize(*args)
2018+
def initialize(*args, additional: nil)
2019+
@additional = additional
2020+
@arg0, = args
20152021
@args = args
20162022
@reason = nil
20172023
end
20182024

20192025
attr_reader :args
20202026
attr_writer :reason
2027+
attr_accessor :additional
20212028

20222029
#
20232030
# Pushes back erred argument(s) to +argv+.
@@ -2062,7 +2069,7 @@ def inspect
20622069
# Default stringizing method to emit standard error message.
20632070
#
20642071
def message
2065-
reason + ': ' + args.join(" ").gsub(/\s+$/, "")
2072+
"#{reason}: #{args.join(' ')}#{additional[@arg0] if additional}"
20662073
end
20672074

20682075
alias to_s message

0 commit comments

Comments
 (0)