Loading...
Loading...
Compare original and translation side by side
dart create -t cli <project_name>main()bin/lib/src/lib/<project_name>.dartdart format . --set-exit-if-changeddart create -t cli <project_name>main()bin/lib/src/lib/<project_name>.dartdart format . --set-exit-if-changedargsArgParseraddFlagaddOptiongitCommandRunnerCommandCommandRunner.argParserCommand.argParserUsageExceptionargsArgParseraddFlagaddOptiongitCommandRunnerCommandCommandRunner.argParserCommand.argParserUsageExceptioniostack_traceioExitCodeExitCode.success.codeExitCode.usage.codesharedStdInioChain.capture()stack_traceTrace.terseChain.tersestderrexit(1)UsageExceptioniostack_traceioExitCodeExitCode.success.codeExitCode.usage.codeiosharedStdInstack_traceChain.capture()Trace.terseChain.tersestderrexit(1)UsageException[!IMPORTANT] All new commands and significant features must be covered by automated tests. Manual verification is not sufficient for testing logic. However, manual verification of help text and user experience (UX) is still required to ensure the interface is intuitive and correct.
test_processtest_descriptortest_descriptord.dird.fileawait d.Descriptor.create()TestProcess.start('dart', ['run', 'bin/cli.dart', ...args])StreamQueueemitsThroughemitsawait process.shouldExit(0)await d.Descriptor.validate()[!IMPORTANT] 所有新命令和重要功能必须覆盖自动化测试。 手动验证不足以测试逻辑,但仍需手动验证帮助文本和用户体验(UX),确保界面直观且正确。
test_processtest_descriptortest_descriptord.dird.fileawait d.Descriptor.create()TestProcess.start('dart', ['run', 'bin/cli.dart', ...args])StreamQueueemitsThroughemitsawait process.shouldExit(0)await d.Descriptor.validate()dart run bin/cli.dartdart build clibuild/cli/_/bundle/dart compile exe bin/cli.dart -o <output_path>dart compile aot-snapshot bin/cli.dart.aotdartaotruntime--target-os--target-archdart compile exedart compile aot-snapshot--target-os=linux--target-arch=arm64--target-arch=x64--target-arch=arm--target-arch=riscv64dart compile exe --target-os=linux --target-arch=arm64 bin/cli.dartdart run bin/cli.dartdart build clibuild/cli/_/bundle/dart compile exe bin/cli.dart -o <output_path>dart compile aot-snapshot bin/cli.dartdartaotruntime.aotdart compile exedart compile aot-snapshot--target-os--target-arch--target-os=linux--target-arch=arm64--target-arch=x64--target-arch=arm--target-arch=riscv64dart compile exe --target-os=linux --target-arch=arm64 bin/cli.dartCommandlib/src/commands/namedescriptionargParser.addFlag()argParser.addOption()run()CommandRunnerbin/cli.dartaddCommand()test/test_processdart run bin/cli.dart help <command_name>dart compile exe./bin/cli <command>lib/src/commands/CommandnamedescriptionargParser.addFlag()argParser.addOption()run()bin/cli.dartCommandRunneraddCommand()test/test_processdart run bin/cli.dart help <command_name>dart compile exe./bin/cli <command>dart format . --set-exit-if-changeddart analyzedart testdart compile exe bin/cli.dart -o build/cli-hostdart compile exe --target-os=linux --target-arch=x64 bin/cli.dart -o build/cli-linux-x64dart format . --set-exit-if-changeddart analyzedart testdart compile exe bin/cli.dart -o build/cli-hostdart compile exe --target-os=linux --target-arch=x64 bin/cli.dart -o build/cli-linux-x64import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:stack_trace/stack_trace.dart';
class CommitCommand extends Command {
final String name = 'commit';
final String description = 'Record changes to the repository.';
CommitCommand() {
argParser.addFlag('all', abbr: 'a', help: 'Commit all changed files.');
}
Future<void> run() async {
final commitAll = argResults?['all'] as bool? ?? false;
print('Committing... (All: $commitAll)');
}
}
void main(List<String> args) {
Chain.capture(() async {
final runner = CommandRunner('dgit', 'Distributed version control.')
..addCommand(CommitCommand());
await runner.run(args);
}, onError: (error, chain) {
if (error is UsageException) {
stderr.writeln(error.message);
stderr.writeln(error.usage);
exit(64); // ExitCode.usage.code
} else {
stderr.writeln('Fatal error: $error');
stderr.writeln(chain.terse);
exit(1);
}
});
}import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:stack_trace/stack_trace.dart';
class CommitCommand extends Command {
final String name = 'commit';
final String description = 'Record changes to the repository.';
CommitCommand() {
argParser.addFlag('all', abbr: 'a', help: 'Commit all changed files.');
}
Future<void> run() async {
final commitAll = argResults?['all'] as bool? ?? false;
print('Committing... (All: $commitAll)');
}
}
void main(List<String> args) {
Chain.capture(() async {
final runner = CommandRunner('dgit', 'Distributed version control.')
..addCommand(CommitCommand());
await runner.run(args);
}, onError: (error, chain) {
if (error is UsageException) {
stderr.writeln(error.message);
stderr.writeln(error.usage);
exit(64); // ExitCode.usage.code
} else {
stderr.writeln('Fatal error: $error');
stderr.writeln(chain.terse);
exit(1);
}
});
}import 'package:test/test.dart';
import 'package:test_process/test_process.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
test('CLI formats output correctly and modifies filesystem', () async {
// 1. Setup mock filesystem
await d.dir('project', [
d.file('config.json', '{"key": "value"}')
]).create();
// 2. Spawn the CLI process
final process = await TestProcess.start(
'dart',
['run', 'bin/cli.dart', 'process', '--path', '${d.sandbox}/project']
);
// 3. Validate stdout stream
await expectLater(process.stdout, emitsThrough('Processing complete.'));
// 4. Validate exit code
await process.shouldExit(0);
// 5. Validate filesystem mutations
await d.dir('project', [
d.file('config.json', '{"key": "value"}'),
d.file('output.log', 'Success')
]).validate();
});
}import 'package:test/test.dart';
import 'package:test_process/test_process.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
test('CLI formats output correctly and modifies filesystem', () async {
// 1. 设置模拟文件系统
await d.dir('project', [
d.file('config.json', '{"key": "value"}')
]).create();
// 2. 启动CLI进程
final process = await TestProcess.start(
'dart',
['run', 'bin/cli.dart', 'process', '--path', '${d.sandbox}/project']
);
// 3. 验证标准输出流
await expectLater(process.stdout, emitsThrough('Processing complete.'));
// 4. 验证退出码
await process.shouldExit(0);
// 5. 验证文件系统变更
await d.dir('project', [
d.file('config.json', '{"key": "value"}'),
d.file('output.log', 'Success')
]).validate();
});
}