runIdleCli function
Runs the idle command-line interface.
This is the programmatic entrypoint for bin/idle.dart. It parses args,
performs exactly one command action (create, tick, analyze, save,
load, help), and writes JSON to stdout via CliIo.writeJson.
On failure it returns a non-zero exit code and writes structured error JSON
to stderr. Pass --debug to include a stack trace in that error JSON.
Implementation
Future<int> runIdleCli(List<String> args, {CliIo? io}) async {
final effectiveIo = io ?? CliIo.system();
var debug = false;
try {
final parser = _buildParser();
final results = parser.parse(args);
debug = results['debug'] == true;
if (results['help'] == true || results.command == null) {
effectiveIo.writeJson(_helpJson(command: results.command?.name));
return 0;
}
final command = results.command!;
if (command['help'] == true) {
effectiveIo.writeJson(_helpJson(command: command.name));
return 0;
}
final action = switch (command.name) {
'create' => _create(command, effectiveIo),
'tick' => _tick(command, effectiveIo),
'analyze' => _analyze(command, effectiveIo),
'save' => _save(command, effectiveIo),
'load' => _load(command, effectiveIo),
'help' => _help(command, effectiveIo),
_ => Future.value(
_fail(
effectiveIo,
message: 'Unknown command: ${command.name}',
details: {
'command': command.name,
'supported': const [
'create',
'tick',
'analyze',
'save',
'load',
'help',
],
},
debug: debug,
),
),
};
return await action;
} on FormatException catch (e, st) {
return _fail(
effectiveIo,
message: e.message,
error: e,
stackTrace: st,
debug: debug,
);
} on ArgumentError catch (e, st) {
return _fail(
effectiveIo,
message: e.message ?? 'Invalid argument',
error: e,
stackTrace: st,
debug: debug,
);
} catch (e, st) {
return _fail(
effectiveIo,
message: 'Unhandled error',
error: e,
stackTrace: st,
debug: debug,
);
}
}