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

Shell completion doesn't work on scripts with a dot in name #2166

Closed
ziima opened this issue Jan 4, 2022 · 5 comments · Fixed by #2275
Closed

Shell completion doesn't work on scripts with a dot in name #2166

ziima opened this issue Jan 4, 2022 · 5 comments · Fixed by #2275
Assignees
Milestone

Comments

@ziima
Copy link

ziima commented Jan 4, 2022

I found out, that a shell completion doesn't work on scripts with a dot in their name, mainly the scripts themselves (without an entry point linked) or entry points with a dot in their name (less common). The main problem is that the dot is copied into the name of the completion variable.

MWE:
Let's have a script /tmp/example.py

import click


@click.command()
def main():
    pass


if __name__ == '__main__':
    main()

Completion on this script doesn't do anything using any of reasonable variable names:

_EXAMPLE_COMPLETE=bash_source python3 /tmp/example.py
_EXAMPLEPY_COMPLETE=bash_source python3 /tmp/example.py
_EXAMPLE_PY_COMPLETE=bash_source python3 /tmp/example.py

I found out, click requires the variable _EXAMPLE.PY_COMPLETE to be set, which is not possible (at least) in bash.

It would help if the dot was replaced by an underscore, the same way a dash is handled.

Environment:

  • Python version: 3.9.9
  • Click version: 8.0.3
@davidism
Copy link
Member

davidism commented May 3, 2022

Completion in general will not work with filenames (./example.py) or modules (python -m example), I don't believe that's something Click can control.

While I'd discorage anyone from making an entry point script with a dot in the name (example.hello), there shouldn't be anything technically wrong with that, so it makes sense to support that.

The solution might be to normalize dots the same way we normalize dashes, by replacing them with underscores.

@nicksspirit
Copy link
Contributor

I'll be working on this

@nicksspirit
Copy link
Contributor

Hey @ziima,

This PR #2275 should fix the issue you raised. It is important you setup an entry point in your setup.py or equivalent with the console script that has the dot in the name. Given your /tmp/example.py an example setup.py should look like:

from setuptools import setup

setup(
    name='issue2166', # Your package name
    version='0.1.0',
    py_modules=['example'], # your example.py module
    entry_points={
        'console_scripts': [
            'example.hello = example:main',
        ],
    },
)

See click's documentation on setup tools

@ziima
Copy link
Author

ziima commented May 9, 2022

@OdinTech3 Nice, I looking forward for the change.

I have the entry_points set up, but they're hard to use in development or ad hoc scripts 🤷

@davidism
Copy link
Member

davidism commented May 9, 2022

There shouldn't be anything different between dev and prod regarding entry points. pip install -e . will set up any defined entry points just as pip install . would. To be clear, the PR does not make it possible to complete with ./myscript.py or python -m myscript, that's an inability of the shell, not of Click. The only thing it changes is allow a dot in the entry point name.

@davidism davidism added this to the 8.1.4 milestone Jun 30, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants