diff --git a/DTLib/Console/LaunchArgument.cs b/DTLib/Console/LaunchArgument.cs index 82d80e6..d437267 100644 --- a/DTLib/Console/LaunchArgument.cs +++ b/DTLib/Console/LaunchArgument.cs @@ -8,18 +8,20 @@ public class LaunchArgument public string? ParamName; public Action? Handler; public Action? 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 handler, string paramName) - : this(aliases, description) + public LaunchArgument(string[] aliases, string description, Action handler, string paramName, int priority=0) + : this(aliases, description, priority) { HandlerWithArg = handler; ParamName = paramName; diff --git a/DTLib/Console/LaunchArgumentParser.cs b/DTLib/Console/LaunchArgumentParser.cs index 3d89fd0..3240469 100644 --- a/DTLib/Console/LaunchArgumentParser.cs +++ b/DTLib/Console/LaunchArgumentParser.cs @@ -87,10 +87,9 @@ public class LaunchArgumentParser /// happens after help message is displayed 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!(); } } } \ No newline at end of file diff --git a/DTLib/DTLib.csproj b/DTLib/DTLib.csproj index d24de62..595e31b 100644 --- a/DTLib/DTLib.csproj +++ b/DTLib/DTLib.csproj @@ -2,7 +2,7 @@ DTLib - 1.0.2 + 1.0.3 Timerix Library for all my C# projects GIT