Beispiel #1
0
        public int Execute()
        {
            try
            {
                var regex = new Regex(settings.Pattern, settings.RegexOptions);

                var inputs = new Inputs(settings.Files);
                if(!settings.Output.FileNames.HasValue)
                    settings.Output.FileNames = inputs.Any;

                var anyMatch = false;
                inputs.Process((name, file) => {
                    var fileHasNoMatch = true;
                    string line;
                    var lineNumber = 0;
                    while ((line = file.ReadLine()) != null)
                    {
                        lineNumber++;
                        if (regex.IsMatch(line) != settings.InvertMatch)
                        {
                            fileHasNoMatch = false;
                            anyMatch = true;
                            if(settings.Output.OnlyFileNames == OnlyFileNames.No)
                            {
                                if (settings.Output.FileNames.Value)
                                    Console.Out.Write("{0}:", name);
                                if (settings.Output.LineNumbers)
                                    Console.Out.Write("{0}:", lineNumber);
                                Console.Out.WriteLine(line);
                            }
                            else if(settings.Output.OnlyFileNames == OnlyFileNames.Matching){
                                Console.WriteLine(name);
                                break;
                            }
                        }
                    }
                    if (settings.Output.OnlyFileNames == OnlyFileNames.NonMatching && fileHasNoMatch)
                    {
                        Console.WriteLine(name);
                    }
                });

                return anyMatch ? 0 : 1;
            }
            catch (Exception ex)
            {
                if(!settings.SuppressErrorMessages)
                    Console.Error.WriteLine(ex.Message);
                return 1;
            }
        }
Beispiel #2
0
        public int Execute()
        {
            try
            {
                var regex = new Regex(settings.Pattern, settings.RegexOptions);
                var inputs = new Inputs(settings.Files);
                inputs.Process((_,file) => {
                    string line;
                    while ((line = file.ReadLine()) != null)
                    {
                        Console.Out.WriteLine(regex.Replace(line, settings.Replace));
                    }
                });

                return 0;
            }
            catch (Exception ex)
            {
                if(!settings.SuppressErrorMessages)
                    Console.Error.WriteLine(ex.Message);
                return 1;
            }
        }