0
# Core CLI Generation
1
2
The main Fire function provides the core functionality for automatically generating command-line interfaces from any Python object. It handles argument parsing, component traversal, function calling, and output formatting.
3
4
## Capabilities
5
6
### Main Fire Function
7
8
The primary entry point that transforms Python objects into CLIs through recursive component traversal and argument consumption.
9
10
```python { .api }
11
def Fire(component=None, command=None, name=None, serialize=None):
12
"""
13
Create a CLI from any Python object.
14
15
Executes a command either from the command argument or from sys.argv by
16
recursively traversing the target object's members, consuming arguments,
17
evaluating functions, and instantiating classes.
18
19
Parameters:
20
- component: The initial target component (function, class, module, object, dict, list, etc.)
21
- command: Optional command to execute (string or list of strings)
22
- name: Optional name of the command for interactive mode and completion
23
- serialize: Optional callable to serialize all objects to text
24
25
Returns:
26
Result of executing the Fire command
27
28
Raises:
29
- ValueError: If command argument is not a string or list
30
- FireError: For Fire-specific errors during execution
31
- FireExit: For controlled exit conditions
32
"""
33
```
34
35
### Usage Examples
36
37
**Basic Function CLI:**
38
```python
39
import fire
40
41
def greet(name, greeting="Hello"):
42
return f"{greeting}, {name}!"
43
44
if __name__ == '__main__':
45
fire.Fire(greet)
46
# Command line: python script.py John --greeting=Hi
47
# Output: Hi, John!
48
```
49
50
**Class-based CLI:**
51
```python
52
import fire
53
54
class Calculator:
55
def add(self, x, y):
56
return x + y
57
58
def multiply(self, x, y):
59
return x * y
60
61
if __name__ == '__main__':
62
fire.Fire(Calculator)
63
# Command line: python script.py add 5 3
64
# Output: 8
65
```
66
67
**Module CLI:**
68
```python
69
import fire
70
import math
71
72
if __name__ == '__main__':
73
fire.Fire(math)
74
# Command line: python script.py sqrt 16
75
# Output: 4.0
76
```
77
78
**Dictionary CLI:**
79
```python
80
import fire
81
82
def task1():
83
return "Task 1 completed"
84
85
def task2():
86
return "Task 2 completed"
87
88
commands = {
89
'task1': task1,
90
'task2': task2
91
}
92
93
if __name__ == '__main__':
94
fire.Fire(commands)
95
# Command line: python script.py task1
96
# Output: Task 1 completed
97
```
98
99
### Core Utility Functions
100
101
Helper functions used internally by Fire for output display and completion script generation.
102
103
```python { .api }
104
def Display(lines, out):
105
"""
106
Display lines to output stream.
107
108
Parameters:
109
- lines: Iterable of strings to display
110
- out: Output stream (file-like object)
111
"""
112
113
def CompletionScript(name, component, shell):
114
"""
115
Generate completion script for a Fire CLI (wrapper function).
116
117
Parameters:
118
- name: Name of the CLI command
119
- component: Component to generate completion for
120
- shell: Shell type ('bash' or 'fish')
121
122
Returns:
123
String containing the completion script
124
"""
125
```
126
127
## Fire Flags
128
129
Fire CLIs support universal flags that work with any Fire-generated CLI:
130
131
- `--help` or `-h`: Show help information
132
- `--interactive` or `-i`: Drop into interactive Python REPL after running command
133
- `--verbose` or `-v`: Include private members in help and usage information
134
- `--completion`: Generate Bash completion script to stdout
135
- `--completion fish`: Generate Fish completion script to stdout
136
- `--separator SEPARATOR`: Use custom separator instead of default '-'
137
- `--trace`: Show Fire execution trace for the command
138
139
These flags must be separated from component arguments with `--`:
140
```bash
141
python script.py command arg1 arg2 -- --help --verbose
142
```