예제 #1
0
 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
         );
 }
예제 #2
0
        public static int Process(CompareFileRegexProcessingOptions opts)
        {
            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();

            Dictionary <string, int> sourceResults  = new Dictionary <string, int>();
            Dictionary <string, int> compareResults = new Dictionary <string, int>();

            //Build sourceResults
            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 (sourceResults.ContainsKey(output))
                    {
                        sourceResults[output] = sourceResults[output]++;
                    }
                    else
                    {
                        sourceResults.Add(output, 1);
                    }
                }
            }

            //Build compareResults
            files = Directory.GetFiles(directory, opts.CompareFileName);
            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 (compareResults.ContainsKey(output))
                    {
                        compareResults[output] = compareResults[output]++;
                    }
                    else
                    {
                        compareResults.Add(output, 1);
                    }
                }
            }

            int countDiff    = 0;
            int countMissing = 0;

            Console.WriteLine($"Differences {opts.FileName} in {opts.CompareFileName}?");
            foreach (var k in sourceResults.Keys)
            {
                if (compareResults.ContainsKey(k) && compareResults[k] != sourceResults[k])
                {
                    Console.WriteLine($"Count  - {compareResults[k]} - {sourceResults[k]} - {k}");
                    countDiff++;
                }
                else if (!compareResults.ContainsKey(k))
                {
                    Console.WriteLine($"Missing  - {k}");
                    countMissing++;
                }
            }
            Console.WriteLine("----------------------------------");
            Console.WriteLine($"Differences {opts.CompareFileName} in {opts.FileName}?");
            foreach (var k in compareResults.Keys)
            {
                if (!sourceResults.ContainsKey(k))
                {
                    Console.WriteLine($"Missing - {k}");
                    countMissing++;
                }
            }

            Console.WriteLine($"{opts.FileName} Count: {sourceResults.Count}");
            Console.WriteLine($"{opts.CompareFileName} Count: {compareResults.Count}");
            Console.WriteLine($"Missing: {countMissing}");
            Console.WriteLine($"Diff   : {countDiff}");

            Console.ReadKey();

            return(0);
        }