dart-cli-app-best-practices by kevmoo/dash_skills
npx skills add https://github.com/kevmoo/dash_skills --skill dart-cli-app-best-practices在以下情况下使用此技能:
bin/) 时。bin/)保持入口点文件(例如 bin/my_app.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 中。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
对于旨在直接在 Linux 和 Mac 上运行的 CLI 工具,请添加 shebang 并确保文件可执行。
推荐做法:
#!/usr/bin/env dart。chmod +x bin/my_script.dart 使其可执行。#!/usr/bin/env dart
void main() => print('Ready to run!');
exitCode)正确处理进程终止,以便进行调试和正确的状态报告。
推荐做法:
使用 exitCode 设置器来报告失败。
允许 main 函数自然完成。
为清晰起见使用标准退出码(sysexits)(例如,64 表示用法错误,78 表示配置错误)。
package:io 中的 ExitCode 类或 FreeBSD sysexits 手册页。import 'dart:io';
void main() { if (someFailure) { exitCode = 64; // 推荐做法! return; } }
避免做法:
exit(code),因为它会立即终止 VM,从而阻止“退出时暂停”调试和 finally 块的运行。未捕获的异常会自动设置非零退出码,但您应该优雅地处理预期的错误。
示例:
Future<void> main(List<String> arguments) async {
try {
await runApp(arguments);
} catch (e, stack) {
print('App crashed!');
print(e);
print(stack);
exitCode = 1; // 明确地标记为失败
}
}
使用这些由 Dart 团队 维护的社区标准包来解决常见的 CLI 问题:
| 类别 | 推荐包 | 用途 |
|---|---|---|
| 堆栈跟踪 | package:stack_trace | 详细、更清晰的堆栈跟踪 |
| 参数解析 | package:args | 标准的标志/选项解析 |
| 测试 | package:test_process | CLI 应用的集成测试 |
| 测试 | package:test_descriptor | 用于测试的文件系统夹具 |
| 网络 | package:http | 标准 HTTP 客户端(记得设置用户代理!) |
| ANSI 输出 | package:io | 处理 ANSI 颜色和样式 |
| 类别 | 推荐包 | 用途 |
|---|---|---|
| 配置 | package:json_serializable | 强类型配置对象 |
| CLI 生成 | package:build_cli | 从类生成参数解析器 |
| 版本信息 | package:build_version | 自动版本注入 |
| 配置 | package:checked_yaml | 带行号的精确 YAML 解析 |
.dart_tool/[pkg_name]/。每周安装次数
73
代码仓库
GitHub 星标数
119
首次出现
2026年2月6日
安全审计
安装于
antigravity57
github-copilot25
codex25
opencode25
gemini-cli24
amp23
Use this skill when:
bin/).bin/)Keep the contents of your entrypoint file (e.g., bin/my_app.dart) minimal. This improves testability by decoupling logic from the process runner.
DO:
// bin/my_app.dart
import 'package:my_app/src/entry_point.dart';
Future<void> main(List<String> arguments) async {
await runApp(arguments);
}
DON'T:
bin/my_app.dart.For CLI tools intended to be run directly on Linux and Mac, add a shebang and ensure the file is executable.
DO:
#!/usr/bin/env dart to the first line.chmod +x bin/my_script.dart to make it executable.#!/usr/bin/env dart
void main() => print('Ready to run!');
exitCode)Properly handle process termination to allow for debugging and correct status reporting.
DO:
Use the exitCode setter to report failure.
Allow main to complete naturally.
Use standard exit codes (sysexits) for clarity (e.g., 64 for bad usage, 78 for configuration errors).
package:io ExitCode class or FreeBSD sysexits man page.import 'dart:io';
void main() { if (someFailure) { exitCode = 64; // DO! return; } }
AVOID:
exit(code) directly, as it terminates the VM immediately, preventing "pause on exit" debugging and finally blocks from running.Uncaught exceptions automatically set a non-zero exit code, but you should handle expected errors gracefully.
Example:
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
}
}
Use these community-standard packages owned by the Dart team to solve common CLI problems:
| Category | Recommended Package | Usage |
|---|---|---|
| Stack Traces | package:stack_trace | detailed, cleaner stack traces |
| Arg Parsing | package:args | standard flag/option parsing |
| Testing | package:test_process | integration testing for CLI apps |
| Testing | package:test_descriptor |
| Category | Recommended Package | Usage |
|---|---|---|
| Configuration | package:json_serializable | strongly typed config objects |
| CLI Generation | package:build_cli | generate arg parsers from classes |
| Version Info | package:build_version | automatic version injection |
| Configuration |
.dart_tool/[pkg_name]/.Weekly Installs
73
Repository
GitHub Stars
119
First Seen
Feb 6, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
antigravity57
github-copilot25
codex25
opencode25
gemini-cli24
amp23
Lark Skill Maker 教程:基于飞书CLI创建AI技能,自动化工作流与API调用指南
39,100 周安装
| file system fixtures for tests |
| Networking | package:http | standard HTTP client (remember user-agent!) |
| ANSI Output | package:io | handling ANSI colors and styles |
package:checked_yaml| precise YAML parsing with line numbers |