Skip to content

Commit

Permalink
Removing cloaker, bypassing suggestions if DidYouMean is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
nanobowers committed Apr 27, 2024
1 parent 975d280 commit a7b13fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
17 changes: 4 additions & 13 deletions lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ def initialize(*a, &b)
@settings = DEFAULT_SETTINGS
end

# instance_eval(&b) if b # can't take arguments
#cloaker(&b).bind(self).call(*a) if b
self.instance_exec(*a, &b) if block_given?
end

Expand Down Expand Up @@ -249,7 +247,10 @@ def educate_on_error
def handle_unknown_argument(arg, candidates, suggestions)
errstring = "unknown argument '#{arg}'"
errstring += " for command '#{subcommand_name}'" if self.respond_to?(:subcommand_name)
if suggestions
if (suggestions &&
Module::const_defined?("DidYouMean") &&
Module::const_defined?("DidYouMean::JaroWinkler") &&
Module::const_defined?("DidYouMean::Levenshtein"))
input = arg.sub(/^[-]*/,'')

# Code borrowed from did_you_mean gem
Expand Down Expand Up @@ -620,16 +621,6 @@ def wrap_line(str, opts = {})
ret
end

## instance_eval but with ability to handle block arguments
## thanks to _why: http://redhanded.hobix.com/inspect/aBlockCostume.html
def cloaker(&b)
(class << self; self; end).class_eval do
define_method :cloaker_, &b
meth = instance_method :cloaker_
remove_method :cloaker_
meth
end
end
end

class Option
Expand Down
7 changes: 7 additions & 0 deletions test/optimist/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def test_unknown_arguments
end

def test_unknown_arguments_with_suggestions
unless (Module::const_defined?("DidYouMean") &&
Module::const_defined?("DidYouMean::JaroWinkler") &&
Module::const_defined?("DidYouMean::Levenshtein"))
# if we cannot
skip("Skipping because DidYouMean was not found")
return false
end
sugp = Parser.new(:suggestions => true)
err = assert_raises(CommandlineError) { sugp.parse(%w(--bone)) }
assert_match(/unknown argument '--bone'$/, err.message)
Expand Down

0 comments on commit a7b13fa

Please sign in to comment.