commit 7b8550ee0fba041c7fa4ffddb72d8c88b5ceeb45 Author: glax Date: Sat Jun 1 23:33:47 2024 +0200 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.GlaxArguments/.idea/.gitignore b/.idea/.idea.GlaxArguments/.idea/.gitignore new file mode 100644 index 0000000..f4ea5a7 --- /dev/null +++ b/.idea/.idea.GlaxArguments/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/modules.xml +/contentModel.xml +/.idea.GlaxArguments.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.GlaxArguments/.idea/encodings.xml b/.idea/.idea.GlaxArguments/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.GlaxArguments/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.GlaxArguments/.idea/indexLayout.xml b/.idea/.idea.GlaxArguments/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.GlaxArguments/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.GlaxArguments/.idea/vcs.xml b/.idea/.idea.GlaxArguments/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.GlaxArguments/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GlaxArguments.sln b/GlaxArguments.sln new file mode 100644 index 0000000..fc953e2 --- /dev/null +++ b/GlaxArguments.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlaxArguments", "GlaxArguments\GlaxArguments.csproj", "{AD75545B-4371-4F15-832C-3501A82655CC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{078D9A7E-8CEB-457C-85F2-8F630245D5C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AD75545B-4371-4F15-832C-3501A82655CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD75545B-4371-4F15-832C-3501A82655CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD75545B-4371-4F15-832C-3501A82655CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD75545B-4371-4F15-832C-3501A82655CC}.Release|Any CPU.Build.0 = Release|Any CPU + {078D9A7E-8CEB-457C-85F2-8F630245D5C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {078D9A7E-8CEB-457C-85F2-8F630245D5C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {078D9A7E-8CEB-457C-85F2-8F630245D5C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {078D9A7E-8CEB-457C-85F2-8F630245D5C6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/GlaxArguments/Argument.cs b/GlaxArguments/Argument.cs new file mode 100644 index 0000000..207f92d --- /dev/null +++ b/GlaxArguments/Argument.cs @@ -0,0 +1,53 @@ +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace GlaxArguments; + +public struct Argument +{ + [Required] + public string[] Flags; + [Required] + public string? Description; + [Required] + public int ParameterCount; + + public Argument(string[] flags, int parameterCount = 0, string? description = null) + { + if (flags.Length < 1) + throw new ArgumentException( "Missing flags for Argument."); + foreach(string flag in flags) + if(flag == "--") + throw new ArgumentException("Flag can not be named '--'."); + else if(flag.StartsWith("--") && flag.Length < 4) + throw new ArgumentException("Flags one character can only have one dash."); + else if (flag.StartsWith("-") && !flag.StartsWith("--") && flag.Length != 2) + throw new ArgumentException("Flags with more than one character need to have two dashes."); + this.Flags = flags; + if (parameterCount < 0) + throw new ArgumentException("Argument can not have less that 0 parameters."); + this.ParameterCount = parameterCount; + this.Description = description; + } + + public override bool Equals(object? obj) + { + return obj is Argument a && Equals(a); + } + + public bool Equals(Argument other) + { + return Flags.Equals(other.Flags); + } + + public override int GetHashCode() + { + return Flags.GetHashCode(); + } + + public override string ToString() + { + string arg = " "; + return $"{string.Join('\n', Flags)}\t{new StringBuilder(arg.Length * ParameterCount).Insert(0, arg, ParameterCount)}\t{Description}"; + } +} \ No newline at end of file diff --git a/GlaxArguments/ArgumentFetcher.cs b/GlaxArguments/ArgumentFetcher.cs new file mode 100644 index 0000000..6436eff --- /dev/null +++ b/GlaxArguments/ArgumentFetcher.cs @@ -0,0 +1,58 @@ +namespace GlaxArguments; + +public class ArgumentFetcher +{ + public Argument[] ValidArguments; + + private static readonly Argument HelpArg = new(["-h", "--help"], 0, "Print this help text."); + + public ArgumentFetcher(IEnumerable arguments) + { + Argument[] temp = arguments as Argument[] ?? arguments.ToArray(); + temp = temp.DistinctBy(t => t.Flags).ToArray(); + if(temp.Length != arguments.Count()) + throw new ArgumentException("Can not have duplicate flags."); + ValidArguments = temp.Concat(new[] { HelpArg }).ToArray(); + } + + public Dictionary Fetch(string argumentString) + { + Dictionary ret = new(); + string[] parts = argumentString.Split(' '); + for (int i = 0; i < parts.Length; i++) + { + string flag = parts[i]; + if(flag.Length < 1) + continue; + if (!(flag.StartsWith('-') && flag.Length == 2 || flag.StartsWith("--"))) + throw new ArgumentException($"{flag} is not a flag. Enter -h for a list of all valid flags."); + if (!ValidArguments.Any(argument => argument.Flags.Contains(flag))) + throw new ArgumentNullException($"Flag {flag} does not exist."); + Argument argument = ValidArguments.First(argument => argument.Flags.Contains(flag)); + if (argument.Equals(HelpArg)) + { + ret.Clear(); + ret.Add(argument, ValidArguments.Select(arg => arg.ToString()).ToArray()); + return ret; + } + + int lastParameterIndex = i + argument.ParameterCount; + if(lastParameterIndex >= parts.Length) + throw new ArgumentException($"Not enough Parameters provided for flag {flag}. (Expected {argument.ParameterCount})"); + + List parameters = new(); + for (; i < lastParameterIndex; i++) + { + string param = parts[i + 1]; + if(param.StartsWith('-')) + throw new ArgumentException($"Not enough Parameters provided for flag {flag}. (Expected {argument.ParameterCount})"); + if(param.StartsWith('"') && param.EndsWith('"')) + parameters.Add(param.Substring(1, param.Length - 2)); + else + parameters.Add(param); + } + ret.Add(argument, parameters.ToArray()); + } + return ret; + } +} \ No newline at end of file diff --git a/GlaxArguments/GlaxArguments.csproj b/GlaxArguments/GlaxArguments.csproj new file mode 100644 index 0000000..3a63532 --- /dev/null +++ b/GlaxArguments/GlaxArguments.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/Test/Program.cs b/Test/Program.cs new file mode 100644 index 0000000..90f1639 --- /dev/null +++ b/Test/Program.cs @@ -0,0 +1,13 @@ +using GlaxArguments; + +Argument[] arguments = +[ + new (["-t", "--test"], 0, "Test arg"), + new (["--test1"], 1, "Test arg with 1 parameter"), + new (["--test2"], 2, "Test arg with 2 parameters"), + new (["--test3"], 0, "Test arg") +]; +ArgumentFetcher fetcher = new(arguments); +Dictionary fetched = fetcher.Fetch("-h"); +foreach(KeyValuePair arg in fetched) + Console.WriteLine($"{arg.Key.Flags[0]} - {arg.Key.Description}\n\t{string.Join("\n", arg.Value)}"); \ No newline at end of file diff --git a/Test/Test.csproj b/Test/Test.csproj new file mode 100644 index 0000000..4a2441a --- /dev/null +++ b/Test/Test.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + +