olofk / fusesoc

Package manager and build abstraction tool for FPGA/ASIC development
BSD 2-Clause "Simplified" License
1.17k stars 242 forks source link

Add better bash_completion with the argcomplete package #672

Open nicorum opened 7 months ago

nicorum commented 7 months ago

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 commented 6 months ago

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.

nicorum commented 5 months ago

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

nicorum commented 2 weeks ago

Small update: It seems that adding PYTHON_ARGCOMPLETE_OK to the binary generated by setuptools is not necessary. The argcomplete package automatically searches the script that is called by this binary. I originally tested this under Ubuntu with python 3.8 where it didn't work, but under fedora with python 3.12 it works. So this pull request should work with newer python versions as it is. For older versions there is always the option to add eval "$(register-python-argcomplete fusesoc)" to the bash_profile etc.