Skip to content

Commit f7d89fb

Browse files
BornToBeRootclaude
andcommitted
Feature: Run command parser with unit tests
- RunCommandParser (Models): "command target [argument] [type]" or "command @Profile". Whitespace around ";" is removed, so lists can be typed with spaces; a type keyword and the second argument (e.g. ports, DNS server) can be in any order; the rest after "@" is the profile name (names can contain spaces). - First test project of the solution (NETworkManager.Tests, xUnit v3) with tests for the parser. dotnet test uses Microsoft.Testing.Platform (global.json). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 038e868 commit f7d89fb

14 files changed

Lines changed: 600 additions & 381 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NETworkManager.Models.RunCommand;
2+
3+
/// <summary>
4+
/// Second argument of a run command after the target.
5+
/// </summary>
6+
public enum RunCommandArgument
7+
{
8+
None,
9+
Ports,
10+
DNSServer
11+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace NETworkManager.Models.RunCommand;
6+
7+
/// <summary>
8+
/// Command of the run command dialog (e.g. "dnslookup" with the alias "dns"), which opens an application or the
9+
/// settings.
10+
/// </summary>
11+
public sealed class RunCommandDefinition
12+
{
13+
/// <summary>
14+
/// Whether the command opens an application or the settings.
15+
/// </summary>
16+
public RunCommandType Type { get; init; }
17+
18+
/// <summary>
19+
/// Application to open (<see cref="ApplicationName.None" /> for the settings).
20+
/// </summary>
21+
public ApplicationName Application { get; init; }
22+
23+
/// <summary>
24+
/// Command (lowercase, e.g. "dnslookup").
25+
/// </summary>
26+
public string Command { get; init; }
27+
28+
/// <summary>
29+
/// Short forms of the command (lowercase, e.g. "dns", "nslookup").
30+
/// </summary>
31+
public IReadOnlyList<string> Aliases { get; init; } = [];
32+
33+
/// <summary>
34+
/// The command accepts a target (e.g. host, IP range, query).
35+
/// </summary>
36+
public bool SupportsTarget { get; init; }
37+
38+
/// <summary>
39+
/// The command accepts a profile ("@name") instead of a target.
40+
/// </summary>
41+
public bool SupportsProfile { get; init; }
42+
43+
/// <summary>
44+
/// Second argument after the target (e.g. ports or DNS server).
45+
/// </summary>
46+
public RunCommandArgument Argument { get; init; }
47+
48+
/// <summary>
49+
/// Type keywords that can follow the target (lowercase, e.g. "a", "aaaa" for the DNS lookup).
50+
/// </summary>
51+
public IReadOnlyList<string> Types { get; init; } = [];
52+
53+
/// <summary>
54+
/// Syntax of the arguments for the hint (e.g. "&lt;hosts&gt; [server] [type]"), empty if there are none.
55+
/// </summary>
56+
public string Syntax { get; init; } = string.Empty;
57+
58+
/// <summary>
59+
/// Checks if the text is the command or one of its aliases (case-insensitive).
60+
/// </summary>
61+
/// <param name="text">Command text.</param>
62+
/// <returns>True if the text matches.</returns>
63+
public bool Matches(string text)
64+
{
65+
return string.Equals(Command, text, StringComparison.OrdinalIgnoreCase) ||
66+
Aliases.Any(x => string.Equals(x, text, StringComparison.OrdinalIgnoreCase));
67+
}
68+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NETworkManager.Models.RunCommand;
2+
3+
/// <summary>
4+
/// Errors of a run command input.
5+
/// </summary>
6+
public enum RunCommandParseError
7+
{
8+
None,
9+
Empty,
10+
UnknownCommand,
11+
NoArguments,
12+
NoProfiles,
13+
MissingProfileName,
14+
UnknownArgument
15+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace NETworkManager.Models.RunCommand;
2+
3+
/// <summary>
4+
/// Result of <see cref="RunCommandParser.Parse" />.
5+
/// </summary>
6+
public sealed class RunCommandParseResult
7+
{
8+
/// <summary>
9+
/// Command that matches the first word, or null if there is none.
10+
/// </summary>
11+
public RunCommandDefinition Command { get; init; }
12+
13+
/// <summary>
14+
/// First word as typed.
15+
/// </summary>
16+
public string CommandText { get; init; } = string.Empty;
17+
18+
/// <summary>
19+
/// Target (e.g. "10.0.0.1;10.0.0.2"), or null if there is none.
20+
/// </summary>
21+
public string Target { get; init; }
22+
23+
/// <summary>
24+
/// Name of the profile ("@name"), or null if no profile is used. Empty if only "@" was typed.
25+
/// </summary>
26+
public string ProfileName { get; init; }
27+
28+
/// <summary>
29+
/// Second argument (e.g. ports or DNS server), or null if there is none.
30+
/// </summary>
31+
public string Argument { get; init; }
32+
33+
/// <summary>
34+
/// Type keyword in lowercase (e.g. "aaaa"), or null if there is none.
35+
/// </summary>
36+
public string Type { get; init; }
37+
38+
/// <summary>
39+
/// Part of the input that is being typed.
40+
/// </summary>
41+
public RunCommandPosition Position { get; init; }
42+
43+
/// <summary>
44+
/// Word that is being typed (empty after a space), used to filter the suggestions.
45+
/// </summary>
46+
public string CurrentWord { get; init; } = string.Empty;
47+
48+
/// <summary>
49+
/// Input without the word that is being typed (e.g. "dns borntoberoot.net " for "dns borntoberoot.net aa"),
50+
/// used to complete a suggestion.
51+
/// </summary>
52+
public string InputBeforeCurrentWord { get; init; } = string.Empty;
53+
54+
/// <summary>
55+
/// Error, if the input can't be run.
56+
/// </summary>
57+
public RunCommandParseError Error { get; init; }
58+
59+
/// <summary>
60+
/// Value the error refers to (e.g. the unknown command or argument).
61+
/// </summary>
62+
public string ErrorValue { get; init; }
63+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NETworkManager.Models.RunCommand;
2+
3+
/// <summary>
4+
/// Part of the input that is being typed (used for the suggestions).
5+
/// </summary>
6+
public enum RunCommandPosition
7+
{
8+
Command,
9+
Target,
10+
Argument
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace NETworkManager.Models.RunCommand;
2+
3+
/// <summary>
4+
/// What a run command opens.
5+
/// </summary>
6+
public enum RunCommandType
7+
{
8+
Application,
9+
Setting
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<RootNamespace>NETworkManager.Tests</RootNamespace>
5+
<AssemblyName>NETworkManager.Tests</AssemblyName>
6+
<TargetFramework>net10.0-windows10.0.22621.0</TargetFramework>
7+
<IsPackable>false</IsPackable>
8+
<IsTestProject>true</IsTestProject>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1" />
13+
<PackageReference Include="xunit.v3" Version="4.0.2" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\NETworkManager.Models\NETworkManager.Models.csproj" />
17+
</ItemGroup>
18+
</Project>

0 commit comments

Comments
 (0)