private static TVal GetPartialOptionMatch()

in Source/NuGetGallery.Operations/Infrastructure/CommandLineParser.cs [163:192]


        private static TVal GetPartialOptionMatch<TVal>(IEnumerable<TVal> source, Func<TVal, string> getDisplayName, Func<TVal, string> getAltName, string option, string value)
        {
            var results = from item in source
                          where getDisplayName(item).StartsWith(value, StringComparison.OrdinalIgnoreCase) ||
                                (getAltName(item) ?? String.Empty).StartsWith(value, StringComparison.OrdinalIgnoreCase)
                          select item;

            if (!results.Any())
            {
                throw new CommandLineException(TaskResources.UnknownOptionError, option);
            }

            var result = results.FirstOrDefault();
            if (results.Skip(1).Any())
            {
                try
                {
                    // When multiple results are found, if there's an exact match, return it.
                    result = results.First(c => value.Equals(getDisplayName(c), StringComparison.OrdinalIgnoreCase) ||
                                                value.Equals(getAltName(c), StringComparison.OrdinalIgnoreCase));
                }
                catch (InvalidOperationException)
                {
                    throw new CommandLineException(String.Format(CultureInfo.CurrentCulture, TaskResources.AmbiguousOption, value,
                        String.Join(" ", from c in results select getDisplayName(c))));
                }
            }

            return result;
        }