code-abyss 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +197 -0
- package/bin/install.js +193 -0
- package/bin/uninstall.js +42 -0
- package/config/AGENTS.md +247 -0
- package/config/CLAUDE.md +207 -0
- package/config/settings.example.json +27 -0
- package/output-styles/abyss-cultivator.md +399 -0
- package/package.json +41 -0
- package/skills/SKILL.md +115 -0
- package/skills/ai/SKILL.md +29 -0
- package/skills/ai/agent-dev.md +242 -0
- package/skills/ai/llm-security.md +288 -0
- package/skills/architecture/SKILL.md +41 -0
- package/skills/architecture/api-design.md +225 -0
- package/skills/architecture/caching.md +299 -0
- package/skills/architecture/cloud-native.md +285 -0
- package/skills/architecture/compliance.md +299 -0
- package/skills/architecture/data-security.md +184 -0
- package/skills/architecture/message-queue.md +329 -0
- package/skills/architecture/security-arch.md +210 -0
- package/skills/development/SKILL.md +43 -0
- package/skills/development/cpp.md +246 -0
- package/skills/development/go.md +323 -0
- package/skills/development/java.md +277 -0
- package/skills/development/python.md +288 -0
- package/skills/development/rust.md +313 -0
- package/skills/development/shell.md +313 -0
- package/skills/development/typescript.md +277 -0
- package/skills/devops/SKILL.md +36 -0
- package/skills/devops/cost-optimization.md +272 -0
- package/skills/devops/database.md +217 -0
- package/skills/devops/devsecops.md +198 -0
- package/skills/devops/git-workflow.md +181 -0
- package/skills/devops/observability.md +280 -0
- package/skills/devops/performance.md +273 -0
- package/skills/devops/testing.md +186 -0
- package/skills/gen-docs/SKILL.md +114 -0
- package/skills/gen-docs/scripts/doc_generator.py +491 -0
- package/skills/multi-agent/SKILL.md +268 -0
- package/skills/run_skill.py +88 -0
- package/skills/security/SKILL.md +51 -0
- package/skills/security/blue-team.md +379 -0
- package/skills/security/code-audit.md +265 -0
- package/skills/security/pentest.md +226 -0
- package/skills/security/red-team.md +321 -0
- package/skills/security/threat-intel.md +322 -0
- package/skills/security/vuln-research.md +369 -0
- package/skills/tests/README.md +225 -0
- package/skills/tests/SUMMARY.md +362 -0
- package/skills/tests/__init__.py +3 -0
- package/skills/tests/test_change_analyzer.py +558 -0
- package/skills/tests/test_doc_generator.py +538 -0
- package/skills/tests/test_module_scanner.py +376 -0
- package/skills/tests/test_quality_checker.py +516 -0
- package/skills/tests/test_security_scanner.py +426 -0
- package/skills/verify-change/SKILL.md +138 -0
- package/skills/verify-change/scripts/change_analyzer.py +529 -0
- package/skills/verify-module/SKILL.md +125 -0
- package/skills/verify-module/scripts/module_scanner.py +321 -0
- package/skills/verify-quality/SKILL.md +158 -0
- package/skills/verify-quality/scripts/quality_checker.py +481 -0
- package/skills/verify-security/SKILL.md +141 -0
- package/skills/verify-security/scripts/security_scanner.py +368 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: java
|
|
3
|
+
description: Java 开发。企业级应用、Spring Boot、微服务。当用户提到 Java、Spring、Maven、Gradle、JVM 时使用。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 📜 符箓秘典 · Java
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Spring Boot
|
|
10
|
+
|
|
11
|
+
### 基础项目
|
|
12
|
+
```java
|
|
13
|
+
@SpringBootApplication
|
|
14
|
+
public class Application {
|
|
15
|
+
public static void main(String[] args) {
|
|
16
|
+
SpringApplication.run(Application.class, args);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Controller
|
|
21
|
+
@RestController
|
|
22
|
+
@RequestMapping("/api/users")
|
|
23
|
+
public class UserController {
|
|
24
|
+
|
|
25
|
+
@Autowired
|
|
26
|
+
private UserService userService;
|
|
27
|
+
|
|
28
|
+
@GetMapping("/{id}")
|
|
29
|
+
public ResponseEntity<User> getUser(@PathVariable Long id) {
|
|
30
|
+
return userService.findById(id)
|
|
31
|
+
.map(ResponseEntity::ok)
|
|
32
|
+
.orElse(ResponseEntity.notFound().build());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@PostMapping
|
|
36
|
+
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO dto) {
|
|
37
|
+
User user = userService.create(dto);
|
|
38
|
+
return ResponseEntity.status(HttpStatus.CREATED).body(user);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Service
|
|
43
|
+
@Service
|
|
44
|
+
public class UserService {
|
|
45
|
+
|
|
46
|
+
@Autowired
|
|
47
|
+
private UserRepository userRepository;
|
|
48
|
+
|
|
49
|
+
public Optional<User> findById(Long id) {
|
|
50
|
+
return userRepository.findById(id);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Transactional
|
|
54
|
+
public User create(UserDTO dto) {
|
|
55
|
+
User user = new User();
|
|
56
|
+
user.setName(dto.getName());
|
|
57
|
+
user.setEmail(dto.getEmail());
|
|
58
|
+
return userRepository.save(user);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Repository
|
|
63
|
+
@Repository
|
|
64
|
+
public interface UserRepository extends JpaRepository<User, Long> {
|
|
65
|
+
Optional<User> findByEmail(String email);
|
|
66
|
+
List<User> findByNameContaining(String name);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Entity
|
|
71
|
+
```java
|
|
72
|
+
@Entity
|
|
73
|
+
@Table(name = "users")
|
|
74
|
+
@Data
|
|
75
|
+
@NoArgsConstructor
|
|
76
|
+
@AllArgsConstructor
|
|
77
|
+
public class User {
|
|
78
|
+
|
|
79
|
+
@Id
|
|
80
|
+
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
81
|
+
private Long id;
|
|
82
|
+
|
|
83
|
+
@Column(nullable = false)
|
|
84
|
+
private String name;
|
|
85
|
+
|
|
86
|
+
@Column(unique = true, nullable = false)
|
|
87
|
+
private String email;
|
|
88
|
+
|
|
89
|
+
@CreatedDate
|
|
90
|
+
private LocalDateTime createdAt;
|
|
91
|
+
|
|
92
|
+
@LastModifiedDate
|
|
93
|
+
private LocalDateTime updatedAt;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// DTO
|
|
97
|
+
@Data
|
|
98
|
+
public class UserDTO {
|
|
99
|
+
@NotBlank(message = "Name is required")
|
|
100
|
+
private String name;
|
|
101
|
+
|
|
102
|
+
@Email(message = "Invalid email")
|
|
103
|
+
@NotBlank(message = "Email is required")
|
|
104
|
+
private String email;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 配置
|
|
109
|
+
```yaml
|
|
110
|
+
# application.yml
|
|
111
|
+
spring:
|
|
112
|
+
datasource:
|
|
113
|
+
url: jdbc:postgresql://localhost:5432/mydb
|
|
114
|
+
username: ${DB_USER}
|
|
115
|
+
password: ${DB_PASS}
|
|
116
|
+
jpa:
|
|
117
|
+
hibernate:
|
|
118
|
+
ddl-auto: update
|
|
119
|
+
show-sql: true
|
|
120
|
+
|
|
121
|
+
server:
|
|
122
|
+
port: 8080
|
|
123
|
+
|
|
124
|
+
logging:
|
|
125
|
+
level:
|
|
126
|
+
root: INFO
|
|
127
|
+
com.myapp: DEBUG
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## 异常处理
|
|
131
|
+
|
|
132
|
+
```java
|
|
133
|
+
@RestControllerAdvice
|
|
134
|
+
public class GlobalExceptionHandler {
|
|
135
|
+
|
|
136
|
+
@ExceptionHandler(ResourceNotFoundException.class)
|
|
137
|
+
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
|
|
138
|
+
ErrorResponse error = new ErrorResponse(
|
|
139
|
+
HttpStatus.NOT_FOUND.value(),
|
|
140
|
+
ex.getMessage()
|
|
141
|
+
);
|
|
142
|
+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
146
|
+
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
|
|
147
|
+
String message = ex.getBindingResult().getFieldErrors().stream()
|
|
148
|
+
.map(FieldError::getDefaultMessage)
|
|
149
|
+
.collect(Collectors.joining(", "));
|
|
150
|
+
ErrorResponse error = new ErrorResponse(
|
|
151
|
+
HttpStatus.BAD_REQUEST.value(),
|
|
152
|
+
message
|
|
153
|
+
);
|
|
154
|
+
return ResponseEntity.badRequest().body(error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 自定义异常
|
|
159
|
+
public class ResourceNotFoundException extends RuntimeException {
|
|
160
|
+
public ResourceNotFoundException(String message) {
|
|
161
|
+
super(message);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## 测试
|
|
167
|
+
|
|
168
|
+
```java
|
|
169
|
+
@SpringBootTest
|
|
170
|
+
@AutoConfigureMockMvc
|
|
171
|
+
class UserControllerTest {
|
|
172
|
+
|
|
173
|
+
@Autowired
|
|
174
|
+
private MockMvc mockMvc;
|
|
175
|
+
|
|
176
|
+
@MockBean
|
|
177
|
+
private UserService userService;
|
|
178
|
+
|
|
179
|
+
@Test
|
|
180
|
+
void shouldGetUser() throws Exception {
|
|
181
|
+
User user = new User(1L, "Alice", "alice@example.com");
|
|
182
|
+
when(userService.findById(1L)).thenReturn(Optional.of(user));
|
|
183
|
+
|
|
184
|
+
mockMvc.perform(get("/api/users/1"))
|
|
185
|
+
.andExpect(status().isOk())
|
|
186
|
+
.andExpect(jsonPath("$.name").value("Alice"));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
@Test
|
|
190
|
+
void shouldCreateUser() throws Exception {
|
|
191
|
+
UserDTO dto = new UserDTO();
|
|
192
|
+
dto.setName("Bob");
|
|
193
|
+
dto.setEmail("bob@example.com");
|
|
194
|
+
|
|
195
|
+
User user = new User(1L, "Bob", "bob@example.com");
|
|
196
|
+
when(userService.create(any())).thenReturn(user);
|
|
197
|
+
|
|
198
|
+
mockMvc.perform(post("/api/users")
|
|
199
|
+
.contentType(MediaType.APPLICATION_JSON)
|
|
200
|
+
.content(objectMapper.writeValueAsString(dto)))
|
|
201
|
+
.andExpect(status().isCreated())
|
|
202
|
+
.andExpect(jsonPath("$.name").value("Bob"));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 单元测试
|
|
207
|
+
@ExtendWith(MockitoExtension.class)
|
|
208
|
+
class UserServiceTest {
|
|
209
|
+
|
|
210
|
+
@Mock
|
|
211
|
+
private UserRepository userRepository;
|
|
212
|
+
|
|
213
|
+
@InjectMocks
|
|
214
|
+
private UserService userService;
|
|
215
|
+
|
|
216
|
+
@Test
|
|
217
|
+
void shouldFindById() {
|
|
218
|
+
User user = new User(1L, "Alice", "alice@example.com");
|
|
219
|
+
when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
|
220
|
+
|
|
221
|
+
Optional<User> result = userService.findById(1L);
|
|
222
|
+
|
|
223
|
+
assertTrue(result.isPresent());
|
|
224
|
+
assertEquals("Alice", result.get().getName());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## 项目结构
|
|
230
|
+
|
|
231
|
+
```
|
|
232
|
+
myproject/
|
|
233
|
+
├── pom.xml / build.gradle
|
|
234
|
+
├── src/
|
|
235
|
+
│ ├── main/
|
|
236
|
+
│ │ ├── java/
|
|
237
|
+
│ │ │ └── com/myapp/
|
|
238
|
+
│ │ │ ├── Application.java
|
|
239
|
+
│ │ │ ├── controller/
|
|
240
|
+
│ │ │ ├── service/
|
|
241
|
+
│ │ │ ├── repository/
|
|
242
|
+
│ │ │ ├── model/
|
|
243
|
+
│ │ │ ├── dto/
|
|
244
|
+
│ │ │ └── config/
|
|
245
|
+
│ │ └── resources/
|
|
246
|
+
│ │ └── application.yml
|
|
247
|
+
│ └── test/
|
|
248
|
+
│ └── java/
|
|
249
|
+
└── target/
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## 常用依赖
|
|
253
|
+
|
|
254
|
+
```xml
|
|
255
|
+
<!-- pom.xml -->
|
|
256
|
+
<dependencies>
|
|
257
|
+
<dependency>
|
|
258
|
+
<groupId>org.springframework.boot</groupId>
|
|
259
|
+
<artifactId>spring-boot-starter-web</artifactId>
|
|
260
|
+
</dependency>
|
|
261
|
+
<dependency>
|
|
262
|
+
<groupId>org.springframework.boot</groupId>
|
|
263
|
+
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
264
|
+
</dependency>
|
|
265
|
+
<dependency>
|
|
266
|
+
<groupId>org.springframework.boot</groupId>
|
|
267
|
+
<artifactId>spring-boot-starter-validation</artifactId>
|
|
268
|
+
</dependency>
|
|
269
|
+
<dependency>
|
|
270
|
+
<groupId>org.projectlombok</groupId>
|
|
271
|
+
<artifactId>lombok</artifactId>
|
|
272
|
+
</dependency>
|
|
273
|
+
</dependencies>
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python
|
|
3
|
+
description: Python 开发。Web框架、数据处理、自动化脚本、测试。当用户提到 Python、Django、Flask、FastAPI、pytest、pandas 时使用。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 📜 符箓秘典 · Python
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Web 框架
|
|
10
|
+
|
|
11
|
+
### FastAPI (推荐)
|
|
12
|
+
```python
|
|
13
|
+
from fastapi import FastAPI, HTTPException, Depends
|
|
14
|
+
from pydantic import BaseModel
|
|
15
|
+
from typing import Optional
|
|
16
|
+
|
|
17
|
+
app = FastAPI()
|
|
18
|
+
|
|
19
|
+
class User(BaseModel):
|
|
20
|
+
name: str
|
|
21
|
+
email: str
|
|
22
|
+
age: Optional[int] = None
|
|
23
|
+
|
|
24
|
+
@app.get("/users/{user_id}")
|
|
25
|
+
async def get_user(user_id: int):
|
|
26
|
+
return {"user_id": user_id}
|
|
27
|
+
|
|
28
|
+
@app.post("/users")
|
|
29
|
+
async def create_user(user: User):
|
|
30
|
+
return user
|
|
31
|
+
|
|
32
|
+
# 依赖注入
|
|
33
|
+
async def get_db():
|
|
34
|
+
db = Database()
|
|
35
|
+
try:
|
|
36
|
+
yield db
|
|
37
|
+
finally:
|
|
38
|
+
await db.close()
|
|
39
|
+
|
|
40
|
+
@app.get("/items")
|
|
41
|
+
async def get_items(db = Depends(get_db)):
|
|
42
|
+
return await db.fetch_all("SELECT * FROM items")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Flask
|
|
46
|
+
```python
|
|
47
|
+
from flask import Flask, request, jsonify
|
|
48
|
+
|
|
49
|
+
app = Flask(__name__)
|
|
50
|
+
|
|
51
|
+
@app.route('/api/users', methods=['GET', 'POST'])
|
|
52
|
+
def users():
|
|
53
|
+
if request.method == 'POST':
|
|
54
|
+
data = request.json
|
|
55
|
+
return jsonify(data), 201
|
|
56
|
+
return jsonify([])
|
|
57
|
+
|
|
58
|
+
@app.errorhandler(404)
|
|
59
|
+
def not_found(e):
|
|
60
|
+
return jsonify(error="Not found"), 404
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Django
|
|
64
|
+
```python
|
|
65
|
+
# models.py
|
|
66
|
+
from django.db import models
|
|
67
|
+
|
|
68
|
+
class User(models.Model):
|
|
69
|
+
name = models.CharField(max_length=100)
|
|
70
|
+
email = models.EmailField(unique=True)
|
|
71
|
+
created_at = models.DateTimeField(auto_now_add=True)
|
|
72
|
+
|
|
73
|
+
# views.py
|
|
74
|
+
from django.http import JsonResponse
|
|
75
|
+
from django.views import View
|
|
76
|
+
|
|
77
|
+
class UserView(View):
|
|
78
|
+
def get(self, request, user_id):
|
|
79
|
+
user = User.objects.get(id=user_id)
|
|
80
|
+
return JsonResponse({'name': user.name})
|
|
81
|
+
|
|
82
|
+
# urls.py
|
|
83
|
+
urlpatterns = [
|
|
84
|
+
path('users/<int:user_id>/', UserView.as_view()),
|
|
85
|
+
]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 异步编程
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import asyncio
|
|
92
|
+
import aiohttp
|
|
93
|
+
|
|
94
|
+
async def fetch(url: str) -> str:
|
|
95
|
+
async with aiohttp.ClientSession() as session:
|
|
96
|
+
async with session.get(url) as response:
|
|
97
|
+
return await response.text()
|
|
98
|
+
|
|
99
|
+
async def fetch_all(urls: list[str]) -> list[str]:
|
|
100
|
+
tasks = [fetch(url) for url in urls]
|
|
101
|
+
return await asyncio.gather(*tasks)
|
|
102
|
+
|
|
103
|
+
# 运行
|
|
104
|
+
asyncio.run(fetch_all(['http://example.com', 'http://example.org']))
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 数据处理
|
|
108
|
+
|
|
109
|
+
### Pandas
|
|
110
|
+
```python
|
|
111
|
+
import pandas as pd
|
|
112
|
+
|
|
113
|
+
# 读取数据
|
|
114
|
+
df = pd.read_csv('data.csv')
|
|
115
|
+
df = pd.read_json('data.json')
|
|
116
|
+
|
|
117
|
+
# 数据清洗
|
|
118
|
+
df = df.dropna()
|
|
119
|
+
df = df.drop_duplicates()
|
|
120
|
+
df['column'] = df['column'].str.strip()
|
|
121
|
+
|
|
122
|
+
# 数据转换
|
|
123
|
+
df['date'] = pd.to_datetime(df['date'])
|
|
124
|
+
df['category'] = df['category'].astype('category')
|
|
125
|
+
|
|
126
|
+
# 聚合分析
|
|
127
|
+
result = df.groupby('category').agg({
|
|
128
|
+
'value': ['sum', 'mean', 'count']
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
# 导出
|
|
132
|
+
df.to_csv('output.csv', index=False)
|
|
133
|
+
df.to_json('output.json', orient='records')
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 测试
|
|
137
|
+
|
|
138
|
+
### pytest
|
|
139
|
+
```python
|
|
140
|
+
import pytest
|
|
141
|
+
from myapp import calculate, UserService
|
|
142
|
+
|
|
143
|
+
# 基础测试
|
|
144
|
+
def test_add():
|
|
145
|
+
assert calculate.add(1, 2) == 3
|
|
146
|
+
|
|
147
|
+
# 参数化
|
|
148
|
+
@pytest.mark.parametrize("a,b,expected", [
|
|
149
|
+
(1, 2, 3),
|
|
150
|
+
(0, 0, 0),
|
|
151
|
+
(-1, 1, 0),
|
|
152
|
+
])
|
|
153
|
+
def test_add_params(a, b, expected):
|
|
154
|
+
assert calculate.add(a, b) == expected
|
|
155
|
+
|
|
156
|
+
# Fixture
|
|
157
|
+
@pytest.fixture
|
|
158
|
+
def user_service():
|
|
159
|
+
service = UserService()
|
|
160
|
+
yield service
|
|
161
|
+
service.cleanup()
|
|
162
|
+
|
|
163
|
+
def test_create_user(user_service):
|
|
164
|
+
user = user_service.create("test")
|
|
165
|
+
assert user.name == "test"
|
|
166
|
+
|
|
167
|
+
# Mock
|
|
168
|
+
from unittest.mock import Mock, patch
|
|
169
|
+
|
|
170
|
+
@patch('myapp.requests.get')
|
|
171
|
+
def test_fetch(mock_get):
|
|
172
|
+
mock_get.return_value.json.return_value = {"id": 1}
|
|
173
|
+
result = fetch_user(1)
|
|
174
|
+
assert result["id"] == 1
|
|
175
|
+
|
|
176
|
+
# 异步测试
|
|
177
|
+
@pytest.mark.asyncio
|
|
178
|
+
async def test_async_fetch():
|
|
179
|
+
result = await async_fetch()
|
|
180
|
+
assert result is not None
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 运行测试
|
|
184
|
+
```bash
|
|
185
|
+
pytest # 运行所有
|
|
186
|
+
pytest test_file.py # 指定文件
|
|
187
|
+
pytest -k "test_add" # 匹配名称
|
|
188
|
+
pytest -v # 详细输出
|
|
189
|
+
pytest --cov=myapp # 覆盖率
|
|
190
|
+
pytest -x # 失败即停
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## CLI 工具
|
|
194
|
+
|
|
195
|
+
### Typer (推荐)
|
|
196
|
+
```python
|
|
197
|
+
import typer
|
|
198
|
+
|
|
199
|
+
app = typer.Typer()
|
|
200
|
+
|
|
201
|
+
@app.command()
|
|
202
|
+
def hello(name: str, count: int = 1):
|
|
203
|
+
"""Say hello NAME, COUNT times."""
|
|
204
|
+
for _ in range(count):
|
|
205
|
+
typer.echo(f"Hello {name}!")
|
|
206
|
+
|
|
207
|
+
@app.command()
|
|
208
|
+
def goodbye(name: str, formal: bool = False):
|
|
209
|
+
if formal:
|
|
210
|
+
typer.echo(f"Goodbye Ms. {name}. Have a good day.")
|
|
211
|
+
else:
|
|
212
|
+
typer.echo(f"Bye {name}!")
|
|
213
|
+
|
|
214
|
+
if __name__ == "__main__":
|
|
215
|
+
app()
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### argparse
|
|
219
|
+
```python
|
|
220
|
+
import argparse
|
|
221
|
+
|
|
222
|
+
parser = argparse.ArgumentParser(description='My CLI tool')
|
|
223
|
+
parser.add_argument('input', help='Input file')
|
|
224
|
+
parser.add_argument('-o', '--output', default='output.txt')
|
|
225
|
+
parser.add_argument('-v', '--verbose', action='store_true')
|
|
226
|
+
|
|
227
|
+
args = parser.parse_args()
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## 项目结构
|
|
231
|
+
|
|
232
|
+
```
|
|
233
|
+
myproject/
|
|
234
|
+
├── pyproject.toml # 项目配置
|
|
235
|
+
├── README.md
|
|
236
|
+
├── src/
|
|
237
|
+
│ └── myproject/
|
|
238
|
+
│ ├── __init__.py
|
|
239
|
+
│ ├── main.py
|
|
240
|
+
│ ├── models.py
|
|
241
|
+
│ └── utils.py
|
|
242
|
+
├── tests/
|
|
243
|
+
│ ├── __init__.py
|
|
244
|
+
│ ├── conftest.py
|
|
245
|
+
│ └── test_main.py
|
|
246
|
+
└── scripts/
|
|
247
|
+
└── run.py
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### pyproject.toml
|
|
251
|
+
```toml
|
|
252
|
+
[project]
|
|
253
|
+
name = "myproject"
|
|
254
|
+
version = "0.1.0"
|
|
255
|
+
dependencies = [
|
|
256
|
+
"fastapi>=0.100.0",
|
|
257
|
+
"uvicorn>=0.23.0",
|
|
258
|
+
]
|
|
259
|
+
|
|
260
|
+
[project.optional-dependencies]
|
|
261
|
+
dev = [
|
|
262
|
+
"pytest>=7.0.0",
|
|
263
|
+
"pytest-cov>=4.0.0",
|
|
264
|
+
]
|
|
265
|
+
|
|
266
|
+
[tool.pytest.ini_options]
|
|
267
|
+
testpaths = ["tests"]
|
|
268
|
+
|
|
269
|
+
[tool.ruff]
|
|
270
|
+
line-length = 120
|
|
271
|
+
select = ["E", "F", "I"]
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## 常用库
|
|
275
|
+
|
|
276
|
+
| 库 | 用途 |
|
|
277
|
+
|---|------|
|
|
278
|
+
| requests/httpx | HTTP 客户端 |
|
|
279
|
+
| aiohttp | 异步 HTTP |
|
|
280
|
+
| SQLAlchemy | ORM |
|
|
281
|
+
| Pydantic | 数据验证 |
|
|
282
|
+
| Click/Typer | CLI |
|
|
283
|
+
| pytest | 测试 |
|
|
284
|
+
| pandas | 数据处理 |
|
|
285
|
+
| loguru | 日志 |
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|