static void Main(string[] args) { var parsedArgs = Parser.Default.ParseArguments <FileRegexProcessingOptions, CompareFileRegexProcessingOptions>(args) .MapResult( (FileRegexProcessingOptions opts) => FileRegexProcessingOptions.Process(opts), (CompareFileRegexProcessingOptions opts) => CompareFileRegexProcessingOptions.Process(opts), errs => 1 ); }
public static int Process(FileRegexProcessingOptions opts) { HashSet <string> hash = new HashSet <string>(); var directory = opts.Dir; if (String.IsNullOrEmpty(directory)) { directory = "."; } var files = Directory.GetFiles(directory, opts.FileName); var pattern = new Regex(opts.Pattern); var groupNames = pattern.GetGroupNames(); foreach (var f in files) { string content = File.ReadAllText(f); var matches = pattern.Matches(content); foreach (Match match in matches) { var output = opts.Template; foreach (var g in groupNames) { output = output.Replace($"{{{g}}}", match.Groups[g].Value); } if (opts.Unique && !hash.Contains(output)) { hash.Add(output); Console.WriteLine(output); } else if (!opts.Unique) { Console.WriteLine(output); } } } Console.ReadKey(); return(0); }