Initial Commit
This commit is contained in:
commit
7b8550ee0f
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
/packages/
|
||||||
|
riderModule.iml
|
||||||
|
/_ReSharper.Caches/
|
13
.idea/.idea.GlaxArguments/.idea/.gitignore
generated
vendored
Normal file
13
.idea/.idea.GlaxArguments/.idea/.gitignore
generated
vendored
Normal file
@ -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
|
4
.idea/.idea.GlaxArguments/.idea/encodings.xml
generated
Normal file
4
.idea/.idea.GlaxArguments/.idea/encodings.xml
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
</project>
|
8
.idea/.idea.GlaxArguments/.idea/indexLayout.xml
generated
Normal file
8
.idea/.idea.GlaxArguments/.idea/indexLayout.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="UserContentModel">
|
||||||
|
<attachedFolders />
|
||||||
|
<explicitIncludes />
|
||||||
|
<explicitExcludes />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/.idea.GlaxArguments/.idea/vcs.xml
generated
Normal file
6
.idea/.idea.GlaxArguments/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
22
GlaxArguments.sln
Normal file
22
GlaxArguments.sln
Normal file
@ -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
|
53
GlaxArguments/Argument.cs
Normal file
53
GlaxArguments/Argument.cs
Normal file
@ -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 = "<arg> ";
|
||||||
|
return $"{string.Join('\n', Flags)}\t{new StringBuilder(arg.Length * ParameterCount).Insert(0, arg, ParameterCount)}\t{Description}";
|
||||||
|
}
|
||||||
|
}
|
58
GlaxArguments/ArgumentFetcher.cs
Normal file
58
GlaxArguments/ArgumentFetcher.cs
Normal file
@ -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<Argument> 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<Argument, string[]> Fetch(string argumentString)
|
||||||
|
{
|
||||||
|
Dictionary<Argument, string[]> 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<string> 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;
|
||||||
|
}
|
||||||
|
}
|
9
GlaxArguments/GlaxArguments.csproj
Normal file
9
GlaxArguments/GlaxArguments.csproj
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
13
Test/Program.cs
Normal file
13
Test/Program.cs
Normal file
@ -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<Argument, string[]> fetched = fetcher.Fetch("-h");
|
||||||
|
foreach(KeyValuePair<Argument, string[]> arg in fetched)
|
||||||
|
Console.WriteLine($"{arg.Key.Flags[0]} - {arg.Key.Description}\n\t{string.Join("\n", arg.Value)}");
|
14
Test/Test.csproj
Normal file
14
Test/Test.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\GlaxArguments\GlaxArguments.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user