private static async System.Threading.Tasks.Task RunAsync(string[] args) { if (File.Exists(SessionFileName)) { kitsuSession = await KitsuSessionManager.RestoreSessionFromFileAsync(SessionFileName); } Console.Title = string.Format("Inari for Kitsu.IO (v{0})", Assembly.GetExecutingAssembly().GetName().Version.ToString()); if (kitsuSession != null) { Console.Title += string.Format(" | Logged in as: {0}", kitsuSession.UserName); } TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(); Parser.Default.ParseArguments <AuthOptions, TrendingOptions, GetOptions>(args) .WithParsed <AuthOptions>(opts => AuthenicationCommand(opts, taskCompletionSource)) .WithParsed <TrendingOptions>(opts => TrendingCommand(opts, taskCompletionSource)) .WithParsed <GetOptions>(opts => GetCommand(opts, taskCompletionSource)) .WithNotParsed(errs => { taskCompletionSource.SetResult(null); }); await taskCompletionSource.Task; }
private static async void AuthenicationCommand(AuthOptions authOptions, TaskCompletionSource <object> taskCompletionSource) { try { KitsuSession session = await KitsuSessionManager.BeginSession(authOptions.Username, authOptions.Password); await KitsuSessionManager.PersistSessionToFileAsync(session, SessionFileName); Console.WriteLine("Logged in as: {0}", session.UserName); taskCompletionSource.SetResult(session); } catch (Exception ex) { Console.Error.WriteLine("Unable to log in."); Console.Error.WriteLine(ex.ToString()); taskCompletionSource.SetException(ex); } }