norwegianblue
norwegianblue copied to clipboard
Autocomplete after the eol command
Hello,
This is a draft to resolve #125. As the project is using argparse, I thought it was a good idea to use argcomplete. This implementation is very simple but it seems to do the trick.
# register eol with the shell's completion framework by running register-python-argcomplete:
eval "$(register-python-argcomplete eol)"
# use it
eol ne
nextcloud nextjs netbsd nexus neo4j neos --
Cheers 🍰
Thanks for this!
# register eol with the shell's completion framework by running register-python-argcomplete: eval "$(register-python-argcomplete my-python-app)" # use it eol ne nextcloud nextjs netbsd nexus neo4j neos --
Is my-python-app correct here?
Hello
Is
my-python-appcorrect here?
Sorry no 😓 , it should be replaced by eol. Fixed in the PR description.
Thanks, so does the eval need to be run just once, or once per new shell or something?
@hugovk the use of eval is for one time activation (for the current shell), there are several options that are nicely described in the pytest documentation for bash completion.
I pushed a commit to support global completion.
The eval works for me, but I've run activate-global-python-argcomplete (on macOS with iterm2):
❯ activate-global-python-argcomplete
Defaulting to system-wide installation.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/share/zsh/site-functions/_python-argcomplete...
Installed.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/etc/bash_completion.d/python-argcomplete...
Installed.
Please restart your shell or source the installed file to activate it.
And opened a new shell, but it's not completing:
❯ eol ne
Do you know why?
Works on my side on new shell but only after I added the flag # PYTHON_ARGCOMPLETE_OK in the code (cli.py). Have you pulled this commit to perform your test?
Instead, the shell will look for the string
PYTHON_ARGCOMPLETE_OKin the first 1024 bytes of any executable that it's running completion for, and if it's found, follow the rest of the argcomplete protocol as described above. --source
Yes:
❯ head --lines 2 src/norwegianblue/cli.py
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
❯ pip install -e . --quiet
❯ eol ne<tab>
New terminal tab:
❯ eol ne<tab>
Sorry for that. The script python-argcomplete is located on my side at:
/usr/local/share/zsh/site-functions/_python-argcompleteforzsh/usr/local/etc/bash_completion.d/python-argcompleteforbash
Seems that on your side it's installed at the following locations:
/opt/homebrew/share/zsh/site-functions/_python-argcompleteforzsh/opt/homebrew/etc/bash_completion.d/python-argcompleteforbash
Could you please try to source the one corresponding to your shell to check if it exists and if it solves the problem?
source /opt/homebrew/share/zsh/site-functions/_python-argcomplete
Sorry for that. The script
python-argcompleteis located on my side at:
/usr/local/share/zsh/site-functions/_python-argcompleteforzsh/usr/local/etc/bash_completion.d/python-argcompleteforbashSeems that on your side it's installed at the following locations:
/opt/homebrew/share/zsh/site-functions/_python-argcompleteforzsh/opt/homebrew/etc/bash_completion.d/python-argcompleteforbash
That's right.
Could you please try to source the one corresponding to your shell to check if it exists and if it solves the problem?
source /opt/homebrew/share/zsh/site-functions/_python-argcomplete
Yes, that works.
I don't know why it is not sourced automatically by your shell 🤔?
I've no idea...
python3.13 is the default interpreter on this machine, I also have:
❯ locate _python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/opt/homebrew/Cellar/pipx/1.6.0/libexec/lib/python3.12/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/opt/homebrew/share/zsh/site-functions/_python-argcomplete
activate-global-python-argcomplete on my path is for 3.13:
❯ which activate-global-python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.13/bin/activate-global-python-argcomplete
But when I run it, it installs from 3.13 to a Homebrew path:
❯ activate-global-python-argcomplete
Defaulting to system-wide installation.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/share/zsh/site-functions/_python-argcomplete...
Installed.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/etc/bash_completion.d/python-argcomplete...
Installed.
Please restart your shell or source the installed file to activate it.
I don't use Homebrew Python for anything other than letting Homebrew manage it for its own use, so it feels like a bug that the 3.13 activate-global-python-argcomplete installs completion for another interpreter, let alone the Homebrew one.
Anyway, it also works if I source that 3.13 one in a new tab. What do you suggest? Should I add manual sourcing of that to my ~/.zshrc or something?
zsh
From this helper.
Completion functions for commands are stored in files with names beginning with an underscore
_, and these files should be placed in a directory listed in the$fpathvariable. You can add a directory to$fpathby adding a line like this to your~/.zshrcfile:
So you can check if your fpath variable contains the location of the autocomplete script installation.
echo $fpath | grep /opt/homebrew/share/zsh/site-functions
If it's not the case you could add a line in your ~/.zshrc file.
fpath=(/opt/homebrew/share/zsh/site-functions $fpath)
bash
Based on my understanding of this answer.
A .bash_completion script should perform this kind of source.
for bcfile in ~/.bash_completion.d/* ; do
[ -f "$bcfile" ] && . $bcfile
done
So you can check if your
fpathvariable contains the location of the autocomplete script installation.echo $fpath | grep /opt/homebrew/share/zsh/site-functions
Yes, it's the first (and second) entry in my $fpath.
Could we get this merged in? would be super helpful for speeding things up in the CLI.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 98.61%. Comparing base (7baf0e3) to head (964197d).
:warning: Report is 1 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #216 +/- ##
=======================================
Coverage 98.61% 98.61%
=======================================
Files 7 7
Lines 432 433 +1
=======================================
+ Hits 426 427 +1
Misses 6 6
| Flag | Coverage Δ | |
|---|---|---|
| macos-latest | 98.61% <100.00%> (+<0.01%) |
:arrow_up: |
| ubuntu-latest | 98.61% <100.00%> (+<0.01%) |
:arrow_up: |
| windows-latest | ? |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
I've resolved the conflict, and tested again, it now works for me! 🎉
❯ eval "$(register-python-argcomplete eol)"
❯ eol p<tab>
pci-dss panos pangp
phoenix-framework photon perl
phpmyadmin phpbb php
pixel-watch pigeonhole pixel
plone plesk pnpm
postfix pop-os podman
postmarketos powershell postgresql
prometheus privatebin proftpd
proxmox-ve protractor puppet
python --
I'll get this merged and released soon, hopefully this week.
Would someone like to suggest some text to stick in the README about this?