diff --git a/src/dotnet-ylt/Commands/AddCalculatorCommand.cs b/src/dotnet-ylt/Commands/AddCalculatorCommand.cs index 6e18623..85288a9 100644 --- a/src/dotnet-ylt/Commands/AddCalculatorCommand.cs +++ b/src/dotnet-ylt/Commands/AddCalculatorCommand.cs @@ -34,6 +34,10 @@ public async Task OnExecute() if (Namespace != null) { Namespace = Namespace.Replace('/', '.').Replace('\\', '.'); + if (!ValidateNamespace(Namespace)) + { + return 1; + } } XDocument xProj = XDocument.Load(projectFile); diff --git a/src/dotnet-ylt/Commands/AddParserCommand.cs b/src/dotnet-ylt/Commands/AddParserCommand.cs index 83dff36..e8417af 100644 --- a/src/dotnet-ylt/Commands/AddParserCommand.cs +++ b/src/dotnet-ylt/Commands/AddParserCommand.cs @@ -35,6 +35,10 @@ public async Task OnExecute() if (Namespace != null) { Namespace = Namespace.Replace('/', '.').Replace('\\', '.'); + if (!ValidateNamespace(Namespace)) + { + return 1; + } } XDocument xProj = XDocument.Load(projectFile); diff --git a/src/dotnet-ylt/Commands/CommandBase.cs b/src/dotnet-ylt/Commands/CommandBase.cs index 0c1ebca..d1f95aa 100644 --- a/src/dotnet-ylt/Commands/CommandBase.cs +++ b/src/dotnet-ylt/Commands/CommandBase.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using System.Xml.Linq; using YaccLexTools.Utilities; @@ -13,6 +14,21 @@ namespace DotnetYaccLexTools.Commands public abstract class CommandBase { + protected readonly Regex _namespaceRegex = new Regex(@"^[a-zA-Z_][a-zA-Z\d_]*(\.[a-zA-Z_][a-zA-Z\d_]*)*$", RegexOptions.Compiled); + + + protected bool ValidateNamespace(string @namespace) + { + if (!_namespaceRegex.IsMatch(@namespace)) + { + Console.Error.WriteLine("Namespace is not valid."); + return false; + } + + return true; + } + + protected string? GetProjectFile() { var projectFiles = Directory.GetFiles(".", "*.csproj");