public override void Execute()
        {
            base.Execute();

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

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

                StartGrep();
            }

            Terminate();
        }
Beispiel #2
0
 private static void CheckOptions(GrepCopyCommandLineOptions 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 grepcopy."));
         }
         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."));
         }
         if (checkedOptions.IsSetCopyFolder && string.IsNullOrEmpty(checkedOptions.CopyFolder))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "bad copy folder path format."));
         }
     }
 }
Beispiel #3
0
        public override void Execute()
        {
            base.Execute();

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

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

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

                StartGrep();
            }

            Terminate();
        }
 private static void CheckOptions(GrepCopyCommandLineOptions 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 grepcopy."));
         }
         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."));
         }
         if (checkedOptions.IsSetCopyFolder && string.IsNullOrEmpty(checkedOptions.CopyFolder))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
               "Option used in invalid context -- {0}", "bad copy folder path format."));
         }
     }
 }
        private static GrepCopyCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                  "Option used in invalid context -- {0}", "must specify a option."));

            GrepCopyCommandLineOptions targetOptions = new GrepCopyCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    GrepCopyOptionType optionType = GrepCopyOptions.GetOptionType(arg);
                    if (optionType == GrepCopyOptionType.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 GrepCopyOptionType.RegexPattern:
                            targetOptions.IsSetRegexPattern = true;
                            targetOptions.RegexPattern = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.File:
                            targetOptions.IsSetPath = true;
                            targetOptions.FilePaths.Add(commandLineOptions.Arguments[arg]);
                            break;
                        case GrepCopyOptionType.FixedStrings:
                            targetOptions.IsSetFixedStrings = true;
                            break;
                        case GrepCopyOptionType.IgnoreCase:
                            targetOptions.IsSetIgnoreCase = true;
                            break;
                        case GrepCopyOptionType.InvertMatch:
                            targetOptions.IsSetInvertMatch = true;
                            break;
                        case GrepCopyOptionType.OutputFile:
                            targetOptions.IsSetOutputFile = true;
                            targetOptions.OutputFile = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.CopyFolder:
                            targetOptions.IsSetCopyFolder = true;
                            targetOptions.CopyFolder = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.Count:
                            targetOptions.IsSetCount = true;
                            break;
                        case GrepCopyOptionType.FilesWithoutMatch:
                            targetOptions.IsSetFilesWithoutMatch = true;
                            break;
                        case GrepCopyOptionType.FilesWithMatchs:
                            targetOptions.IsSetFilesWithMatchs = true;
                            break;
                        case GrepCopyOptionType.NoMessages:
                            targetOptions.IsSetNoMessages = true;
                            break;
                        case GrepCopyOptionType.WithFileName:
                            targetOptions.IsSetWithFileName = true;
                            break;
                        case GrepCopyOptionType.NoFileName:
                            targetOptions.IsSetNoFileName = true;
                            break;
                        case GrepCopyOptionType.LineNumber:
                            targetOptions.IsSetLineNumber = true;
                            break;
                        case GrepCopyOptionType.Directory:
                            targetOptions.IsSetDirectory = true;
                            break;
                        case GrepCopyOptionType.ExcludeFiles:
                            targetOptions.IsSetExcludeFiles = true;
                            targetOptions.ExcludeFilesPattern = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.ExcludeDirectories:
                            targetOptions.IsSetExcludeDirectories = true;
                            targetOptions.ExcludeDirectoriesPattern = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.IncludeFiles:
                            targetOptions.IsSetIncludeFiles = true;
                            targetOptions.IncludeFilesPattern = commandLineOptions.Arguments[arg];
                            break;
                        case GrepCopyOptionType.Recursive:
                            targetOptions.IsSetRecursive = true;
                            break;
                        case GrepCopyOptionType.Help:
                            targetOptions.IsSetHelp = true;
                            break;
                        case GrepCopyOptionType.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;
        }
Beispiel #6
0
        private static GrepCopyCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            GrepCopyCommandLineOptions targetOptions = new GrepCopyCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    GrepCopyOptionType optionType = GrepCopyOptions.GetOptionType(arg);
                    if (optionType == GrepCopyOptionType.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 GrepCopyOptionType.RegexPattern:
                        targetOptions.IsSetRegexPattern = true;
                        targetOptions.RegexPattern      = commandLineOptions.Arguments[arg];
                        break;

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

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

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

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

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

                    case GrepCopyOptionType.CopyFolder:
                        targetOptions.IsSetCopyFolder = true;
                        targetOptions.CopyFolder      = commandLineOptions.Arguments[arg];
                        break;

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

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

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

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

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

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

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

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

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

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

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

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

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

                    case GrepCopyOptionType.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);
        }