Bash tab completion for argparse
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for generating shell-specific completion code that integrates argcomplete with various shell environments. The shell integration system generates the necessary shell code to bridge between the shell's completion system and Python-based completion logic.
Generate shell-specific completion code for different shell environments.
def shellcode(
executables: List[str],
use_defaults: bool = True,
shell: str = "bash",
complete_arguments: Optional[List[str]] = None,
argcomplete_script: Optional[str] = None
) -> strParameters:
executables: List of executable names to enable completion foruse_defaults: Whether to fallback to readline's default completion (bash only)shell: Target shell ("bash", "zsh", "fish", "tcsh", "powershell")complete_arguments: Custom arguments for the complete command (bash only)argcomplete_script: Alternative script to call for completionReturns:
Usage:
from argcomplete import shellcode
# Generate bash completion code
bash_code = shellcode(['my-script', 'my-other-script'], shell='bash')
print(bash_code)
# Generate zsh completion code
zsh_code = shellcode(['my-script'], shell='zsh')
# Generate fish completion code
fish_code = shellcode(['my-script'], shell='fish')
# Disable default completions
code = shellcode(['my-script'], use_defaults=False)
# Custom complete arguments
code = shellcode(['my-script'], complete_arguments=['-o', 'nospace'])Generates bash completion code that integrates with the bash completion system:
# Example generated bash code
_python_argcomplete_my_script() {
local IFS=$'\013'
COMPREPLY=($(IFS="$IFS" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
_ARGCOMPLETE=1 \
_ARGCOMPLETE_SHELL="bash" \
__python_argcomplete_run my-script))
}
complete -o nospace -o default -o bashdefault -F _python_argcomplete_my_script my-scriptFeatures:
COMP_WORDBREAKS customizationGenerates zsh completion code using the _describe function:
# Example generated zsh code
_python_argcomplete_my_script() {
local completions
completions=($(IFS="$IFS" \
COMP_LINE="$BUFFER" \
COMP_POINT="$CURSOR" \
_ARGCOMPLETE=1 \
_ARGCOMPLETE_SHELL="zsh" \
__python_argcomplete_run my-script))
_describe "my-script" completions
}
compdef _python_argcomplete_my_script my-scriptFeatures:
_describe for rich completions with descriptionsGenerates fish shell completion code:
# Example generated fish code
function __fish_my_script_complete
set -x _ARGCOMPLETE 1
set -x _ARGCOMPLETE_SHELL fish
set -x COMP_LINE (commandline -p)
set -x COMP_POINT (string length (commandline -cp))
my-script 8>&1 9>&2 1>/dev/null 2>&1
end
complete --command my-script -f -a '(__fish_my_script_complete)'Features:
Generates PowerShell completion code using Register-ArgumentCompleter:
# Example generated PowerShell code
Register-ArgumentCompleter -Native -CommandName my-script -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SHELL = "powershell"
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
my-script 2>&1 | Out-Null | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_)
}
}Generates tcsh completion code:
# Example generated tcsh code
complete "my-script" 'p@*@`python-argcomplete-tcsh "my-script"`@' ;Generate completion for multiple related scripts:
code = shellcode([
'my-tool',
'my-tool-admin',
'my-tool-dev'
], shell='bash')Use a different script for completion than the executable being completed:
# All executables use the same completion script
code = shellcode(
['my-tool', 'my-tool-legacy'],
argcomplete_script='my-tool-completion'
)# Bash
eval "$(python -c "from argcomplete import shellcode; print(shellcode(['my-script']))")"
# Or save to file
python -c "from argcomplete import shellcode; print(shellcode(['my-script']))" > my-script-completion.bash
source my-script-completion.bash# Add to .zshrc
eval "$(python -c "from argcomplete import shellcode; print(shellcode(['my-script'], shell='zsh'))")"# Save to completion file
python -c "from argcomplete import shellcode; print(shellcode(['my-script'], shell='fish'))" > ~/.config/fish/completions/my-script.fishThe shell integration relies on specific environment variables during completion:
_ARGCOMPLETE: Set to indicate completion mode_ARGCOMPLETE_SHELL: Target shell typeCOMP_LINE: Full command line being completedCOMP_POINT: Cursor position in the command line_ARGCOMPLETE_SUPPRESS_SPACE: Control space appending_ARGCOMPLETE_IFS: Internal field separator for completion output_ARGCOMPLETE_DFS: Description field separator_ARGCOMPLETE_COMP_WORDBREAKS: Word break characters_ARC_DEBUG: Enable debug output_ARGCOMPLETE_STDOUT_FILENAME: Redirect completion output to fileARGCOMPLETE_USE_TEMPFILES: Use temporary files for IPCEnable debug output to troubleshoot completion issues:
export _ARC_DEBUG=1
my-script <TAB> # Debug output will show in terminalThe debug output includes:
Install with Tessl CLI
npx tessl i tessl/pypi-argcomplete