/// <summary> /// Получение словаря из аргументов командной строки /// </summary> /// <param name="options">Объект именованных аргументов командной строки</param> /// <returns>Словарь вида слово : значение</returns> /// <exception cref="Exception">Если опции --with и --dict были написаны вместе ИЛИ если доступа к файлу-словарю нет</exception> public static Dictionary <string, string> GetWordsFromOptions(this CommandLineOptions options) { if (options.IsReplaceFileADictionary && options.WordToReplaceWith != '\0') { throw new Exception("Опции -w / --with и -d / --dict не могут использоваться одновременно"); } if (!ConsoleArgsParser.FilePathIsValid(options.ReplaceFilePath)) { throw new Exception("Путь к файлу с заменяемыми словами - неверный"); } var replacedLines = File.ReadAllLines(options.ReplaceFilePath); var dict = new Dictionary <string, string>(); foreach (var line in replacedLines) { var splittedElem = line.Split(':'); string newWord; if (options.IsReplaceFileADictionary) { newWord = splittedElem.Last(); } else { var elemLength = splittedElem.First().Length; newWord = new string(options.WordToReplaceWith, elemLength); } dict[splittedElem.First()] = newWord; } return(dict); }
private static void Main(string[] args) { try { var inputTimer = new Stopwatch(); var replaceTimer = new Stopwatch(); var outputTimer = new Stopwatch(); var argParser = new ConsoleArgsParser(args); var options = argParser.Options; if (options == null) { return; } var reader = argParser.Reader; var writer = argParser.Writer; var wordsDict = options.GetWordsFromOptions(); inputTimer.Start(); var inputData = reader.ReadAll(); inputTimer.Stop(); replaceTimer.Start(); var replacedData = WordReplacer.ReplaceWordsInStrings(inputData, wordsDict); replaceTimer.Stop(); outputTimer.Start(); writer.Write(replacedData); outputTimer.Stop(); if (options.TimeTheProgram) { Console.WriteLine($"\n\nЧтение заняло: {inputTimer.ElapsedMilliseconds} мс\n" + $"Замена слов заняла: {replaceTimer.ElapsedMilliseconds} мс\n" + $"Вывод занял: {outputTimer.ElapsedMilliseconds} мс"); } } catch (Exception e) { Console.Error.WriteLine(e.Message); } }