parseCliArgs static method

GitRevisionCliArgs parseCliArgs(
  1. List<String> args
)

Implementation

static GitRevisionCliArgs parseCliArgs(List<String> args) {
  final ArgResults argResults = _cliArgParser.parse(args);

  final parsedCliArgs = GitRevisionCliArgs();

  parsedCliArgs.showHelp = argResults['help'] as bool;
  parsedCliArgs.showVersion = argResults['version'] as bool;
  parsedCliArgs.fullOutput = argResults['full'] as bool;
  parsedCliArgs.repoPath = argResults['context'] as String?;
  parsedCliArgs.baseBranch = argResults['baseBranch'] as String?;
  parsedCliArgs.yearFactor = intArg(argResults, 'yearFactor');
  parsedCliArgs.stopDebounce = intArg(argResults, 'stopDebounce');
  if (argResults.rest.length == 1) {
    final rest = argResults.rest[0];
    if (rest.isNotEmpty) {
      parsedCliArgs.revision = rest;
    }
  } else if (argResults.rest.length > 1) {
    throw ArgError('expected only one revision argument, found ${argResults.rest.length}: ${argResults.rest}');
  }

  final String? rawName = argResults['name'] as String?;
  if (rawName != null) {
    String safeName = rawName.replaceAll(RegExp(r'[^\w_\-\/]+'), '_').replaceAll('__', '_');

    // trim underscore at start and end
    if (safeName[0] == '_') {
      safeName = safeName.substring(1, safeName.length - 1);
    }
    if (safeName[safeName.length - 1] == '_') {
      safeName = safeName.substring(0, safeName.length - 2);
    }

    if (safeName.isNotEmpty && safeName != '_') {
      parsedCliArgs.name = safeName;
    }
  }

  return parsedCliArgs;
}