diff --git a/DTLib.Dtsod/DTLib.Dtsod.csproj b/DTLib.Dtsod/DTLib.Dtsod.csproj index 0673c4e..a7c8734 100644 --- a/DTLib.Dtsod/DTLib.Dtsod.csproj +++ b/DTLib.Dtsod/DTLib.Dtsod.csproj @@ -2,7 +2,7 @@ DTLib.Dtsod - 1.0.2 + 1.0.3 Timerix Definitely not json GIT @@ -33,7 +33,7 @@ - + diff --git a/DTLib.Logging/DTLib.Logging.csproj b/DTLib.Logging/DTLib.Logging.csproj index 31547aa..6ff1c66 100644 --- a/DTLib.Logging/DTLib.Logging.csproj +++ b/DTLib.Logging/DTLib.Logging.csproj @@ -2,7 +2,7 @@ DTLib.Logging - 1.0.6 + 1.0.7 Timerix Loggers with dependency injection GIT @@ -30,7 +30,7 @@ - + diff --git a/DTLib.Network/DTLib.Network.csproj b/DTLib.Network/DTLib.Network.csproj index 4a23456..1642e7c 100644 --- a/DTLib.Network/DTLib.Network.csproj +++ b/DTLib.Network/DTLib.Network.csproj @@ -2,7 +2,7 @@ DTLib.Network - 1.0.2 + 1.0.3 Timerix Some sick network protocols GIT @@ -32,6 +32,6 @@ - + diff --git a/DTLib.Tests/DTLib.Tests.csproj b/DTLib.Tests/DTLib.Tests.csproj index 3d276d9..2a6f9b5 100644 --- a/DTLib.Tests/DTLib.Tests.csproj +++ b/DTLib.Tests/DTLib.Tests.csproj @@ -27,14 +27,12 @@ - + + - - - diff --git a/DTLib/ColoredConsole.cs b/DTLib/Console/ColoredConsole.cs similarity index 71% rename from DTLib/ColoredConsole.cs rename to DTLib/Console/ColoredConsole.cs index 3e56569..f899197 100644 --- a/DTLib/ColoredConsole.cs +++ b/DTLib/Console/ColoredConsole.cs @@ -1,4 +1,4 @@ -namespace DTLib; +namespace DTLib.Console; // // вывод и ввод цветного текста в консоли @@ -32,9 +32,9 @@ public static class ColoredConsole public static void Write(ConsoleColor color,string msg) { - Console.ForegroundColor = color; - Console.Write(msg); - Console.ForegroundColor = ConsoleColor.Gray; + System.Console.ForegroundColor = color; + System.Console.Write(msg); + System.Console.ForegroundColor = ConsoleColor.Gray; } public static void Write(string msg) => Write(ConsoleColor.Gray, msg); @@ -53,18 +53,18 @@ public static class ColoredConsole for (ushort i = 0; i < input.Length; i++) { - Console.ForegroundColor = ParseColor(input[i++]); - Console.Write(input[i]); + System.Console.ForegroundColor = ParseColor(input[i++]); + System.Console.Write(input[i]); } - Console.ForegroundColor = ConsoleColor.Gray; + System.Console.ForegroundColor = ConsoleColor.Gray; } - public static void WriteLine() => Console.WriteLine(); + public static void WriteLine() => System.Console.WriteLine(); public static void WriteLine(ConsoleColor color,string msg) { - Console.ForegroundColor = color; - Console.WriteLine(msg); - Console.ForegroundColor = ConsoleColor.Gray; + System.Console.ForegroundColor = color; + System.Console.WriteLine(msg); + System.Console.ForegroundColor = ConsoleColor.Gray; } public static void WriteLine(params string[] input) @@ -76,9 +76,9 @@ public static class ColoredConsole // ввод цветного текста public static string Read(ConsoleColor color) { - Console.ForegroundColor = color; - var r = Console.ReadLine(); - Console.ForegroundColor = ConsoleColor.Gray; + System.Console.ForegroundColor = color; + var r = System.Console.ReadLine(); + System.Console.ForegroundColor = ConsoleColor.Gray; return r; } diff --git a/DTLib/Console/LaunchArgument.cs b/DTLib/Console/LaunchArgument.cs new file mode 100644 index 0000000..82d80e6 --- /dev/null +++ b/DTLib/Console/LaunchArgument.cs @@ -0,0 +1,41 @@ +namespace DTLib.Console; + +#nullable enable +public class LaunchArgument +{ + public string[] Aliases; + public string Description; + public string? ParamName; + public Action? Handler; + public Action? HandlerWithArg; + + private LaunchArgument(string[] aliases, string description) + { + Aliases = aliases; + Description = description; + } + + public LaunchArgument(string[] aliases, string description, Action handler) + : this(aliases, description) => Handler = handler; + + public LaunchArgument(string[] aliases, string description, Action handler, string paramName) + : this(aliases, description) + { + HandlerWithArg = handler; + ParamName = paramName; + } + + public StringBuilder AppendHelpInfo(StringBuilder b) + { + 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); + return b; + } + + public override string ToString() => + $"{{{{{Aliases.MergeToString(", ")}}}, Handler: {Handler is null}, HandlerWithArg: {HandlerWithArg is null}}}"; +} \ No newline at end of file diff --git a/DTLib/Console/LaunchArgumentParser.cs b/DTLib/Console/LaunchArgumentParser.cs new file mode 100644 index 0000000..3d89fd0 --- /dev/null +++ b/DTLib/Console/LaunchArgumentParser.cs @@ -0,0 +1,110 @@ +namespace DTLib.Console; + +public class LaunchArgumentParser +{ + private Dictionary argDict = new(); + private List argList = new(); + public bool ExitIfNoArgs = true; + + public class ExitAfterHelpException : Exception + { + internal ExitAfterHelpException() : base("your program can use this exception to exit after displaying help message") + { } + } + + public string CreateHelpMessage() + { + StringBuilder b = new(); + foreach (var arg in argList) + arg.AppendHelpInfo(b).Append('\n'); + b.Remove(b.Length-1, 1); + return b.ToString(); + } + public string CreateHelpArgMessage(string argAlias) + { + StringBuilder b = new(); + var arg = Get(argAlias); + arg.AppendHelpInfo(b); + return b.ToString(); + } + private void HelpHandler() + { + System.Console.WriteLine(CreateHelpMessage()); + throw new ExitAfterHelpException(); + } + + private void HelpArgHandler(string argAlias) + { + System.Console.WriteLine(CreateHelpArgMessage(argAlias)); + throw new ExitAfterHelpException(); + } + + + public LaunchArgumentParser() + { + var help = new LaunchArgument(new[] { "h", "help" }, + "shows help message", HelpHandler); + Add(help); + var helpArg = new LaunchArgument( new[]{ "ha", "helparg" }, + "shows help message for particular argument", + HelpArgHandler, "argAlias"); + Add(helpArg); + } + + public LaunchArgumentParser(ICollection arguments) : this() + { + foreach (var arg in arguments) + Add(arg); + } + public LaunchArgumentParser(params LaunchArgument[] arguments) : this() + { + for (var i = 0; i < arguments.Length; i++) + Add(arguments[i]); + } + + public void Add(LaunchArgument arg) + { + argList.Add(arg); + for(int a=0; aprogram launch args + /// argument {args[i]} should have a parameter after it + /// argument hasn't got any handlers + /// happens after help message is displayed + public void ParseAndHandle(string[] args) + { + if (args.Length == 0 && ExitIfNoArgs) + { + HelpHandler(); + } + + for (int i = 0; i < args.Length; i++) + { + LaunchArgument arg = Get(args[i]); + + if (arg.HandlerWithArg is not null) + { + if (i+1 >= args.Length) + throw new Exception($"argument <{args[i]}> should have a parameter after it"); + 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"); + } + } +} \ No newline at end of file diff --git a/DTLib/DependencyResolver.cs b/DTLib/DependencyResolver.cs index 6dc174f..3bc3c89 100644 --- a/DTLib/DependencyResolver.cs +++ b/DTLib/DependencyResolver.cs @@ -1,5 +1,4 @@ using System.Runtime.InteropServices; -using System.Threading; namespace DTLib; diff --git a/DTLib/Logging/ConsoleLogger.cs b/DTLib/Logging/ConsoleLogger.cs index 35933ac..a0b15d2 100644 --- a/DTLib/Logging/ConsoleLogger.cs +++ b/DTLib/Logging/ConsoleLogger.cs @@ -1,4 +1,6 @@ -namespace DTLib.Logging; +using DTLib.Console; + +namespace DTLib.Logging; // вывод лога в консоль и файл public class ConsoleLogger : FileLogger @@ -23,7 +25,7 @@ public class ConsoleLogger : FileLogger lock (consolelocker) { ColoredConsole.Write(msg); - Console.WriteLine(); + System.Console.WriteLine(); } } } diff --git a/pack.sh b/pack.sh index 95cc2f6..c506ed4 100644 --- a/pack.sh +++ b/pack.sh @@ -1,6 +1,7 @@ #!/usr/bin/bash set -ex rm -rf nuget +mkdir nuget dotnet pack -c Release -o ./nuget/ rm ./nuget/DTLib.Tests.* ls nuget