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

CLI: do not provide command hints during tab-completion #5012

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Jul 6, 2021

Fixes #3815

The verdi command uses a custom Group subclass that overrides the
get_command method to provide some additional information in case the
provided command name does not exist. These hints should help the user
spot potential typos by giving a list of existing commands with similar
names.

However, this feature was also being triggered during tab-completion.
For example, typing verdi comput list followed by activating
tab-completion would result in the error being displayed since comput
is not a valid command. In this case, one does not want to display the
error message with hint at all.

The tricky part is that there is no canonical way to distinguish between
a normal command execution and a tab-completion event. The best bet is
to use the resilient_parsing attribute on the Context which is set
to True during tab-completion. Although this attribute was introduced
into click directly to support auto-completion functionality, the
problem is that this is not the only use case for which this flag can be
set. It is therefore possible that there is some code path where this
flag is set to True but it does not actually correspond to a
tab-completion event. For now there doesn't seem to be a better solution
though and in most cases this approach should work.

The `verdi` command uses a custom `Group` subclass that overrides the
`get_command` method to provide some additional information in case the
provided command name does not exist. These hints should help the user
spot potential typos by giving a list of existing commands with similar
names.

However, this feature was also being triggered during tab-completion.
For example, typing `verdi comput list` followed by activating
tab-completion would result in the error being displayed since `comput`
is not a valid command. In this case, one does not want to display the
error message with hint at all.

The tricky part is that there is no canonical way to distinguish between
a normal command execution and a tab-completion event. The best bet is
to use the `resilient_parsing` attribute on the `Context` which is set
to `True` during tab-completion. Although this attribute was introduced
into `click` directly to support auto-completion functionality, the
problem is that this is not the only use case for which this flag can be
set. It is therefore possible that there is some code path where this
flag is set to `True` but it does not actually correspond to a
tab-completion event. For now there doesn't seem to be a better solution
though and in most cases this approach should work.
@sphuber sphuber requested a review from giovannipizzi July 6, 2021 12:37
@codecov
Copy link

codecov bot commented Jul 6, 2021

Codecov Report

Merging #5012 (ae521d9) into develop (a2ddbd5) will increase coverage by 0.01%.
The diff coverage is 50.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5012      +/-   ##
===========================================
+ Coverage    80.12%   80.12%   +0.01%     
===========================================
  Files          515      515              
  Lines        36692    36694       +2     
===========================================
+ Hits         29397    29399       +2     
  Misses        7295     7295              
Flag Coverage Δ
django 74.59% <50.00%> (-<0.01%) ⬇️
sqlalchemy 73.52% <50.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/cmdline/commands/cmd_verdi.py 82.86% <50.00%> (-1.99%) ⬇️
aiida/transports/util.py 65.63% <0.00%> (+3.13%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2ddbd5...ae521d9. Read the comment docs.

Copy link
Member

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

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

Thanks! I think that's OK - as a note (for myself mostly), I tried and just returning will trigger the 'default' bash completion (files in the current folder). I think that's OK and in line with other codes.

Also, I think using resilient_parsing is OK - maybe there are other places where this will be needed, but I think we're being more resilient than before with this change, so this should be good.

@sphuber sphuber merged commit 8e763bb into aiidateam:develop Jul 6, 2021
@sphuber sphuber deleted the fix/3815/verdi-no-error-during-tab-complete branch July 6, 2021 13:33
sphuber added a commit that referenced this pull request Aug 9, 2021
The `verdi` command uses a custom `Group` subclass that overrides the
`get_command` method to provide some additional information in case the
provided command name does not exist. These hints should help the user
spot potential typos by giving a list of existing commands with similar
names.

However, this feature was also being triggered during tab-completion.
For example, typing `verdi comput list` followed by activating
tab-completion would result in the error being displayed since `comput`
is not a valid command. In this case, one does not want to display the
error message with hint at all.

The tricky part is that there is no canonical way to distinguish between
a normal command execution and a tab-completion event. The best bet is
to use the `resilient_parsing` attribute on the `Context` which is set
to `True` during tab-completion. Although this attribute was introduced
into `click` directly to support auto-completion functionality, the
problem is that this is not the only use case for which this flag can be
set. It is therefore possible that there is some code path where this
flag is set to `True` but it does not actually correspond to a
tab-completion event. For now there doesn't seem to be a better solution
though and in most cases this approach should work.

Cherry-pick: 8e763bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TAB completion crashes on unknown commands if followed by parameters
2 participants