runWithArgs<T> static method

Future<T> runWithArgs<T>(
  1. List<String> args,
  2. FutureOr<T> body(
    1. Cli cli,
    2. List<String> rest
    ), {
  3. CliTheme? theme,
  4. SourceLines? sourceLines,
  5. bool handleErrors = true,
  6. int errorExitCode = 1,
  7. CliTerminal open({
    1. bool? ansi,
    2. bool? interactive,
    })?,
})

run with its flags taken off args: -v/--verbose, -q/--quiet, --no-color and --no-input, wherever they are before a --; the rest, in order and with what follows -- as it is, goes to body. Errors are handled as run handles them.

Future<void> main(List<String> args) => Cli.runWithArgs(
  args,
  (cli, rest) => cli.info('files: ${rest.join(', ')}'),
);

Implementation

static Future<T> runWithArgs<T>(
  List<String> args,
  FutureOr<T> Function(Cli cli, List<String> rest) body, {
  CliTheme? theme,
  SourceLines? sourceLines,
  bool handleErrors = true,
  int errorExitCode = 1,
  CliTerminal Function({bool? ansi, bool? interactive})? open,
}) {
  var verbose = false;
  var quiet = false;
  var color = true;
  var input = true;
  final rest = <String>[];
  for (var i = 0; i < args.length; i++) {
    switch (args[i]) {
      case '-v' || '--verbose':
        verbose = true;
      case '-q' || '--quiet':
        quiet = true;
      case '--no-color':
        color = false;
      case '--no-input':
        input = false;
      case '--':
        rest.addAll(args.skip(i));
        i = args.length;
      default:
        rest.add(args[i]);
    }
  }
  return run(
    (cli) => body(cli, rest),
    verbose: verbose,
    quiet: quiet,
    color: color,
    input: input,
    theme: theme,
    sourceLines: sourceLines,
    handleErrors: handleErrors,
    errorExitCode: errorExitCode,
    open: open,
  );
}