public static async ValueTask Process(IEnumerable <string> args) { try { object?parsed = null; Parser parser = new Parser(settings => { settings.HelpWriter = Console.Error; settings.CaseSensitive = false; settings.CaseInsensitiveEnumValues = true; }); parser.ParseArguments(args, _OptionTypes).WithParsed(obj => parsed = obj); if (parsed is CommandOption commandOption) { MatchesContext matchesContext = await MatchesContext.GetMatchesContext(commandOption.Name); await commandOption.Process(matchesContext); await matchesContext.SaveChangesAsync(); if (!string.IsNullOrWhiteSpace(commandOption.ProcessingFinishedMessage)) { Console.WriteLine(commandOption.ProcessingFinishedMessage); } } else { throw new InvalidOperationException("Did not recognize given arguments."); } } catch (DbUpdateException ex) { if (ex.InnerException is SqliteException sqliteException) { switch (sqliteException.SqliteErrorCode) { case 1: Console.WriteLine( "A database format error has occurred. The referenced database may be from an older version, or corrupted."); break; } } else { Console.WriteLine($"{ex.InnerException?.Message ?? "No inner exception."} Operation not completed."); } } catch (Exception ex) { Console.WriteLine($"{ex.Message} Operation not completed."); } }
public static async ValueTask <MatchesContext> GetMatchesContext(string name) { string databasePath = $@"{Environment.CurrentDirectory}/{name}.sqlite"; if (!File.Exists(databasePath)) { Console.Write($"No match history database exists for '{name}'. Would you like to create one (y / n)? "); PromptDatabaseCreation(); } MatchesContext matchesContext = new MatchesContext(name); await matchesContext.Database.EnsureCreatedAsync(); return(matchesContext); }