Debian Codegraph @

docs/source/shell_completion.rst

raw
1 ================
2 Shell Completion
3 ================
4
5 Cyclopts provides shell completion (tab completion) for bash, zsh, and fish shells.
6
7 Development & Standalone Scripts
8 ==================================
9
10 Shell completion systems (bash, zsh, fish) can only provide completion for **installed commands** (executables in your ``$PATH``), not for arbitrary Python scripts like ``python myapp.py``. This is a fundamental limitation of how shells work.
11
12 To work around this during development, Cyclopts provides a ``cyclopts run`` command that acts as a wrapper:
13
14 .. code-block:: console
15
16 $ cyclopts run myapp.py --help
17 $ cyclopts run myapp.py:app --verbose
18
19 Since ``cyclopts`` itself is an installed command, the shell can provide completion for it. The ``cyclopts run`` command then loads and executes your script, giving you completion for your development scripts without needing to package and install them.
20
21 **Script Path Format:**
22
23 - ``cyclopts run script.py`` - Auto-detects the App object. If an App object cannot be determined, it will raise an error.
24 - ``cyclopts run script.py:app`` - Explicitly specifies the App object to run
25
26 This is particularly useful during development before packaging your application.
27
28 **Virtual Environment Behavior:**
29
30 ``cyclopts run`` imports your script directly into the **same Python process** (no subprocess is created). This means:
31
32 - It uses whatever Python interpreter is currently running ``cyclopts``
33 - Your script has access to all packages installed in the current environment
34 - You must install ``cyclopts`` in your project's virtual environment
35 - To use: activate your venv, then run ``cyclopts run script.py``
36
37 .. code-block:: console
38
39 $ source .venv/bin/activate # or your venv activation method
40 $ cyclopts run myapp.py
41
42 .. note::
43 Completion for your script's commands comes through the ``cyclopts`` CLI completion.
44 Install it once with: ``cyclopts --install-completion``
45
46 .. warning::
47 **Performance:** ``cyclopts run`` uses **dynamic completion**, which imports your script and calls Python on **every tab press**. This can be slow if your script has heavy imports.
48
49 To mitigate slow imports during development, consider using :ref:`Lazy Loading` for your commands. For production or frequent use, install **static completion** using the methods below. Static completion is pre-generated and does not call Python, making it instantaneous.
50
51 To install completion specifically for your standalone script (without using ``cyclopts run``), you can use the Manual Installation approach below with your script's App object.
52
53 Installation
54 ============
55
56 Programmatic Installation (Recommended)
57 ----------------------------------------
58
59 Add completion installation to your CLI application using :meth:`App.register_install_completion_command <cyclopts.App.register_install_completion_command>`:
60
61 .. code-block:: python
62
63 from cyclopts import App
64
65 app = App(name="myapp")
66 app.register_install_completion_command()
67
68 # Your commands here...
69
70 if __name__ == "__main__":
71 app()
72
73 Users can then install completion by running:
74
75 .. code-block:: console
76
77 myapp --install-completion
78
79 Manual Installation
80 -------------------
81
82 For programmatic control, use :meth:`App.install_completion <cyclopts.App.install_completion>` directly:
83
84 .. code-block:: python
85
86 from cyclopts import App
87 from pathlib import Path
88
89 app = App(name="myapp")
90
91 # Install for current shell
92 install_path = app.install_completion()
93 print(f"Installed completion to {install_path}")
94
95 # Install for specific shell
96 install_path = app.install_completion(shell="zsh")
97
98 # Install to custom location
99 install_path = app.install_completion(
100 shell="bash",
101 output=Path("/custom/path/completion.sh"),
102 )
103
104 Default Installation Paths
105 ---------------------------
106
107 - **Zsh**: ``~/.zsh/completions/_cyclopts_<app_name>``
108 - **Bash**: ``~/.local/share/bash-completion/completions/<app_name>``
109 - **Fish**: ``~/.config/fish/completions/<app_name>.fish``
110
111 Script Generation
112 =================
113
114 To generate a completion script without installing it, use :meth:`App.generate_completion <cyclopts.App.generate_completion>`:
115
116 .. code-block:: python
117
118 from cyclopts import App
119
120 app = App(name="myapp")
121 script = app.generate_completion(shell="zsh")
122 print(script)
123
124 Shell Configuration
125 ===================
126
127 By default, Cyclopts modifies your shell RC file to enable completion:
128
129 - **Zsh**: Adds to ``~/.zshrc``
130 - **Bash**: Adds to ``~/.bashrc``
131 - **Fish**: No modification needed (automatically loads from ``~/.config/fish/completions/``)
132
133 After installation, restart your shell or source the RC file.
134
135 To install without modifying shell RC files, use:
136
137 .. code-block:: python
138
139 app.register_install_completion_command(add_to_startup=False)