Skip to content

Commit 44dfb35

Browse files
committed
Fix up did_you_mean on older ruby versions
1 parent 0879c17 commit 44dfb35

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

lib/thor/error.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
class Thor
2+
Correctable =
3+
begin
4+
require 'did_you_mean'
5+
DidYouMean::Correctable
6+
rescue LoadError
7+
end
8+
29
# Thor::Error is raised when it's caused by wrong usage of thor classes. Those
310
# errors have their backtrace suppressed and are nicely shown to the user.
411
#
@@ -38,7 +45,7 @@ def initialize(command, all_commands, namespace)
3845
super(message)
3946
end
4047

41-
prepend DidYouMean::Correctable
48+
prepend Correctable if Correctable
4249
end
4350
UndefinedTaskError = UndefinedCommandError
4451

@@ -78,7 +85,7 @@ def initialize(switches, unknown)
7885
super("Unknown switches #{unknown.map(&:inspect).join(', ')}")
7986
end
8087

81-
prepend DidYouMean::Correctable
88+
prepend Correctable if Correctable
8289
end
8390

8491
class RequiredArgumentMissingError < InvocationError
@@ -87,8 +94,10 @@ class RequiredArgumentMissingError < InvocationError
8794
class MalformattedArgumentError < InvocationError
8895
end
8996

90-
DidYouMean::SPELL_CHECKERS.merge!(
91-
'Thor::UndefinedCommandError' => UndefinedCommandError::SpellChecker,
92-
'Thor::UnknownArgumentError' => UnknownArgumentError::SpellChecker
93-
)
97+
if Correctable
98+
DidYouMean::SPELL_CHECKERS.merge!(
99+
'Thor::UndefinedCommandError' => UndefinedCommandError::SpellChecker,
100+
'Thor::UnknownArgumentError' => UnknownArgumentError::SpellChecker
101+
)
102+
end
94103
end

spec/base_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,8 @@ def hello
263263
end
264264

265265
it "suggests commands that are similar if there is a typo" do
266-
expected = <<~MSG
267-
Could not find command "paintz" in "barn" namespace.
268-
Did you mean? "paint"
269-
MSG
266+
expected = "Could not find command \"paintz\" in \"barn\" namespace.\n"
267+
expected << "Did you mean? \"paint\"" if Thor::Correctable
270268

271269
expect(capture(:stderr) { Barn.start(%w(paintz)) }).to eq(expected)
272270
end

spec/parser/options_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ def remaining
114114
create :foo => "baz", :bar => :required
115115
parse("--bar", "baz", "--baz", "unknown")
116116

117-
expected = <<~MSG.chomp
118-
Unknown switches "--baz"
119-
Did you mean? "--bar"
120-
MSG
117+
expected = "Unknown switches \"--baz\""
118+
expected << "\nDid you mean? \"--bar\"" if Thor::Correctable
121119

122120
expect { check_unknown! }.to raise_error(Thor::UnknownArgumentError, expected)
123121
end

0 commit comments

Comments
 (0)