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

add suffix and Levenshtein matching for invalid option names #7637

Conversation

Projects
None yet
5 participants
@cosmicexplorer
Copy link
Contributor

commented Apr 29, 2019

Problem

When typing out a pants command line, a user may wish to use many options in many different scopes, which may take a lot of typing. If any of those options are misspelled, Pants currently doesn't display any help to the user helping them to figure it out, e.g.:

> ./pants --python-setup-interpreter-search-path="['/usr/bin']" options
timestamp: 2019-04-28T23:44:14.453055
Exception caught: (pants.option.errors.ParseError)
  ...
  File "/Users/dmcclanahan/workspace/source-v2/.pex/install/pantsbuild.pants-1.15.0rc0+git5c01b701-cp27-cp27m-macosx_10_11_x86_64.whl.850db4d17434427af3fd197127b57022c1d69259/pantsbuild.pants-1.15.0rc0+git5c01b701-cp27-cp27m-macosx_10_11_x86_64.whl/pants/option/parser.py", line 225, in parse_args
    self._scope_str(), ', '.join(flag_value_map.keys())))

Exception message: Unrecognized command line flags on scope 'python-setup': --interpreter-search-path

Solution

  • When unregistered options are found, fuzzy match them against a list of all registered options in all their scopes.
    • Add suffix matching to suggest containing scopes for options incorrectly provided in the global scope.
    • Add similarity matching via Levenshtein edit distance for misspelled options (configurable with the --option-name-check-distance global bootstrap option).

Result

> ./pants --python-setup-interpreter-search-path="['/usr/bin']" options
Exception caught: (pants.option.errors.ParseError)
  ...
  File "/Users/dmcclanahan/tools/pants/src/python/pants/option/parser.py", line 324, in _raise_error_for_invalid_flag_names
    suggestions=suggestions_message))

Exception message: Unrecognized command line flags on scope 'python-setup': --interpreter-search-path. Suggestions:
--interpreter-search-path: [--python-setup-interpreter-search-paths]

Incorrectly-typed option names become less of a problem for new users!

@illicitonion
Copy link
Contributor

left a comment

This is great, thanks!

Show resolved Hide resolved src/python/pants/option/parser.py Outdated
@Eric-Arellano
Copy link
Contributor

left a comment

Great UX win. Thank you Danny for doing this!

@@ -26,6 +26,7 @@ pyopenssl==17.3.0
pystache==0.5.3
pytest-cov>=2.5,<2.6
pytest>=3.4,<4.0
python-Levenshtein==0.12.0

This comment has been minimized.

Copy link
@Eric-Arellano

Eric-Arellano Apr 29, 2019

Contributor

I thought for a second you were going to re-implement the algorithm. Glad this library exists.

Show resolved Hide resolved src/python/pants/option/global_options.py Outdated
Show resolved Hide resolved src/python/pants/option/options.py Outdated
Show resolved Hide resolved src/python/pants/option/parser.py

@cosmicexplorer cosmicexplorer force-pushed the cosmicexplorer:suggest-similar-options-in-scope branch from bddfd8f to ce46248 Apr 30, 2019

@cosmicexplorer cosmicexplorer requested a review from Eric-Arellano Apr 30, 2019

@benjyw

This comment has been minimized.

Copy link
Contributor

commented Apr 30, 2019

This is awesome! Thanks @cosmicexplorer .

I'll use this forum to also call attention to ./pants bash-completion which will generate a bash autocompletion script that you can either copy to /etc/bash-completion.d/pants-completion.bash or source in ~/.bashrc.

@cosmicexplorer

This comment has been minimized.

Copy link
Contributor Author

commented Apr 30, 2019

I'll use this forum to also call attention to ./pants bash-completion which will generate a bash autocompletion script that you can either copy to /etc/bash-completion.d/pants-completion.bash or source in ~/.bashrc.

Yes! I realized this while making this PR, it is the other consumer of Options#walk_parsers() which made this change so easy to make:

self.context.options.walk_parsers(get_from_parser)

@cosmicexplorer cosmicexplorer merged commit d80a5ee into pantsbuild:master Apr 30, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.