Exemple #1
0
        public override void Execute()
        {
            base.Execute();

            List <string>      singleOptionList = GrepOptions.GetSingleOptions();
            CommandLineOptions cloptions        = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray());

            options = ParseOptions(cloptions);
            CheckOptions(options);

            if (options.IsSetHelp)
            {
                RaiseCommandLineUsage(this, GrepOptions.Usage);
            }
            else if (options.IsSetVersion)
            {
                RaiseCommandLineUsage(this, Version);
            }
            else
            {
                if (options.IsSetOutputFile)
                {
                    DeleteOutputFile();
                }

                StartGrep();
            }

            Terminate();
        }
Exemple #2
0
        public override void Execute()
        {
            base.Execute();

              List<string> singleOptionList = GrepOptions.GetSingleOptions();
              CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray());
              options = ParseOptions(cloptions);
              CheckOptions(options);

              if (options.IsSetHelp)
              {
            RaiseCommandLineUsage(this, GrepOptions.Usage);
              }
              else if (options.IsSetVersion)
              {
            RaiseCommandLineUsage(this, Version);
              }
              else
              {
            if (options.IsSetOutputFile)
            {
              DeleteOutputFile();
            }

            StartGrep();
              }

              Terminate();
        }
Exemple #3
0
 private static void CheckOptions(GrepCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (!checkedOptions.IsSetRegexPattern || string.IsNullOrEmpty(checkedOptions.RegexPattern))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify regex pattern for matching."));
         }
         if (!checkedOptions.IsSetPath || checkedOptions.FilePaths.Count <= 0)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a path for grep."));
         }
         if (checkedOptions.IsSetOutputFile && string.IsNullOrEmpty(checkedOptions.OutputFile))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "bad output file path format."));
         }
     }
 }
Exemple #4
0
        private static GrepCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            GrepCommandLineOptions targetOptions = new GrepCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    GrepOptionType optionType = GrepOptions.GetOptionType(arg);
                    if (optionType == GrepOptionType.None)
                    {
                        throw new CommandLineException(
                                  string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}",
                                                string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg)));
                    }

                    switch (optionType)
                    {
                    case GrepOptionType.RegexPattern:
                        targetOptions.IsSetRegexPattern = true;
                        targetOptions.RegexPattern      = commandLineOptions.Arguments[arg];
                        break;

                    case GrepOptionType.File:
                        targetOptions.IsSetPath = true;
                        targetOptions.FilePaths.Add(commandLineOptions.Arguments[arg]);
                        break;

                    case GrepOptionType.FixedStrings:
                        targetOptions.IsSetFixedStrings = true;
                        break;

                    case GrepOptionType.IgnoreCase:
                        targetOptions.IsSetIgnoreCase = true;
                        break;

                    case GrepOptionType.InvertMatch:
                        targetOptions.IsSetInvertMatch = true;
                        break;

                    case GrepOptionType.OutputFile:
                        targetOptions.IsSetOutputFile = true;
                        targetOptions.OutputFile      = commandLineOptions.Arguments[arg];
                        break;

                    case GrepOptionType.Count:
                        targetOptions.IsSetCount = true;
                        break;

                    case GrepOptionType.FilesWithoutMatch:
                        targetOptions.IsSetFilesWithoutMatch = true;
                        break;

                    case GrepOptionType.FilesWithMatchs:
                        targetOptions.IsSetFilesWithMatchs = true;
                        break;

                    case GrepOptionType.NoMessages:
                        targetOptions.IsSetNoMessages = true;
                        break;

                    case GrepOptionType.WithFileName:
                        targetOptions.IsSetWithFileName = true;
                        break;

                    case GrepOptionType.NoFileName:
                        targetOptions.IsSetNoFileName = true;
                        break;

                    case GrepOptionType.LineNumber:
                        targetOptions.IsSetLineNumber = true;
                        break;

                    case GrepOptionType.Directory:
                        targetOptions.IsSetDirectory = true;
                        break;

                    case GrepOptionType.ExcludeFiles:
                        targetOptions.IsSetExcludeFiles   = true;
                        targetOptions.ExcludeFilesPattern = commandLineOptions.Arguments[arg];
                        break;

                    case GrepOptionType.ExcludeDirectories:
                        targetOptions.IsSetExcludeDirectories   = true;
                        targetOptions.ExcludeDirectoriesPattern = commandLineOptions.Arguments[arg];
                        break;

                    case GrepOptionType.IncludeFiles:
                        targetOptions.IsSetIncludeFiles   = true;
                        targetOptions.IncludeFilesPattern = commandLineOptions.Arguments[arg];
                        break;

                    case GrepOptionType.Recursive:
                        targetOptions.IsSetRecursive = true;
                        break;

                    case GrepOptionType.Help:
                        targetOptions.IsSetHelp = true;
                        break;

                    case GrepOptionType.Version:
                        targetOptions.IsSetVersion = true;
                        break;
                    }
                }
            }

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (!targetOptions.IsSetRegexPattern)
                {
                    targetOptions.IsSetRegexPattern = true;
                    targetOptions.RegexPattern      = commandLineOptions.Parameters.First();

                    for (int i = 1; i < commandLineOptions.Parameters.Count; i++)
                    {
                        targetOptions.IsSetPath = true;
                        targetOptions.FilePaths.Add(commandLineOptions.Parameters.ElementAt(i));
                    }
                }
                else
                {
                    if (!targetOptions.IsSetPath)
                    {
                        targetOptions.IsSetPath = true;
                        foreach (var item in commandLineOptions.Parameters)
                        {
                            targetOptions.FilePaths.Add(item);
                        }
                    }
                }
            }

            return(targetOptions);
        }
Exemple #5
0
        private static GrepCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              GrepCommandLineOptions targetOptions = new GrepCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              GrepOptionType optionType = GrepOptions.GetOptionType(arg);
              if (optionType == GrepOptionType.None)
            throw new CommandLineException(
              string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}",
              string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg)));

              switch (optionType)
              {
            case GrepOptionType.RegexPattern:
              targetOptions.IsSetRegexPattern = true;
              targetOptions.RegexPattern = commandLineOptions.Arguments[arg];
              break;
            case GrepOptionType.File:
              targetOptions.IsSetPath = true;
              targetOptions.FilePaths.Add(commandLineOptions.Arguments[arg]);
              break;
            case GrepOptionType.FixedStrings:
              targetOptions.IsSetFixedStrings = true;
              break;
            case GrepOptionType.IgnoreCase:
              targetOptions.IsSetIgnoreCase = true;
              break;
            case GrepOptionType.InvertMatch:
              targetOptions.IsSetInvertMatch = true;
              break;
            case GrepOptionType.OutputFile:
              targetOptions.IsSetOutputFile = true;
              targetOptions.OutputFile = commandLineOptions.Arguments[arg];
              break;
            case GrepOptionType.Count:
              targetOptions.IsSetCount = true;
              break;
            case GrepOptionType.FilesWithoutMatch:
              targetOptions.IsSetFilesWithoutMatch = true;
              break;
            case GrepOptionType.FilesWithMatchs:
              targetOptions.IsSetFilesWithMatchs = true;
              break;
            case GrepOptionType.NoMessages:
              targetOptions.IsSetNoMessages = true;
              break;
            case GrepOptionType.WithFileName:
              targetOptions.IsSetWithFileName = true;
              break;
            case GrepOptionType.NoFileName:
              targetOptions.IsSetNoFileName = true;
              break;
            case GrepOptionType.LineNumber:
              targetOptions.IsSetLineNumber = true;
              break;
            case GrepOptionType.Directory:
              targetOptions.IsSetDirectory = true;
              break;
            case GrepOptionType.ExcludeFiles:
              targetOptions.IsSetExcludeFiles = true;
              targetOptions.ExcludeFilesPattern = commandLineOptions.Arguments[arg];
              break;
            case GrepOptionType.ExcludeDirectories:
              targetOptions.IsSetExcludeDirectories = true;
              targetOptions.ExcludeDirectoriesPattern = commandLineOptions.Arguments[arg];
              break;
            case GrepOptionType.IncludeFiles:
              targetOptions.IsSetIncludeFiles = true;
              targetOptions.IncludeFilesPattern = commandLineOptions.Arguments[arg];
              break;
            case GrepOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case GrepOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case GrepOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              if (commandLineOptions.Parameters.Count > 0)
              {
            if (!targetOptions.IsSetRegexPattern)
            {
              targetOptions.IsSetRegexPattern = true;
              targetOptions.RegexPattern = commandLineOptions.Parameters.First();

              for (int i = 1; i < commandLineOptions.Parameters.Count; i++)
              {
            targetOptions.IsSetPath = true;
            targetOptions.FilePaths.Add(commandLineOptions.Parameters.ElementAt(i));
              }
            }
            else
            {
              if (!targetOptions.IsSetPath)
              {
            targetOptions.IsSetPath = true;
            foreach (var item in commandLineOptions.Parameters)
            {
              targetOptions.FilePaths.Add(item);
            }
              }
            }
              }

              return targetOptions;
        }
Exemple #6
0
 private static void CheckOptions(GrepCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
       {
     if (!checkedOptions.IsSetRegexPattern || string.IsNullOrEmpty(checkedOptions.RegexPattern))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify regex pattern for matching."));
     }
     if (!checkedOptions.IsSetPath || checkedOptions.FilePaths.Count <= 0)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify a path for grep."));
     }
     if (checkedOptions.IsSetOutputFile && string.IsNullOrEmpty(checkedOptions.OutputFile))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "bad output file path format."));
     }
       }
 }