parse static method

ConvertCommandSettings parse(
  1. List<String> arguments
)

Parses arguments. Throws a ConvertUsageException saying what is wrong (an unknown option, a missing value, no input), or one whose ConvertUsageException.isHelp is set for -h and --help.

Implementation

static ConvertCommandSettings parse(List<String> arguments) {
  if (arguments.contains('-h') || arguments.contains('--help')) {
    throw const ConvertUsageException.helpAsked();
  }
  if (cliOptions['convert']!.unknownIn(arguments) case final String bad) {
    throw ConvertUsageException(unknownOption(bad));
  }
  final inputs = <String>[];
  var output = '.';
  var dryRun = false, overwrite = false, materials = true, json = false;
  var split = false;
  String? prefix, reportPath;
  var textures = TextureFamily.auto;
  var mips = true, impostor = false;
  var lods = const <double>[];
  int? chunks;
  for (var i = 0; i < arguments.length; i++) {
    final a = arguments[i];
    String? next() => i + 1 < arguments.length ? arguments[++i] : null;
    switch (a) {
      case '-o' || '--output':
        output =
            next() ??
            (throw const ConvertUsageException('-o needs a directory'));
      case '--dry-run':
        dryRun = true;
      case '--overwrite':
        overwrite = true;
      case '--asset-prefix':
        prefix =
            next() ??
            (throw const ConvertUsageException(
              '--asset-prefix needs a path',
            ));
      case '--no-materials':
        materials = false;
      case '--report':
        reportPath =
            next() ??
            (throw const ConvertUsageException('--report needs a file'));
      case '--json':
        json = true;
      case '--split':
        split = true;
      case '--textures':
        textures = TextureFamily.parse(
          next() ??
              (throw const ConvertUsageException(
                '--textures names a family',
              )),
        );
      case '--no-mips':
        mips = false;
      case '--impostor':
        impostor = true;
      case '--chunks':
        chunks = defaultChunkThreshold;
      case _ when a.startsWith('--chunks='):
        chunks =
            parseChunkThreshold(a.substring(9)) ??
            (throw const ConvertUsageException(
              '--chunks= takes a triangle count',
            ));
      case '--lods':
        lods = parseLodRatios(
          next() ??
              (throw const ConvertUsageException('--lods takes ratios')),
        );
      case _ when a.startsWith('--lods='):
        lods = parseLodRatios(a.substring(7));
      case '-h' || '--help':
        throw const ConvertUsageException.helpAsked();
      case _ when a.startsWith('-'):
        throw ConvertUsageException('unknown option $a');
      default:
        inputs.add(a);
    }
  }
  if (inputs.isEmpty) {
    throw const ConvertUsageException('no input named');
  }
  return ConvertCommandSettings(
    inputs: inputs,
    output: output,
    dryRun: dryRun,
    overwrite: overwrite,
    assetPrefix: prefix,
    materials: materials,
    reportPath: reportPath,
    json: json,
    split: split,
    models: ModelSettings(
      textures: textures,
      mips: mips,
      lods: lods,
      impostor: impostor,
      chunks: chunks,
    ),
  );
}