Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for DidYouMean when long options are spelled incorrectly #150

Merged
merged 4 commits into from May 1, 2024

Conversation

nanobowers
Copy link
Collaborator

Enables "did_you_mean" style suggestions for long options.
This works similar to how it does when you misspell something in Ruby, but applied to long options in Optimist.

It uses the same string-distance algorithm as Ruby to see if any of the long options are within a reasonable distance. The DidYouMean feature was added in ruby2.3, so this will not work with older rubies but I dont believe we support those anymore.

There is a keyword option that can be passed in at the Optimist.options() level to disable suggestions, e.g.
Optimist.options(ARGV, suggestions: false) and get legacy behavior. Some tweaks to how options are merged was necessary for this.

examples script and output provided in the /examples directory.

@miq-bot add-label enhancement
@miq-bot add-reviewer @Fryguy

@nanobowers
Copy link
Collaborator Author

did_you_mean was added in ruby 2.3, so unclear why the CI fails. I can't reproduce the errors on my system w/ RVM using those version of ruby/jruby

lib/optimist.rb Outdated
# instance_eval(&b) if b # can't take arguments
cloaker(&b).bind(self).call(*a) if b
#cloaker(&b).bind(self).call(*a) if b
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this line just be deleted? I'm curious why you stopped using the cloaker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, should be deleted, I just removed the call to it and the cloaker method.
AFAICT the cloaker was a super clever trick used before instance_exec was in ruby, since instance_eval didnt support passing a block, but i could be wrong about that...

In any event, instance_exec seems to work such that all tests pass and doesnt require weird trickery, so felt like the cloaker could be safely removed.

@Fryguy Fryguy self-assigned this May 1, 2024
@Fryguy Fryguy merged commit 7431ace into ManageIQ:master May 1, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants