run method

Future<int> run(
  1. List<String> args
)

Runs the CLI with the given arguments.

Returns the exit code (0 for success, non-zero for failure).

Implementation

Future<int> run(List<String> args) async {
  try {
    if (_shouldShowHelp(args)) {
      _printUsage();
      return 0;
    }

    if (args.isEmpty) {
      _printUsage();
      return 64; // EX_USAGE
    }

    String? token;
    final githubUrl = args.firstWhere(
      (arg) => !arg.startsWith('--'),
      orElse: () {
        _printUsage();
        throw const FormatException('GitHub URL is required');
      },
    );

    // Parse token from --token argument
    final tokenIndex = args.indexOf('--token');
    if (tokenIndex != -1 && tokenIndex + 1 < args.length) {
      token = args[tokenIndex + 1];
      if (token.startsWith('--')) {
        _errorOutput('❌ Error: Missing token value after --token');
        _printUsage();
        return 64; // EX_USAGE
      }
    }

    // Get output dir (first non-flag argument after the URL)
    final outputPath =
        args
            .skip(1)
            .where((arg) => arg != '--token' && !arg.startsWith('--'))
            .firstOrNull ??
        '';

    // Create a new downloader with the token if provided
    final downloader = token != null
        ? GitHubContentDownloader(githubToken: token)
        : _downloader;

    await downloader.downloadRepository(githubUrl, outputPath: outputPath);
    return 0;
  } on InvalidGitHubUrlException catch (e) {
    _errorOutput('❌ Invalid GitHub URL: ${e.message}');
    _printUsage();
    return 64; // EX_USAGE
  } on GitHubApiException catch (e) {
    _errorOutput('❌ GitHub API Error: ${e.message}');
    return 69; // EX_UNAVAILABLE
  } catch (e) {
    _errorOutput('❌ Error: $e');
    return 1; // EX_SOFTWARE
  }
}