Skip to content

Commit 2b347d4

Browse files
Merge pull request #357 from ssrihari/master
Displays a message without a stack trace for ambiguous commands
2 parents 6ae2fa1 + a651381 commit 2b347d4

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

lib/thor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def normalize_command_name(meth) #:nodoc:
421421

422422
possibilities = find_command_possibilities(meth)
423423
if possibilities.size > 1
424-
raise ArgumentError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]"
424+
raise AmbiguousTaskError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]"
425425
elsif possibilities.size < 1
426426
meth = meth || default_command
427427
elsif map[meth]

lib/thor/error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class UndefinedCommandError < Error
1313
end
1414
UndefinedTaskError = UndefinedCommandError
1515

16+
class AmbiguousCommandError < Error
17+
end
18+
AmbiguousTaskError = AmbiguousCommandError
19+
1620
# Raised when a command was found, but not invoked properly.
1721
class InvocationError < Error
1822
end

spec/thor_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,16 @@ def boring(*args)
303303
end
304304

305305
context "when the user enters an ambiguous substring of a command" do
306-
it "raises an exception that explains the ambiguity" do
307-
expect{ MyScript.start(["call"]) }.to raise_error(ArgumentError, 'Ambiguous command call matches [call_myself_with_wrong_arity, call_unexistent_method]')
306+
it "raises an exception and displays a message that explains the ambiguity" do
307+
shell = Thor::Base.shell.new
308+
expect(shell).to receive(:error).with('Ambiguous command call matches [call_myself_with_wrong_arity, call_unexistent_method]')
309+
MyScript.start(["call"], :shell => shell)
308310
end
309311

310312
it "raises an exception when there is an alias" do
311-
expect{ MyScript.start(["f"]) }.to raise_error(ArgumentError, 'Ambiguous command f matches [foo, fu]')
313+
shell = Thor::Base.shell.new
314+
expect(shell).to receive(:error).with('Ambiguous command f matches [foo, fu]')
315+
MyScript.start(["f"], :shell => shell)
312316
end
313317
end
314318

0 commit comments

Comments
 (0)