-
Notifications
You must be signed in to change notification settings - Fork 204
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
CLI: fix shell completion of script for verdi run
#5430
Conversation
76a7fef
to
8849518
Compare
@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. |
@sphuber absolutely not mind 😄. I'll have a review and test the change later.
|
8849518
to
c9e2ee2
Compare
There was a problem hiding this 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.
aiida/cmdline/commands/cmd_verdi.py
Outdated
return False | ||
|
||
# Allow characters that typically designate the start of a path. | ||
return not value[0].isalnum() and value[0] not in ['/', '.', '~'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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`.
c9e2ee2
to
1ad1d76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! approved.
When we moved to the built-in shell completion of
click==8.0
insteadof the external
click_completion
, the script argument ofverdi run
was no longer auto-completed. The reason is that
click
does notautocomplete files for the
STRING
type.The parameter type is changed to
click.Path
, which not only fixes theproblem 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.