Пример #1
0
        public static FindOptionType GetOptionType(string option)
        {
            FindOptionType optionType = FindOptionType.None;

            foreach (var pair in Options)
            {
                foreach (var item in pair.Value)
                {
                    if (item == option)
                    {
                        optionType = pair.Key;
                        break;
                    }
                }
            }

            return(optionType);
        }
Пример #2
0
        private static FindCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            FindCommandLineOptions targetOptions = new FindCommandLineOptions();

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

                    case FindOptionType.Directory:
                        targetOptions.IsSetDirectory = true;
                        targetOptions.Directory      = commandLineOptions.Arguments[arg];
                        break;

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

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

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

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (!targetOptions.IsSetDirectory)
                {
                    targetOptions.IsSetDirectory = true;
                    targetOptions.Directory      = commandLineOptions.Parameters.First();
                }

                if (commandLineOptions.Parameters.Count >= 2)
                {
                    if (!targetOptions.IsSetRegexPattern)
                    {
                        targetOptions.IsSetRegexPattern = true;
                        targetOptions.RegexPattern      = commandLineOptions.Parameters.ElementAt(1);
                    }
                }
            }

            return(targetOptions);
        }