using CommandLine;

using RolemasterDb.ImportTool;

var runner = new CriticalImportCommandRunner();

var exitCode = await Parser.Default.ParseArguments<ResetOptions, ExtractOptions, LoadOptions, ImportOptions, ReimportImagesOptions>(args)
    .MapResult(
        (ResetOptions options) => ExecuteAsync(() => runner.RunAsync(options)),
        (ExtractOptions options) => ExecuteAsync(() => runner.RunAsync(options)),
        (LoadOptions options) => ExecuteAsync(() => runner.RunAsync(options)),
        (ImportOptions options) => ExecuteAsync(() => runner.RunAsync(options)),
        (ReimportImagesOptions options) => ExecuteAsync(() => runner.RunAsync(options)),
        _ => Task.FromResult(1));

return exitCode;

static async Task<int> ExecuteAsync(Func<Task<int>> command)
{
    try
    {
        return await command();
    }
    catch (Exception exception)
    {
        Console.Error.WriteLine(exception.Message);
        return 1;
    }
}
