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 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); }