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: fix shell completion of script for verdi run #5430

Merged
merged 2 commits into from
Mar 14, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Mar 12, 2022

When we moved to the built-in shell completion of click==8.0 instead
of the external click_completion, the script argument of verdi run
was no longer auto-completed. The reason is that click does not
autocomplete files for the STRING type.

The parameter type is changed to click.Path, which not only fixes the
problem of auto-completion, it also provides automatic validation by
making sure if it is an existing file and not a directory. This allows
to simplify the logic in the command body significantly as well as we no
longer need to handle the IOError of unreadable files manually.

@sphuber sphuber force-pushed the fix/verdi-run-shell-complete branch from 76a7fef to 8849518 Compare March 12, 2022 12:09
@sphuber
Copy link
Contributor Author

sphuber commented Mar 12, 2022

@unkcpz I took the start of your fix and fleshed it out a bit to fix the implementation. I then also added a fix on top to reinstate shell completion for relative filepaths. So I decided to make a separate PR, hope you don't mind.

@unkcpz
Copy link
Member

unkcpz commented Mar 12, 2022

@sphuber absolutely not mind 😄. I'll have a review and test the change later.

After your change, the ~ and . relative path is working for the verdi run but still not for all other param type, correct?

Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

@sphuber thanks for the fixes. Looks all good and super clear, only one minor thing can be added I think.

return False

# Allow characters that typically designate the start of a path.
return not value[0].isalnum() and value[0] not in ['/', '.', '~']
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return not value[0].isalnum() and value[0] not in ['/', '.', '~']
return not value[0].isalnum() and value[0] not in ['/', '.', '~', '$']

I think should also add to escape for $HOME and $PWD here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

sphuber added 2 commits March 14, 2022 14:36
When we moved to the built-in shell completion of `click==8.0` instead
of the external `click_completion`, the script argument of `verdi run`
was no longer auto-completed. The reason is that `click` does not
autocomplete files for the `STRING` type.

The parameter type is changed to `click.Path`, which not only fixes the
problem of auto-completion, it also provides automatic validation by
making sure if it is an existing file and not a directory. This allows
to simplify the logic in the command body significantly as well as we no
longer need to handle the `IOError` of unreadable files manually.
Since `click==8.0` started shipping with their own implementation of
shell completion, we started using that instead of `click_completion`.
However, `click`'s implementation currently does not auto-complete
relative filepaths that start, for example, with either `.`, `~` or `$`.

They made an exception for the `/` character, in the following PR:

    pallets/click#1930

Other users have noticed the problem with relative filepaths:

    pallets/click#2040

But it is not sure if they plan to support this anytime soon so we
simply monkeypatch `click` ourselves by changing the implementation for
the `click.shell_completion._start_of_option`.
@sphuber sphuber force-pushed the fix/verdi-run-shell-complete branch from c9e2ee2 to 1ad1d76 Compare March 14, 2022 13:36
@sphuber sphuber requested a review from unkcpz March 14, 2022 13:36
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

thanks! approved.

@sphuber sphuber merged commit 186b16f into aiidateam:develop Mar 14, 2022
@sphuber sphuber deleted the fix/verdi-run-shell-complete branch March 14, 2022 13:59
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.

2 participants