Argcomplete

raw JSON →
3.6.3 verified Tue May 12 auth: no python install: verified quickstart: verified

Argcomplete provides robust and extensible tab completion for command-line interfaces built with Python's `argparse` library. It enhances user experience by offering dynamic suggestions for arguments, options, and their values in shells like Bash and Zsh. The library is actively maintained with frequent releases, often including compatibility fixes for newer Python versions and shell environments.

pip install argcomplete
error ModuleNotFoundError: No module named 'argcomplete'
cause The 'argcomplete' package is not installed in the current Python environment.
fix
Install the package using pip: 'pip install argcomplete'.
error AttributeError: 'Namespace' object has no attribute 'x'
cause Attempting to access a command-line argument that was not defined in the ArgumentParser.
fix
Ensure the argument is added to the parser: 'parser.add_argument('--x', ...)' and access it as 'args.x'.
error AttributeError: 'Namespace' object has no attribute 'accumulate'
cause Accessing an attribute that was not defined due to missing or incorrect argument definitions.
fix
Define the argument with the correct 'dest' parameter: 'parser.add_argument('--sum', dest='accumulate', ...)' and access it as 'args.accumulate'.
error AttributeError: 'Namespace' object has no attribute 'check'
cause Using a different attribute name than specified in the 'dest' parameter of add_argument.
fix
Access the attribute using the name specified in 'dest': 'args.performance' if 'parser.add_argument('--check', dest='performance', ...)' was used.
error AttributeError: 'Namespace' object has no attribute 'command'
cause Accessing a subcommand argument without setting the 'dest' parameter in add_subparsers.
fix
Set the 'dest' parameter when adding subparsers: 'subparsers = parser.add_subparsers(dest='command')' and access it as 'args.command'.
gotcha The `# PYTHON_ARGCOMPLETE_OK` marker must be present within the first 1024 bytes of the script file for global completion to detect it. Incorrect placement can lead to completion not working.
fix Ensure '# PYTHON_ARGCOMPLETE_OK' is near the top of your script file, ideally on the second line after the shebang.
gotcha Heavy computations or side effects executed before `argcomplete.autocomplete(parser)` in your script will run every time tab completion is attempted. This can make completion sluggish and unresponsive, negatively impacting user experience.
fix Structure your script to initialize the `ArgumentParser` and call `argcomplete.autocomplete()` as early as possible in the execution flow. Defer expensive operations until after `parser.parse_args()`.
breaking Argcomplete requires Bash 4.2+ for global completion. Older Bash versions (e.g., Bash 3.2 on macOS by default) do not support the `complete -D` functionality needed, leading to global completion failures.
fix Upgrade Bash to version 4.2 or newer, or use Zsh which has native support. Alternatively, register each script individually using `eval "$(register-python-argcomplete my-script.py)"`.
breaking Specific argcomplete versions were required to maintain compatibility with changes in Python's `argparse` module, particularly with Python 3.11.9+, 3.12.3+, 3.12.8+, and 3.13.1+. Failing to update could result in completion errors or broken argument parsing.
fix Always use the latest stable version of `argcomplete` to ensure compatibility with the most recent Python releases. Check release notes for specific compatibility updates.
gotcha If your script tries to read from `stdin` during completion, it can cause the shell to lock up, as `argcomplete` typically redirects `stdin` during completion generation. This was explicitly addressed in v3.4.0.
fix Upgrade to argcomplete 3.4.0 or newer. Ensure your completion logic (code executed before `parse_args()` when `argcomplete` is active) does not attempt to read from `stdin`.
gotcha After debugging completion issues or if an older completion function was registered, the shell might retain a broken completion. This can manifest as `_minimal` being used or no completion at all.
fix Restart your shell, or run `complete -r my-python-app` to remove a specific completion, or `complete -r -D` to remove the default completion (if set by `activate-global-python-argcomplete`). Setting `_ARC_DEBUG=1` can help diagnose issues.
gotcha Running `pip` as the 'root' user, especially within containerized or automated testing environments, can lead to package permission issues, conflicts with system package managers, and an unstable environment. While not directly an `argcomplete` bug, such `pip` warnings signal an unrecommended setup that could indirectly impact `argcomplete`'s installation, dependencies, or overall script execution.
fix Always prefer using virtual environments (e.g., `venv`) for `pip` operations to isolate dependencies and avoid system conflicts. If a virtual environment is not an option, ensure `pip` commands are executed by a non-root user. Additionally, keep `pip` updated (`pip install --upgrade pip`) to benefit from the latest features and bug fixes.
activate-global-python-argcomplete
eval "$(register-python-argcomplete my-script.py)"
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.03s 18.1M
3.10 alpine (musl) - - 0.03s 18.1M
3.10 slim (glibc) wheel 1.5s 0.02s 19M
3.10 slim (glibc) - - 0.02s 19M
3.11 alpine (musl) wheel - 0.05s 19.9M
3.11 alpine (musl) - - 0.05s 19.9M
3.11 slim (glibc) wheel 1.6s 0.04s 20M
3.11 slim (glibc) - - 0.04s 20M
3.12 alpine (musl) wheel - 0.04s 11.8M
3.12 alpine (musl) - - 0.04s 11.8M
3.12 slim (glibc) wheel 1.5s 0.04s 12M
3.12 slim (glibc) - - 0.04s 12M
3.13 alpine (musl) wheel - 0.03s 11.5M
3.13 alpine (musl) - - 0.04s 11.4M
3.13 slim (glibc) wheel 1.6s 0.03s 12M
3.13 slim (glibc) - - 0.03s 12M
3.9 alpine (musl) wheel - 0.02s 17.6M
3.9 alpine (musl) - - 0.03s 17.6M
3.9 slim (glibc) wheel 2.1s 0.02s 18M
3.9 slim (glibc) - - 0.02s 18M

Create a Python script (e.g., `my_cli.py`) with the `#!` shebang and the `# PYTHON_ARGCOMPLETE_OK` marker. Initialize your `ArgumentParser`, add arguments, and then call `argcomplete.autocomplete(parser)` before `parser.parse_args()`. To activate completion for this script in your shell, run `eval "$(register-python-argcomplete my_cli.py)"`. For global activation across all argcomplete-enabled scripts, run `activate-global-python-argcomplete` once.

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcomplete

def main():
    parser = argparse.ArgumentParser(description='A simple CLI tool with completion.')
    parser.add_argument('--name', help='Your name').completer = argcomplete.completers.ChoicesCompleter(['Alice', 'Bob', 'Charlie'])
    parser.add_argument('--action', choices=['start', 'stop', 'restart'], help='Action to perform')

    # Enable argcomplete on the parser
    argcomplete.autocomplete(parser)

    args = parser.parse_args()

    if args.name:
        print(f"Hello, {args.name}!")
    if args.action:
        print(f"Performing action: {args.action}")

if __name__ == '__main__':
    main()