dart-cli-app-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart CLI Application Best Practices
Dart CLI应用最佳实践
1. When to use this skill
1. 何时使用此技能
Use this skill when:
- Creating a new Dart CLI application.
- Refactoring an existing CLI entrypoint ().
bin/ - Reviewing CLI code for quality and standards.
- Setting up executable scripts for Linux/Mac.
使用场景:
- 创建新的Dart CLI应用。
- 重构现有CLI入口点(目录)。
bin/ - 审查CLI代码的质量与标准合规性。
- 为Linux/Mac系统设置可执行脚本。
2. Best Practices
2. 最佳实践
Entrypoint Structure (bin/
)
bin/入口点结构(bin/
)
bin/Keep the contents of your entrypoint file (e.g., ) minimal. This improves testability by decoupling logic from the process runner.
bin/my_app.dartDO:
dart
// bin/my_app.dart
import 'package:my_app/src/entry_point.dart';
Future<void> main(List<String> arguments) async {
await runApp(arguments);
}DON'T:
- Put complex logic directly in .
bin/my_app.dart - Define classes or heavy functions in the entrypoint.
保持入口点文件(如)的内容尽可能精简。通过将逻辑与进程运行器解耦,提升可测试性。
bin/my_app.dart推荐做法:
dart
// bin/my_app.dart
import 'package:my_app/src/entry_point.dart';
Future<void> main(List<String> arguments) async {
await runApp(arguments);
}不推荐做法:
- 在中直接编写复杂逻辑。
bin/my_app.dart - 在入口点中定义类或复杂函数。
Executable Scripts
可执行脚本
For CLI tools intended to be run directly on Linux and Mac, add a shebang and ensure the file is executable.
DO:
- Add to the first line.
#!/usr/bin/env dart - Run to make it executable.
chmod +x bin/my_script.dart
dart
#!/usr/bin/env dart
void main() => print('Ready to run!');针对要在Linux和Mac上直接运行的CLI工具,添加shebang并确保文件具备可执行权限。
推荐做法:
- 在文件首行添加。
#!/usr/bin/env dart - 运行赋予可执行权限。
chmod +x bin/my_script.dart
dart
#!/usr/bin/env dart
void main() => print('Ready to run!');Process Termination (exitCode
)
exitCode进程终止(exitCode
)
exitCodeProperly handle process termination to allow for debugging and correct status reporting.
DO:
- Use the setter to report failure.
exitCode - Allow to complete naturally.
main - Use standard exit codes (sysexits) for clarity (e.g., for bad usage,
64for configuration errors).78- See
package:ioclass or FreeBSD sysexits man page.ExitCode
- See
dart
import 'dart:io';
void main() {
if (someFailure) {
exitCode = 64; // DO!
return;
}
}AVOID:
- Calling directly, as it terminates the VM immediately, preventing "pause on exit" debugging and
exit(code)blocks from running.finally
正确处理进程终止,以便于调试和状态报告。
推荐做法:
- 使用设置器报告失败状态。
exitCode - 让函数自然执行完成。
main - 使用标准退出码(sysexits)以确保清晰性(例如,表示使用错误,
64表示配置错误)。78- 可参考的
package:io类或FreeBSD的sysexits手册页。ExitCode
- 可参考
dart
import 'dart:io';
void main() {
if (someFailure) {
exitCode = 64; // 推荐做法!
return;
}
}避免:
- 直接调用,因为它会立即终止VM,导致“退出时暂停”调试功能失效,且
exit(code)代码块无法执行。finally
Exception Handling
异常处理
Uncaught exceptions automatically set a non-zero exit code, but you should handle expected errors gracefully.
Example:
dart
Future<void> main(List<String> arguments) async {
try {
await runApp(arguments);
} catch (e, stack) {
print('App crashed!');
print(e);
print(stack);
exitCode = 1; // Explicitly fail
}
}未捕获的异常会自动设置非零退出码,但你仍需优雅处理预期错误。
示例:
dart
Future<void> main(List<String> arguments) async {
try {
await runApp(arguments);
} catch (e, stack) {
print('App crashed!');
print(e);
print(stack);
exitCode = 1; // 显式标记失败
}
}3. Recommended Packages
3. 推荐包
Use these community-standard packages to solve common CLI problems:
| Category | Recommended Package | Usage |
|---|---|---|
| Stack Traces | | detailed, cleaner stack traces |
| Version Info | | automatic version injection |
| Arg Parsing | | standard flag/option parsing |
| CLI Generation | | generate arg parsers from classes |
| Configuration | | precise YAML parsing with line numbers |
| Configuration | | strongly typed config objects |
| Testing | | integration testing for CLI apps |
| Testing | | file system fixtures for tests |
| Networking | | standard HTTP client (remember user-agent!) |
使用这些社区标准包解决常见CLI问题:
| 分类 | 推荐包 | 用途 |
|---|---|---|
| 堆栈跟踪 | | 详细、更清晰的堆栈跟踪 |
| 版本信息 | | 自动注入版本信息 |
| 参数解析 | | 标准的标记/选项解析 |
| CLI生成 | | 从类生成参数解析器 |
| 配置处理 | | 带行号的精确YAML解析 |
| 配置处理 | | 强类型配置对象 |
| 测试 | | CLI应用的集成测试 |
| 测试 | | 测试用的文件系统夹具 |
| 网络请求 | | 标准HTTP客户端(记得设置User-Agent!) |
4. Conventions
4. 约定
- File Caching: Write cached files to .
.dart_tool/[pkg_name]/ - User-Agent: Always set a User-Agent header in HTTP requests, including version info.
- ANSI Output: Use for handling ANSI colors and styles.
package:io
- 文件缓存:将缓存文件写入目录。
.dart_tool/[pkg_name]/ - User-Agent:在HTTP请求中始终设置User-Agent头,包含版本信息。
- ANSI输出:使用处理ANSI颜色与样式。
package:io