run method

Future<int> run(
  1. List<String> args, {
  2. required CliRejectionHandler onReject,
  3. IOSink? stdout,
  4. IOSink? stderr,
})

Resolves and dispatches args. onReject is required: it takes over for every invocation that does not resolve to a route, and decides the process exit code; the router itself never maps a rejection to one.

stdout and stderr are I/O wiring, not behavior: when null (the default), they fall back to the real process streams, io.stdout and io.stderr. This is the router's one deliberate default, made only to avoid forcing every caller to thread the process streams through by hand; pass a fake io.IOSink explicitly (e.g. in a test) to capture what a handler writes instead.

Implementation

Future<int> run(
  List<String> args, {
  required CliRejectionHandler onReject,
  io.IOSink? stdout,
  io.IOSink? stderr,
}) async {
  final out = stdout ?? io.stdout;
  final err = stderr ?? io.stderr;
  final outcome = resolve(args);
  if (outcome is CliRejection) {
    return await onReject(outcome);
  }
  final resolution = outcome as CliResolution;
  final h = _composeMiddlewares(_middlewares, resolution.handler);
  final req = CliRequest(
    originalArgs: args,
    route: resolution.route,
    params: resolution.params,
    rest: resolution.rest,
    options: resolution.options,
    stdout: out,
    stderr: err,
  );
  return await h(req);
}