LaunchArgument priority

This commit is contained in:
Timerix22 2023-02-04 01:45:32 +06:00
parent b05357ef6f
commit ac1b8ad4f1
3 changed files with 21 additions and 13 deletions

View File

@ -8,18 +8,20 @@ public class LaunchArgument
public string? ParamName;
public Action? Handler;
public Action<string>? HandlerWithArg;
public int Priority;
private LaunchArgument(string[] aliases, string description)
private LaunchArgument(string[] aliases, string description, int priority)
{
Aliases = aliases;
Description = description;
Priority = priority;
}
public LaunchArgument(string[] aliases, string description, Action handler)
: this(aliases, description) => Handler = handler;
public LaunchArgument(string[] aliases, string description, Action handler, int priority=0)
: this(aliases, description, priority) => Handler = handler;
public LaunchArgument(string[] aliases, string description, Action<string> handler, string paramName)
: this(aliases, description)
public LaunchArgument(string[] aliases, string description, Action<string> handler, string paramName, int priority=0)
: this(aliases, description, priority)
{
HandlerWithArg = handler;
ParamName = paramName;

View File

@ -87,10 +87,9 @@ public class LaunchArgumentParser
/// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception>
public void ParseAndHandle(string[] args)
{
if (args.Length == 0 && ExitIfNoArgs)
{
// show help and throw
if (args.Length == 0 && ExitIfNoArgs)
HelpHandler();
}
for (int i = 0; i < args.Length; i++)
{
@ -100,11 +99,18 @@ public class LaunchArgumentParser
{
if (i+1 >= args.Length)
throw new Exception($"argument <{args[i]}> should have a parameter after it");
arg.HandlerWithArg(args[++i]);
i++; // next arg
arg.Handler = () => arg.HandlerWithArg(args[i]);
}
else if (arg.Handler is not null)
arg.Handler();
else throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
else if (arg.Handler is null) throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
}
// ascending sort by priority
argList.Sort((a0, a1) => a0.Priority-a1.Priority);
// finally executing handlers
foreach (var a in argList)
{
a.Handler!();
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<!--package info-->
<PackageId>DTLib</PackageId>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Authors>Timerix</Authors>
<Description>Library for all my C# projects</Description>
<RepositoryType>GIT</RepositoryType>