-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide help when there is a typo in a command (#138)
Add a basic spell checker to make suggestions when the command could have a typo.
- Loading branch information
Showing
5 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dry/cli/program_name" | ||
require "did_you_mean" | ||
|
||
module Dry | ||
class CLI | ||
# Command(s) usage | ||
# | ||
# @since 1.1.1 | ||
# @api private | ||
module SpellChecker | ||
# @since 1.1.1 | ||
# @api private | ||
def self.call(result, arguments) | ||
commands = result.children.keys | ||
cmd = cmd_to_spell(arguments, result.names) | ||
|
||
suggestions = DidYouMean::SpellChecker.new(dictionary: commands).correct(cmd.first) | ||
if suggestions.any? | ||
"I don't know how to '#{cmd.join(" ")}'. Did you mean: '#{suggestions.first}' ?" | ||
end | ||
end | ||
|
||
# @since 1.1.1 | ||
# @api private | ||
def self.cmd_to_spell(arguments, result_names) | ||
arguments - result_names | ||
end | ||
|
||
# @since 1.1.1 | ||
# @api private | ||
def self.ignore?(cmd) | ||
cmd.empty? || cmd.first.start_with?("-") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "open3" | ||
|
||
RSpec.describe "Spell checker" do | ||
it "print similar command when there is a command with a typo" do | ||
_, stderr, = Open3.capture3("foo routs") | ||
|
||
expected = <<~DESC | ||
I don't know how to 'routs'. Did you mean: 'routes' ? | ||
Commands: | ||
foo assets [SUBCOMMAND] | ||
foo callbacks DIR # Command with callbacks | ||
foo console # Starts Foo console | ||
foo db [SUBCOMMAND] | ||
foo destroy [SUBCOMMAND] | ||
foo exec TASK [DIRS] # Execute a task | ||
foo generate [SUBCOMMAND] | ||
foo greeting [RESPONSE] | ||
foo hello # Print a greeting | ||
foo new PROJECT # Generate a new Foo project | ||
foo root-command [ARGUMENT|SUBCOMMAND] # Root command with arguments and subcommands | ||
foo routes # Print routes | ||
foo server # Start Foo server (only for development) | ||
foo sub [SUBCOMMAND] | ||
foo variadic [SUBCOMMAND] | ||
foo version # Print Foo version | ||
DESC | ||
|
||
expect(stderr).to eq(expected) | ||
end | ||
|
||
it "handles typos in subcommands" do | ||
_, stderr, = Open3.capture3("foo sub comand") | ||
|
||
expected = <<~DESC | ||
I don't know how to 'comand'. Did you mean: 'command' ? | ||
Commands: | ||
foo sub command # Override a subcommand | ||
DESC | ||
|
||
expect(stderr).to eq(expected) | ||
end | ||
end |