public static ReplaceOptionType GetOptionType(string option) { ReplaceOptionType optionType = ReplaceOptionType.None; foreach (var pair in Options) { foreach (var item in pair.Value) { if (item == option) { optionType = pair.Key; break; } } } return(optionType); }
private static ReplaceCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } ReplaceCommandLineOptions targetOptions = new ReplaceCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { ReplaceOptionType optionType = ReplaceOptions.GetOptionType(arg); if (optionType == ReplaceOptionType.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 ReplaceOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.FromText: targetOptions.FromText = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.ToText: targetOptions.ToText = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.InputDirectory: targetOptions.IsSetInputDirectory = true; targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case ReplaceOptionType.Extension: targetOptions.Extensions.AddRange( commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList()); break; case ReplaceOptionType.Help: targetOptions.IsSetHelp = true; break; case ReplaceOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = commandLineOptions.Parameters.First(); } } return(targetOptions); }