From 48eab00a373c5e97502e738ac34b8b97649ebbe6 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 22 Mar 2023 03:08:56 +0600 Subject: [PATCH] LaunchArgument handler(string,string) --- DTLib/Console/LaunchArgument.cs | 45 ++++++++++++++++++++------- DTLib/Console/LaunchArgumentParser.cs | 33 +++++++++++++++----- DTLib/DTLib.csproj | 2 +- 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/DTLib/Console/LaunchArgument.cs b/DTLib/Console/LaunchArgument.cs index d437267..57a5a65 100644 --- a/DTLib/Console/LaunchArgument.cs +++ b/DTLib/Console/LaunchArgument.cs @@ -5,9 +5,12 @@ public class LaunchArgument { public string[] Aliases; public string Description; - public string? ParamName; + protected string? ParamName1; + protected string? ParamName2; public Action? Handler; - public Action? HandlerWithArg; + public Action? HandlerWithArg1; + public Action? HandlerWithArg2; + public int RequiredArgsCount; public int Priority; private LaunchArgument(string[] aliases, string description, int priority) @@ -16,15 +19,31 @@ public class LaunchArgument Description = description; Priority = priority; } - - 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, int priority=0) + public LaunchArgument(string[] aliases, string description, + Action handler, int priority = 0) : this(aliases, description, priority) { - HandlerWithArg = handler; - ParamName = paramName; + Handler = handler; + RequiredArgsCount = 0; + } + + public LaunchArgument(string[] aliases, string description, + Action handler, string paramName1, int priority=0) + : this(aliases, description, priority) + { + HandlerWithArg1 = handler; + ParamName1 = paramName1; + RequiredArgsCount = 1; + } + public LaunchArgument(string[] aliases, string description, + Action handler, string paramName1, string paramName2, int priority=0) + : this(aliases, description, priority) + { + HandlerWithArg2 = handler; + ParamName1 = paramName1; + ParamName2 = paramName2; + RequiredArgsCount = 2; } public StringBuilder AppendHelpInfo(StringBuilder b) @@ -32,12 +51,14 @@ public class LaunchArgument b.Append(Aliases[0]); for (int i = 1; i < Aliases.Length; i++) b.Append(", ").Append(Aliases[i]); - if (!String.IsNullOrEmpty(ParamName)) - b.Append(" [").Append(ParamName).Append(']'); - b.Append(" - ").Append(Description); + if (!string.IsNullOrEmpty(ParamName1)) + b.Append(" [").Append(ParamName1).Append("] "); + if (!string.IsNullOrEmpty(ParamName2)) + b.Append(" [").Append(ParamName2).Append("] "); + b.Append("- ").Append(Description); return b; } public override string ToString() => - $"{{{{{Aliases.MergeToString(", ")}}}, Handler: {Handler is null}, HandlerWithArg: {HandlerWithArg is null}}}"; + $"{{{{{Aliases.MergeToString(", ")}}}, Handler: {Handler is null}, HandlerWithArg: {HandlerWithArg1 is null}}}"; } \ No newline at end of file diff --git a/DTLib/Console/LaunchArgumentParser.cs b/DTLib/Console/LaunchArgumentParser.cs index 14b3bd8..cd779b0 100644 --- a/DTLib/Console/LaunchArgumentParser.cs +++ b/DTLib/Console/LaunchArgumentParser.cs @@ -103,16 +103,33 @@ public class LaunchArgumentParser { LaunchArgument arg = Parse(args[i]); - if (arg.HandlerWithArg is not null) + switch (arg.RequiredArgsCount) { - if (i+1 >= args.Length) - throw new Exception($"argument <{args[i]}> should have a parameter after it"); - i++; // next arg - var i1 = i; - arg.Handler = () => arg.HandlerWithArg(args[i1]); + case 0: + if (arg.Handler is null) + throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers"); + break; + case 1: + { + if (arg.HandlerWithArg1 is null) + throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers"); + if (i + 1 >= args.Length) + throw new Exception($"argument <{args[i]}> should have a parameter after it"); + string arg1 = args[++i]; + arg.Handler = () => arg.HandlerWithArg1(arg1); + break; + } + case 2: + { + if (arg.HandlerWithArg2 is null) + throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers"); + if (i + 2 >= args.Length) + throw new Exception($"argument <{args[i]}> should have two params after it"); + string arg1 = args[++i], arg2 = args[++i]; + arg.Handler = () => arg.HandlerWithArg2(arg1, arg2); + break; + } } - else if (arg.Handler is null) throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers"); - execQueue.Add(arg); } diff --git a/DTLib/DTLib.csproj b/DTLib/DTLib.csproj index d3a3feb..445b909 100644 --- a/DTLib/DTLib.csproj +++ b/DTLib/DTLib.csproj @@ -2,7 +2,7 @@ DTLib - 1.1.7 + 1.1.8 Timerix Library for all my C# projects GIT