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 string? ParamName;
public Action? Handler; public Action? Handler;
public Action<string>? HandlerWithArg; public Action<string>? HandlerWithArg;
public int Priority;
private LaunchArgument(string[] aliases, string description) private LaunchArgument(string[] aliases, string description, int priority)
{ {
Aliases = aliases; Aliases = aliases;
Description = description; Description = description;
Priority = priority;
} }
public LaunchArgument(string[] aliases, string description, Action handler) public LaunchArgument(string[] aliases, string description, Action handler, int priority=0)
: this(aliases, description) => Handler = handler; : this(aliases, description, priority) => Handler = handler;
public LaunchArgument(string[] aliases, string description, Action<string> handler, string paramName) public LaunchArgument(string[] aliases, string description, Action<string> handler, string paramName, int priority=0)
: this(aliases, description) : this(aliases, description, priority)
{ {
HandlerWithArg = handler; HandlerWithArg = handler;
ParamName = paramName; ParamName = paramName;

View File

@ -87,10 +87,9 @@ public class LaunchArgumentParser
/// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception> /// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception>
public void ParseAndHandle(string[] args) public void ParseAndHandle(string[] args)
{ {
// show help and throw
if (args.Length == 0 && ExitIfNoArgs) if (args.Length == 0 && ExitIfNoArgs)
{
HelpHandler(); HelpHandler();
}
for (int i = 0; i < args.Length; i++) for (int i = 0; i < args.Length; i++)
{ {
@ -100,11 +99,18 @@ public class LaunchArgumentParser
{ {
if (i+1 >= args.Length) if (i+1 >= args.Length)
throw new Exception($"argument <{args[i]}> should have a parameter after it"); 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) else if (arg.Handler is null) throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers");
arg.Handler(); }
else 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> <PropertyGroup>
<!--package info--> <!--package info-->
<PackageId>DTLib</PackageId> <PackageId>DTLib</PackageId>
<Version>1.0.2</Version> <Version>1.0.3</Version>
<Authors>Timerix</Authors> <Authors>Timerix</Authors>
<Description>Library for all my C# projects</Description> <Description>Library for all my C# projects</Description>
<RepositoryType>GIT</RepositoryType> <RepositoryType>GIT</RepositoryType>