runCliAdapter function
Runs a CLI adapter end-to-end:
- If
argscontains--helpor-h, writeparser.usageto stdout and return 0. - Otherwise parse the args, writing
ERROR: <message>to stderr and returning 1 on FormatException (whichArgParserExceptionextends). - On successful parse, invoke
executewith the resultingArgResults.
Centralises all --help interception and parser-error formatting so
CLI adapter files don't repeat this boilerplate.
Implementation
Future<int> runCliAdapter(
ArgParser parser,
List<String> args,
Future<int> Function(ArgResults) execute,
) async {
if (args.contains('--help') || args.contains('-h')) {
final usage = parser.usage;
stdout.writeln(usage.isEmpty ? 'No options for this command.' : usage);
return 0;
}
final ArgResults results;
try {
results = parser.parse(args);
} on FormatException catch (e) {
stderr.writeln('ERROR: ${e.message}');
return 1;
}
return execute(results);
}