|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace NETworkManager.Models.RunCommand; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Parses the input of the run command dialog: "command target [argument] [type]" or "command @profile". |
| 10 | +/// </summary> |
| 11 | +public static partial class RunCommandParser |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A list separator with whitespace around it ("10.0.0.1; 10.0.0.2"). |
| 15 | + /// </summary> |
| 16 | + [GeneratedRegex(@"\s*;\s*")] |
| 17 | + private static partial Regex ListSeparatorRegex(); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Parses the input. |
| 21 | + /// - Lists use ";" as everywhere in the application. Whitespace around ";" is removed, so whitespace only |
| 22 | + /// separates the arguments. |
| 23 | + /// - The first word is the command (name or alias), the second word the target. "@name" (the rest of the input, |
| 24 | + /// since profile names can contain spaces) is a profile instead of a target. |
| 25 | + /// - Further words are a type keyword of the command or its second argument (e.g. ports, DNS server), in any |
| 26 | + /// order. |
| 27 | + /// </summary> |
| 28 | + /// <param name="input">Input of the dialog.</param> |
| 29 | + /// <param name="commands">Available commands.</param> |
| 30 | + /// <returns>Parse result, see <see cref="RunCommandParseResult.Error" />.</returns> |
| 31 | + public static RunCommandParseResult Parse(string input, IEnumerable<RunCommandDefinition> commands) |
| 32 | + { |
| 33 | + input = (input ?? string.Empty).TrimStart(); |
| 34 | + |
| 35 | + var endsWithWhitespace = input.Length > 0 && char.IsWhiteSpace(input[^1]); |
| 36 | + var words = ListSeparatorRegex().Replace(input, ";").Split((char[])null, StringSplitOptions.RemoveEmptyEntries); |
| 37 | + |
| 38 | + if (words.Length == 0) |
| 39 | + return new RunCommandParseResult { Position = RunCommandPosition.Command, Error = RunCommandParseError.Empty }; |
| 40 | + |
| 41 | + var commandText = words[0]; |
| 42 | + var command = commands.FirstOrDefault(x => x.Matches(commandText)); |
| 43 | + |
| 44 | + // The command is being typed. |
| 45 | + if (command == null || words.Length == 1 && !endsWithWhitespace) |
| 46 | + return new RunCommandParseResult |
| 47 | + { |
| 48 | + Command = command, |
| 49 | + CommandText = commandText, |
| 50 | + Position = RunCommandPosition.Command, |
| 51 | + CurrentWord = commandText, |
| 52 | + Error = command == null ? RunCommandParseError.UnknownCommand : RunCommandParseError.None, |
| 53 | + ErrorValue = command == null ? commandText : null |
| 54 | + }; |
| 55 | + |
| 56 | + var commandPrefix = commandText + " "; |
| 57 | + |
| 58 | + // Only the command (e.g. "ping "). |
| 59 | + if (words.Length == 1) |
| 60 | + return new RunCommandParseResult |
| 61 | + { |
| 62 | + Command = command, |
| 63 | + CommandText = commandText, |
| 64 | + Position = RunCommandPosition.Target, |
| 65 | + InputBeforeCurrentWord = commandPrefix |
| 66 | + }; |
| 67 | + |
| 68 | + // Profile: the rest of the input after "@" is the name. |
| 69 | + if (words[1].StartsWith('@')) |
| 70 | + { |
| 71 | + var profileName = input[(input.IndexOf('@') + 1)..].Trim(); |
| 72 | + |
| 73 | + var error = !command.SupportsProfile |
| 74 | + ? RunCommandParseError.NoProfiles |
| 75 | + : profileName.Length == 0 |
| 76 | + ? RunCommandParseError.MissingProfileName |
| 77 | + : RunCommandParseError.None; |
| 78 | + |
| 79 | + return new RunCommandParseResult |
| 80 | + { |
| 81 | + Command = command, |
| 82 | + CommandText = commandText, |
| 83 | + ProfileName = profileName, |
| 84 | + Position = RunCommandPosition.Target, |
| 85 | + CurrentWord = "@" + profileName, |
| 86 | + InputBeforeCurrentWord = commandPrefix, |
| 87 | + Error = error, |
| 88 | + ErrorValue = error == RunCommandParseError.NoProfiles ? command.Command : null |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + if (!command.SupportsTarget) |
| 93 | + return new RunCommandParseResult |
| 94 | + { |
| 95 | + Command = command, |
| 96 | + CommandText = commandText, |
| 97 | + Position = RunCommandPosition.Target, |
| 98 | + CurrentWord = words[1], |
| 99 | + InputBeforeCurrentWord = commandPrefix, |
| 100 | + Error = RunCommandParseError.NoArguments, |
| 101 | + ErrorValue = command.Command |
| 102 | + }; |
| 103 | + |
| 104 | + // Further words: type keyword or second argument, in any order. A type keyword is never the second argument. |
| 105 | + string type = null; |
| 106 | + string argument = null; |
| 107 | + string unknownArgument = null; |
| 108 | + |
| 109 | + foreach (var word in words[2..]) |
| 110 | + { |
| 111 | + var keyword = word.ToLowerInvariant(); |
| 112 | + |
| 113 | + if (command.Types.Contains(keyword)) |
| 114 | + { |
| 115 | + if (type == null) |
| 116 | + type = keyword; |
| 117 | + else |
| 118 | + unknownArgument ??= word; |
| 119 | + } |
| 120 | + else if (argument == null && command.Argument != RunCommandArgument.None) |
| 121 | + { |
| 122 | + argument = word; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + unknownArgument ??= word; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + var isTypingTarget = words.Length == 2 && !endsWithWhitespace; |
| 131 | + var currentWord = endsWithWhitespace ? string.Empty : words[^1]; |
| 132 | + |
| 133 | + return new RunCommandParseResult |
| 134 | + { |
| 135 | + Command = command, |
| 136 | + CommandText = commandText, |
| 137 | + Target = words[1], |
| 138 | + Argument = argument, |
| 139 | + Type = type, |
| 140 | + Position = isTypingTarget ? RunCommandPosition.Target : RunCommandPosition.Argument, |
| 141 | + CurrentWord = currentWord, |
| 142 | + InputBeforeCurrentWord = string.Join(" ", words[..^(currentWord.Length == 0 ? 0 : 1)]) + " ", |
| 143 | + Error = unknownArgument == null ? RunCommandParseError.None : RunCommandParseError.UnknownArgument, |
| 144 | + ErrorValue = unknownArgument |
| 145 | + }; |
| 146 | + } |
| 147 | +} |
0 commit comments