Bash tab completion for argparse
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Command-line utilities for registering and activating argcomplete completion for Python scripts. These tools help set up completion for individual scripts or enable global completion support.
After installing argcomplete, the following command-line utilities are available:
Register individual Python scripts for tab completion.
Usage:
register-python-argcomplete [options] executable [executable ...]Options:
--shell SHELL: Target shell (bash, zsh, fish, tcsh, powershell)--complete-arguments ARGS: Custom completion arguments--no-defaults: Disable default completion fallback--external-argcomplete-script SCRIPT: Use external completion scriptExamples:
# Basic registration (bash)
eval "$(register-python-argcomplete my-script.py)"
# Register for zsh
eval "$(register-python-argcomplete --shell zsh my-script.py)"
# Register for fish (save to file)
register-python-argcomplete --shell fish my-script.py > ~/.config/fish/completions/my-script.py.fish
# Register multiple scripts
eval "$(register-python-argcomplete my-script.py my-other-script.py)"
# Custom completion arguments
eval "$(register-python-argcomplete --complete-arguments '-o nospace' my-script.py)"Advanced Usage:
# Use external completion script
eval "$(register-python-argcomplete --external-argcomplete-script my-completion-handler my-script.py)"
# PowerShell registration
register-python-argcomplete --shell powershell my-script.py | Out-String | Invoke-ExpressionEnable global completion for all Python scripts that support argcomplete.
Usage:
activate-global-python-argcomplete [options]Options:
--dest DEST: Destination directory for completion files--shell SHELL: Target shell (bash, zsh)--user: Install for current user only--complete-arguments ARGS: Custom completion argumentsExamples:
# Enable global completion (requires sudo for system-wide)
sudo activate-global-python-argcomplete
# Enable for current user only
activate-global-python-argcomplete --user
# Enable for zsh
activate-global-python-argcomplete --shell zsh --user
# Custom destination
activate-global-python-argcomplete --dest ~/.bash_completion.d --userWhat it does:
# PYTHON_ARGCOMPLETE_OK commentVerify that easy_install scripts are properly set up for argcomplete.
Usage:
python-argcomplete-check-easy-install-script script-namePurpose:
# PYTHON_ARGCOMPLETE_OK marker is presentExample:
python-argcomplete-check-easy-install-script my-installed-toolFor scripts to work with argcomplete, they need:
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcompleteparser = argparse.ArgumentParser()
# Add arguments...
argcomplete.autocomplete(parser) # Must be before parse_args()
args = parser.parse_args()Check if completion is working:
# Test completion (should show available options)
my-script --<TAB><TAB>
# Debug completion
_ARC_DEBUG=1 my-script --<TAB># Add to ~/.bashrc or ~/.bash_profile
eval "$(register-python-argcomplete my-project-cli)"# System-wide (requires sudo)
sudo activate-global-python-argcomplete
# User-specific
activate-global-python-argcomplete --user# Add to ~/.zshrc
eval "$(register-python-argcomplete --shell zsh my-script)"# Enable global completion
activate-global-python-argcomplete --shell zsh --user
# Add to ~/.zshrc if not done automatically
fpath=(~/.local/share/zsh/site-functions $fpath)
autoload -U compinit && compinit# Save completion to fish config
register-python-argcomplete --shell fish my-script > ~/.config/fish/completions/my-script.fish
# Reload fish completions
fish -c "complete --erase; complete --reload"For distributable packages, include completion setup in installation:
setup.py example:
from setuptools import setup
setup(
name="my-package",
entry_points={
'console_scripts': [
'my-tool=my_package.cli:main',
],
},
# Optional: Include completion setup in post-install
)Post-install message:
print("""
To enable tab completion, run:
eval "$(register-python-argcomplete my-tool)"
Or add this line to your ~/.bashrc
""")For containerized applications:
# Install argcomplete
RUN pip install argcomplete
# Enable completion in container
RUN activate-global-python-argcomplete --user
# Set up environment
ENV PYTHONPATH=/app
ENV _ARC_DEBUG=1 # Optional: for debugging# Check if script has proper marker
head -n 5 my-script.py | grep -i argcomplete
# Verify registration
type _python_argcomplete_my_script# Enable debug output
_ARC_DEBUG=1 my-script --<TAB>
# Check environment variables
env | grep -i comp
env | grep -i argcomplete# Bash: Check completion loading
complete -p my-script
# Zsh: Check function definition
which _python_argcomplete_my_script
# Fish: List completions
complete -C my-scriptFor slow completion:
# Profile completion time
time (my-script --<TAB> 2>/dev/null)
# Enable debug to see what's slow
_ARC_DEBUG=1 my-script --<TAB>For global installation issues:
# Use user installation instead
activate-global-python-argcomplete --user
# Check destination permissions
ls -la /etc/bash_completion.d/
ls -la ~/.bash_completion.d/Install with Tessl CLI
npx tessl i tessl/pypi-argcomplete