0
# Core Application Functions
1
2
Main application entry points, command processing, and execution functionality. These functions provide the foundation for command correction and user interaction in the thefuck application.
3
4
## Capabilities
5
6
### Main Entry Points
7
8
The primary CLI entry points that handle argument parsing and dispatch to appropriate functionality.
9
10
```python { .api }
11
def main():
12
"""
13
Main CLI entry point with argument parsing.
14
15
Handles command-line arguments including:
16
- --version: Show version information
17
- --alias: Print shell alias configuration
18
- command arguments: Process command for correction
19
20
Returns:
21
None
22
"""
23
24
def fix_command():
25
"""
26
Main entry point for command correction functionality.
27
28
Sets up user directory, loads settings, processes the command from
29
sys.argv, generates corrections, and handles user interaction.
30
31
Returns:
32
None
33
"""
34
35
def print_alias(entry_point=True):
36
"""
37
Prints shell alias configuration for thefuck integration.
38
39
Parameters:
40
- entry_point (bool): Whether called from deprecated thefuck-alias entry point
41
42
Returns:
43
None
44
45
Prints shell-specific alias command to stdout.
46
"""
47
```
48
49
### User Directory Management
50
51
Functions for managing the user's thefuck configuration directory and initialization.
52
53
```python { .api }
54
def setup_user_dir():
55
"""
56
Returns user config dir, creates it when it doesn't exist.
57
58
Creates ~/.thefuck directory structure including:
59
- Main configuration directory
60
- Rules subdirectory for custom rules
61
- Settings file initialization
62
63
Returns:
64
pathlib.Path: Path to user configuration directory
65
"""
66
```
67
68
### Command Processing
69
70
Core command execution and processing functionality.
71
72
```python { .api }
73
def get_command(settings, args):
74
"""
75
Creates command from args and executes it.
76
77
Parameters:
78
- settings (Settings): Application settings
79
- args (list): Command line arguments (typically sys.argv)
80
81
Returns:
82
Command: Command object with script, stdout, stderr or None if empty command
83
84
Executes the command with proper environment and timeout handling.
85
"""
86
87
def wait_output(settings, popen):
88
"""
89
Returns True if we can get output of the command in the settings.wait_command time.
90
91
Parameters:
92
- settings (Settings): Application settings containing wait_command timeout
93
- popen (subprocess.Popen): Process to wait for
94
95
Returns:
96
bool: True if command completed within timeout, False if killed due to timeout
97
98
Command will be killed if it wasn't finished in time.
99
"""
100
101
def run_command(old_cmd, command, settings):
102
"""
103
Runs command from rule for passed command.
104
105
Parameters:
106
- old_cmd (Command): Original failed command
107
- command (CorrectedCommand): Corrected command to execute
108
- settings (Settings): Application settings
109
110
Returns:
111
None
112
113
Executes side effects, adds to shell history, and prints the command.
114
"""
115
```
116
117
## Usage Examples
118
119
### Basic Command Processing
120
121
```python
122
import sys
123
from thefuck.main import setup_user_dir, get_command, fix_command
124
from thefuck.conf import get_settings
125
126
# Setup user environment
127
user_dir = setup_user_dir()
128
settings = get_settings(user_dir)
129
130
# Process a command
131
command = get_command(settings, ['thefuck', 'git', 'status'])
132
if command:
133
print(f"Executed: {command.script}")
134
print(f"Output: {command.stdout}")
135
print(f"Errors: {command.stderr}")
136
```
137
138
### Shell Alias Generation
139
140
```python
141
from thefuck.main import print_alias
142
import sys
143
144
# Simulate thefuck --alias call
145
sys.argv = ['thefuck', '--alias', 'f']
146
print_alias(False) # Prints shell-specific alias configuration
147
```
148
149
### Complete Correction Flow
150
151
```python
152
from thefuck.main import setup_user_dir, get_command, run_command
153
from thefuck.conf import get_settings
154
from thefuck.corrector import get_corrected_commands
155
from thefuck.ui import select_command
156
157
# Setup
158
user_dir = setup_user_dir()
159
settings = get_settings(user_dir)
160
161
# Process failed command
162
args = ['thefuck', 'git', 'pussh', 'origin', 'main']
163
command = get_command(settings, args)
164
165
if command:
166
# Get corrections
167
corrected_commands = get_corrected_commands(command, user_dir, settings)
168
169
# Select correction (interactive)
170
selected = select_command(corrected_commands, settings)
171
172
if selected:
173
# Execute the correction
174
run_command(command, selected, settings)
175
```
176
177
## Error Handling
178
179
Functions handle various error conditions:
180
181
- **Empty commands**: `get_command` returns None for empty input
182
- **Command timeouts**: `wait_output` kills processes that exceed time limits
183
- **Process failures**: Commands that fail to execute properly are handled gracefully
184
- **User interruption**: Keyboard interrupts during execution are caught and handled
185
186
## Environment Integration
187
188
The core application functions integrate with the system environment:
189
190
- **Shell detection**: Automatically detects and integrates with current shell
191
- **Environment variables**: Respects thefuck-specific environment settings
192
- **History integration**: Adds corrected commands to shell history
193
- **Working directory**: Maintains proper working directory context