Exemple #1
0
        public static void Abbr(string[] args, AppData appData)
        {
            bool delete = false;
            bool room   = false;

            string[] opts = new OptionSet()
            {
                { "d|delete", _ => delete = true },
                { "R|room", _ => room = true },
            }.Parse(args).ToArray();

            string abbr = opts.GetArg(1);
            string ids  = opts.GetArg(2);

            if (!delete)
            {
                CliUtils.Validate(!String.IsNullOrWhiteSpace(abbr), AppError.ErrorCode.ArgumentParseError,
                                  "passed abbreviation is empty");

                CliUtils.Validate(!String.IsNullOrWhiteSpace(ids), AppError.ErrorCode.ArgumentParseError,
                                  "passed id is empty");

                long id;

                try {
                    id = Convert.ToInt64(ids);
                } catch {
                    throw new AppError(AppError.ErrorCode.ArgumentParseError, $"unable to convert id '{ids}' to integer");
                }

                appData.AddAbbr(abbr, id, room);
                appData.SaveChanges();
            }
            else
            {
                CliUtils.Validate(ids == null, AppError.ErrorCode.ArgumentParseError,
                                  "passed id, but requested abbreviation deletion");

                appData.DeleteAbbr(abbr);
                appData.SaveChanges();
            }
        }
Exemple #2
0
        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");
            }
        }