norwegianblue icon indicating copy to clipboard operation
norwegianblue copied to clipboard

Autocomplete after the eol command

Open romainx opened this issue 1 year ago • 13 comments
trafficstars

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 🍰

romainx avatar Jul 26 '24 06:07 romainx

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?

hugovk avatar Jul 26 '24 07:07 hugovk

Hello

Is my-python-app correct here?

Sorry no 😓 , it should be replaced by eol. Fixed in the PR description.

romainx avatar Jul 26 '24 07:07 romainx

Thanks, so does the eval need to be run just once, or once per new shell or something?

hugovk avatar Jul 26 '24 07:07 hugovk

@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.

romainx avatar Jul 26 '24 08:07 romainx

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?

hugovk avatar Jul 26 '24 08:07 hugovk

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_OK in 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

romainx avatar Jul 26 '24 09:07 romainx

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>

hugovk avatar Jul 27 '24 11:07 hugovk

Sorry for that. The script python-argcomplete is located on my side at:

  • /usr/local/share/zsh/site-functions/_python-argcomplete for zsh
  • /usr/local/etc/bash_completion.d/python-argcomplete for bash

Seems that on your side it's installed at the following locations:

  • /opt/homebrew/share/zsh/site-functions/_python-argcomplete for zsh
  • /opt/homebrew/etc/bash_completion.d/python-argcomplete for bash

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

romainx avatar Jul 27 '24 14:07 romainx

Sorry for that. The script python-argcomplete is located on my side at:

  • /usr/local/share/zsh/site-functions/_python-argcomplete for zsh
  • /usr/local/etc/bash_completion.d/python-argcomplete for bash

Seems that on your side it's installed at the following locations:

  • /opt/homebrew/share/zsh/site-functions/_python-argcomplete for zsh
  • /opt/homebrew/etc/bash_completion.d/python-argcomplete for bash

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.

hugovk avatar Jul 27 '24 14:07 hugovk

I don't know why it is not sourced automatically by your shell 🤔?

romainx avatar Jul 27 '24 14:07 romainx

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?

hugovk avatar Jul 28 '24 10:07 hugovk

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 $fpath variable. You can add a directory to $fpath by adding a line like this to your ~/.zshrc file:

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

romainx avatar Jul 29 '24 06:07 romainx

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  

Yes, it's the first (and second) entry in my $fpath.

hugovk avatar Aug 05 '24 06:08 hugovk

Could we get this merged in? would be super helpful for speeding things up in the CLI.

ozskywalker avatar Sep 22 '25 17:09 ozskywalker

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.

codecov[bot] avatar Oct 13 '25 14:10 codecov[bot]

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?

hugovk avatar Oct 13 '25 15:10 hugovk