From fc14cfe0c3b462f3e0e588d141313de97f706f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20St=C4=99pie=C5=84?= Date: Sun, 23 Aug 2026 18:43:26 +0200 Subject: [PATCH] feat: add regex argument type to console command builder --- .../Console/Contracts/CommandArgSpec.cs | 21 ++++++++++++------- .../Console/Contracts/CommandArgsBuilder.cs | 6 ++++++ .../Console/Domain/CommandArgValidator.cs | 2 ++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Features/Console/Contracts/CommandArgSpec.cs b/src/Features/Console/Contracts/CommandArgSpec.cs index c2315ac..c2fa8cb 100644 --- a/src/Features/Console/Contracts/CommandArgSpec.cs +++ b/src/Features/Console/Contracts/CommandArgSpec.cs @@ -8,13 +8,20 @@ public enum CommandArgType Int, Float, Bool, - Choice + Choice, + Regex } -public sealed record CommandArgSpec(string Name, CommandArgType Type, IReadOnlyList Choices = null) -{ - public override string ToString() => - Type == CommandArgType.Choice && Choices is not null - ? string.Join("|", Choices) - : Type.ToString().ToLowerInvariant(); +public sealed record CommandArgSpec( + string Name, + CommandArgType Type, + IReadOnlyList Choices = null, + string Pattern = null +) { + public override string ToString() => Type switch + { + CommandArgType.Choice when Choices is not null => string.Join("|", Choices), + CommandArgType.Regex when Pattern is not null => "regex", + _ => Type.ToString().ToLowerInvariant() + }; } diff --git a/src/Features/Console/Contracts/CommandArgsBuilder.cs b/src/Features/Console/Contracts/CommandArgsBuilder.cs index d7d1db9..4ec48aa 100644 --- a/src/Features/Console/Contracts/CommandArgsBuilder.cs +++ b/src/Features/Console/Contracts/CommandArgsBuilder.cs @@ -36,5 +36,11 @@ public sealed class CommandArgsBuilder return this; } + public CommandArgsBuilder AddRegexArg(string name, string pattern) + { + _specs.Add(new CommandArgSpec(name, CommandArgType.Regex, Pattern: pattern)); + return this; + } + public IReadOnlyList Build() => _specs.ToArray(); } diff --git a/src/Features/Console/Domain/CommandArgValidator.cs b/src/Features/Console/Domain/CommandArgValidator.cs index c2e91cc..3d8aafc 100644 --- a/src/Features/Console/Domain/CommandArgValidator.cs +++ b/src/Features/Console/Domain/CommandArgValidator.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.RegularExpressions; using Game.Features.Console.Contracts; namespace Game.Features.Console.Domain; @@ -18,6 +19,7 @@ public static class CommandArgValidator CommandArgType.Float => float.TryParse(rawValue, NumberStyles.Float, CultureInfo.InvariantCulture, out _), CommandArgType.Bool => bool.TryParse(rawValue, out _), CommandArgType.Choice => spec.Choices is not null && ChoicesContain(spec.Choices, rawValue), + CommandArgType.Regex => spec.Pattern is not null && Regex.IsMatch(rawValue, spec.Pattern), _ => throw new ArgumentOutOfRangeException(nameof(spec), spec.Type, null) };