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 better bash_completion with the argcomplete package #672

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nicorum
Copy link

@nicorum nicorum commented Feb 21, 2024

The current bash_completion script is very basic and the completion breaks after entering the first letter of a core name.
Since fusesoc uses the argparse package for argument parsing, we can use the argcomplete package for bash_completion.

Most of the completion is done automatically by argcomplete using the argparse parser. So there is no need to replicate argument/subparser names in a separate bash_completion file.
I also implemented completion for cores, generators and tools for the specific arguments.

Completion for bash/zsh can be enabled either globally or on a per-command basis.
Global completion can be enabled with the activate-global-python-argcomplete command. Additionally, the fusesoc binary needs the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes. I would have added this feature to the binary, but i can't find it in the repository. So it is not part of this pull request.
To enable completion for the fusesoc binary only, use the eval "$(register-python-argcomplete fusesoc)" command.
Completion can also be used unofficially with other shells (e.g. powershell).

The disadvantage of this approach is that it adds the argcomplete package as a dependency. However, the argcomplete package seems to be well maintained and is part of the repository of many Linux distributions.

@olofk
Copy link
Owner

olofk commented Mar 7, 2024

Fantastic! The binary is magically autogenerated by setuptools somehow. Not sure how to inject additional code directly into that, but everything is possible with Python :) Let me look into that.

completes argument names, sub_parser names and available cores/tools/generators

argcomplete completion can be enabled by two ways:
1. Global:
    can be enabled with the 'activate-global-python-argcomplete' command
    additionaly the fusesoc binary needs the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes, this needs to be added to the pip binary to work
    this only works for zsh and bash
2: Per command:
    for zsh and bash:
        eval "$(register-python-argcomplete fusesoc)"
    for other shells (e.g. powershell):
        https://github.com/kislyuk/argcomplete/tree/develop/contrib
@nicorum
Copy link
Author

nicorum commented Apr 11, 2024

I noticed that I had forgotten to use the pre-commit tool. The branch is now updated with the correctly formatted code.

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.

None yet

2 participants