0
# Shell Completion
1
2
Fire provides functionality for generating shell completion scripts that enable tab completion for Fire-generated CLIs in Bash and Fish shells.
3
4
## Capabilities
5
6
### Completion Script Generation
7
8
Generate shell completion scripts that provide tab completion for commands, subcommands, and options.
9
10
```python { .api }
11
def Script(name, component, default_options=None, shell='bash'):
12
"""
13
Generate shell completion script for a Fire CLI.
14
15
Parameters:
16
- name: Name of the CLI command for completion registration
17
- component: Component to analyze for completion possibilities
18
- default_options: Dictionary of default options available for any command
19
- shell: Shell type, either 'bash' or 'fish'
20
21
Returns:
22
String containing the shell completion script to be sourced
23
"""
24
```
25
26
### Member Visibility
27
28
Utilities for determining which members of a component should be visible in completion suggestions.
29
30
```python { .api }
31
def MemberVisible(component, name, member, class_attrs=None, verbose=False):
32
"""
33
Check if a member should be visible in completion.
34
35
Parameters:
36
- component: The component containing the member
37
- name: Name of the member
38
- member: The member object
39
- class_attrs: Dictionary of class attributes if applicable
40
- verbose: Whether to include private/hidden members
41
42
Returns:
43
Boolean indicating if member should be visible
44
"""
45
46
def VisibleMembers(component, class_attrs=None, verbose=False):
47
"""
48
Get all visible members of a component for completion.
49
50
Parameters:
51
- component: Component to get members from
52
- class_attrs: Dictionary of class attributes if applicable
53
- verbose: Whether to include private/hidden members
54
55
Returns:
56
Dictionary of visible member names to member objects
57
"""
58
```
59
60
### Completion Suggestions
61
62
Generate completion suggestions for a given component context.
63
64
```python { .api }
65
def Completions(component, verbose=False):
66
"""
67
Get completion suggestions for a component.
68
69
Parameters:
70
- component: Component to get completions for
71
- verbose: Whether to include private/hidden members
72
73
Returns:
74
List of completion suggestions (strings)
75
"""
76
```
77
78
## Usage Examples
79
80
**Generate Bash completion script:**
81
```python
82
import fire
83
from fire import completion
84
85
class MyTool:
86
def process(self, file, output=None):
87
return f"Processing {file}"
88
89
def analyze(self, data, verbose=False):
90
return f"Analyzing {data}"
91
92
# Generate completion script
93
script = completion.Script('mytool', MyTool, shell='bash')
94
print(script)
95
```
96
97
**Generate Fish completion script:**
98
```python
99
import fire
100
from fire import completion
101
102
def deploy(environment, version, force=False):
103
return f"Deploying {version} to {environment}"
104
105
def status(service):
106
return f"Status of {service}"
107
108
commands = {
109
'deploy': deploy,
110
'status': status
111
}
112
113
# Generate Fish completion
114
script = completion.Script('deploy-tool', commands, shell='fish')
115
print(script)
116
```
117
118
**Using Fire's built-in completion flag:**
119
```python
120
# Any Fire CLI automatically supports completion generation
121
import fire
122
123
class Calculator:
124
def add(self, x, y):
125
return x + y
126
127
if __name__ == '__main__':
128
fire.Fire(Calculator)
129
# Command line: python calc.py -- --completion
130
# Outputs Bash completion script to stdout
131
132
# Command line: python calc.py -- --completion fish
133
# Outputs Fish completion script to stdout
134
```
135
136
## Setting Up Completion
137
138
**For Bash:**
139
```bash
140
# Generate and save completion script
141
python your_cli.py -- --completion > /etc/bash_completion.d/your_cli
142
143
# Or for user-specific completion
144
python your_cli.py -- --completion > ~/.bash_completion.d/your_cli
145
source ~/.bash_completion.d/your_cli
146
```
147
148
**For Fish:**
149
```bash
150
# Generate and save completion script
151
python your_cli.py -- --completion fish > ~/.config/fish/completions/your_cli.fish
152
```
153
154
**Programmatic setup:**
155
```python
156
import fire
157
from fire import completion
158
159
class MyApp:
160
def install_completion(self, shell='bash'):
161
"""Install shell completion for this CLI."""
162
script = completion.Script('myapp', MyApp, shell=shell)
163
164
if shell == 'bash':
165
with open('/etc/bash_completion.d/myapp', 'w') as f:
166
f.write(script)
167
elif shell == 'fish':
168
import os
169
os.makedirs(os.path.expanduser('~/.config/fish/completions'), exist_ok=True)
170
with open(os.path.expanduser('~/.config/fish/completions/myapp.fish'), 'w') as f:
171
f.write(script)
172
173
return f"Completion installed for {shell}"
174
175
if __name__ == '__main__':
176
fire.Fire(MyApp)
177
```
178
179
The completion system analyzes the component structure to provide intelligent suggestions for:
180
- Available commands and subcommands
181
- Function and method names
182
- Class constructors and properties
183
- Dictionary keys
184
- List indices
185
- Default option flags (`--help`, `--verbose`, etc.)