Skip to content

Commit

Permalink
Added namespace parameter validation. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ernstc committed Nov 18, 2023
1 parent c123314 commit 69fcb57
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/dotnet-ylt/Commands/AddCalculatorCommand.cs
Expand Up @@ -34,6 +34,10 @@ public async Task<int> OnExecute()
if (Namespace != null)
{
Namespace = Namespace.Replace('/', '.').Replace('\\', '.');
if (!ValidateNamespace(Namespace))
{
return 1;
}
}

XDocument xProj = XDocument.Load(projectFile);
Expand Down
4 changes: 4 additions & 0 deletions src/dotnet-ylt/Commands/AddParserCommand.cs
Expand Up @@ -35,6 +35,10 @@ public async Task<int> OnExecute()
if (Namespace != null)
{
Namespace = Namespace.Replace('/', '.').Replace('\\', '.');
if (!ValidateNamespace(Namespace))
{
return 1;
}
}

XDocument xProj = XDocument.Load(projectFile);
Expand Down
16 changes: 16 additions & 0 deletions src/dotnet-ylt/Commands/CommandBase.cs
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using YaccLexTools.Utilities;

Expand All @@ -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");
Expand Down

0 comments on commit 69fcb57

Please sign in to comment.