Example #1
0
        /// <summary>
        /// Gets the best match for value in the given values array
        /// </summary>
        /// <param name="distanceMethod">A method that calculates a string distance</param>
        /// <param name="value"></param>
        /// <param name="values">The values to search through</param>
        /// <param name="treshold">A treshold for word possible match</param>
        /// <returns>The index of the best match or -1 when none is found</returns>
        internal static int GetBestMatchIndex(Func <string, string, int> distanceMethod, string value, string[] values, double treshold)
        {
            if (distanceMethod == null || value == null || values == null)
            {
                return(-1);
            }
            var bestMatchValue = -1d;
            var bestMatchIndex = -1;

            for (var i = 0; i < values.Length; i++)
            {
                if (values[i] == null)
                {
                    continue;
                }

                var distance           = distanceMethod(value, values[i]);
                var length             = value.Length > values[i].Length ? value.Length : values[i].Length;
                var normalizedDistance = StringDistance.NormalizeDistance(distance, length);
                if (bestMatchValue < normalizedDistance && normalizedDistance >= treshold)
                {
                    bestMatchValue = normalizedDistance;
                    bestMatchIndex = i;
                }
            }
            return(bestMatchIndex);
        }
Example #2
0
        /// <summary>
        /// Gets a list of suggestions from sub commands and options of <paramref name="command"/> that are likely to
        /// fix the invalid argument <paramref name="input"/>
        /// </summary>
        /// <param name="command"></param>
        /// <param name="input"></param>
        /// <returns>A list of string with suggestions or null if no suggestions were found</returns>
        public static IEnumerable <string> GetTopSuggestions(CommandLineApplication command, string input)
        {
            var candidates = GetCandidates(command).ToList();

            if (candidates.Count == 0)
            {
                return(Enumerable.Empty <string>());
            }

            return(StringDistance.GetBestMatchesSorted(StringDistance.DamareuLevenshteinDistance,
                                                       input,
                                                       candidates,
                                                       0.33d));
        }