public static void Abbrs(string[] args, AppData appData) { bool erase = false; string save = null; string load = null; new OptionSet() { { "e|erase", _ => erase = true }, { "s=|save=", _ => save = _ }, { "l=|load=", _ => load = _ }, }.Parse(args); if (erase && save == null && load == null) { string[] abbrs = appData.GetAbbrs().ToArray(); foreach (string abbr in abbrs) { appData.DeleteAbbr(abbr); } appData.SaveChanges(); } else if (!erase && save != null && load == null) { var table = new Table(); foreach (string abbr in appData.GetAbbrs()) { table.Add(appData.GetId(abbr), abbr, (appData.IsRoom(abbr) ?? false) ? "room" : ""); } using (var w = new StreamWriter(save)) { table.Display(w); } } else if (!erase && save == null && load != null) { using (var r = new StreamReader(load)) { while (!r.EndOfStream) { string line = r.ReadLine(); string[] tokens = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length >= 2) { long id = Convert.ToInt64(tokens[0]); string abbr = tokens[1]; bool room = tokens.Length >= 3 && tokens[2] == "room"; appData.AddAbbr(abbr, id, room); } } } appData.SaveChanges(); } else if (!erase && save == null && load == null) { var table = new Table(); foreach (string abbr in appData.GetAbbrs()) { table.Add(appData.GetId(abbr), abbr, (appData.IsRoom(abbr) ?? false) ? "room" : ""); } table.Display(); } else { throw new AppError(AppError.ErrorCode.ArgumentParseError, "multiple modes (erase/save/load) are given"); } }
public static void Friends(string[] args, AppData appData) { bool onlyOnline = false; bool sort = false; Action <UserCollection> fullPF = (friends) => { if (friends.Count == 0) { Console.WriteLine("You have no friends :("); } var table = new Table(); foreach (var friend in friends) { table.Add( appData.GetAbbr(friend.Id), friend.Online.HasValue ? (friend.Online.Value ? "online" : "") : "", $"{friend.FirstName} {friend.LastName}", MiscUtils.FormatDate(friend.BirthDate) ); } if (sort) { table.SortBy(2); } table.Display(); }; Action <UserCollection> idsPF = (friends) => { bool first = true; foreach (var friend in friends) { if (first) { first = false; } else { Console.Write(" "); } Console.Write(appData.GetAbbr(friend.Id)); } Console.WriteLine(); }; Action <UserCollection> act = fullPF; new OptionSet() { { "i|ids", _ => act = idsPF }, { "o|online", _ => onlyOnline = true }, { "s|sort", _ => sort = true } }.Parse(args); var vk = new VkApi(); vk.Authorize(appData.AccessToken); var res = vk.Friends.Get(new FriendsGetParams() { Count = null, Fields = ProfileFields.Nickname | ProfileFields.FirstName | ProfileFields.LastName | ProfileFields.BirthDate }); if (onlyOnline) { var res2 = ( from f in res where f.Online.HasValue where f.Online.Value select f ).ToArray(); res = new UserCollection(res2); } act(res); }