Beispiel #1
0
 /// <summary>
 /// For a given set of commands, extracts command names
 /// that are similar to the provided expression in terms
 /// of string distance.
 /// </summary>
 public static IEnumerable <string> SimilarNames(
     this IEnumerable <Command> commands,
     string expression,
     int maximumEditDistance)
 {
     return(commands
            .SelectMany(
                command => PrototypeHelper
                .GetComponents(command.Prototype)
                .Select(name => new { Name = name, Distance = name.LevenshteinDistance(expression) })
                .Where(pair => pair.Distance <= maximumEditDistance))
            .OrderBy(pair => pair.Distance)
            .Select(pair => pair.Name));
 }
Beispiel #2
0
        /// <summary>
        /// Gets the actual name of a command line flag
        /// provided by the user in the command line arguments.
        /// </summary>
        public string GetProvidedName(IEnumerable <string> commandLineArguments)
        {
            IEnumerable <string> flagNames = PrototypeHelper
                                             .GetComponents(this.Prototype)
                                             .SelectMany(flagName =>
                                                         new string[]
            {
                $"-{flagName}",
                $"--{flagName}",
                $"/{flagName}"
            });

            return(commandLineArguments.First(argument => flagNames.Contains(argument)));
        }
Beispiel #3
0
        /// <summary>
        /// Returns a set of commands whose prototypes make a match
        /// with the provided value.
        /// </summary>
        public static IEnumerable <Command> Matching(this IEnumerable <Command> commands, string expression)
        {
            IEnumerable <Command> strictMatches = commands.Where(
                command => new Regex(command.Prototype).IsMatch(expression));

            if (strictMatches.Any())
            {
                return(strictMatches);
            }

            IEnumerable <Command> prefixMatches = commands.Where(command =>
            {
                IEnumerable <string> commandComponents =
                    PrototypeHelper.GetComponents(command.Prototype);

                return(commandComponents.Any(
                           component => component.StartsWith(
                               expression,
                               StringComparison.OrdinalIgnoreCase)));
            });

            return(prefixMatches);
        }