LaunchArgumentParser

This commit is contained in:
2023-02-03 20:46:01 +06:00
parent 702bc25dee
commit 66c53ef815
10 changed files with 178 additions and 27 deletions

View File

@@ -0,0 +1,86 @@
namespace DTLib.Console;
//
// вывод и ввод цветного текста в консоли
// работает медленнее чем хотелось бы
//
public static class ColoredConsole
{
// парсит название цвета в ConsoleColor
public static ConsoleColor ParseColor(string color) => color switch
{
//case "magneta":
"m" => ConsoleColor.Magenta,
//case "green":
"g" => ConsoleColor.Green,
//case "red":
"r" => ConsoleColor.Red,
//case "yellow":
"y" => ConsoleColor.Yellow,
//case "white":
"w" => ConsoleColor.White,
//case "blue":
"b" => ConsoleColor.Blue,
//case "cyan":
"c" => ConsoleColor.Cyan,
//case "h":
"h" or "gray" => ConsoleColor.Gray,
//case "black":
"black" => ConsoleColor.Black,
_ => throw new Exception($"ColoredConsole.ParseColor({color}) error: incorrect color"),
};
public static void Write(ConsoleColor color,string msg)
{
System.Console.ForegroundColor = color;
System.Console.Write(msg);
System.Console.ForegroundColor = ConsoleColor.Gray;
}
public static void Write(string msg) => Write(ConsoleColor.Gray, msg);
// вывод цветного текста
public static void Write(params string[] input)
{
if (input.Length == 1)
{
Write(input[0]);
return;
}
if (input.Length % 2 != 0)
throw new Exception("ColoredConsole.Write() error: every text string must have color string before");
for (ushort i = 0; i < input.Length; i++)
{
System.Console.ForegroundColor = ParseColor(input[i++]);
System.Console.Write(input[i]);
}
System.Console.ForegroundColor = ConsoleColor.Gray;
}
public static void WriteLine() => System.Console.WriteLine();
public static void WriteLine(ConsoleColor color,string msg)
{
System.Console.ForegroundColor = color;
System.Console.WriteLine(msg);
System.Console.ForegroundColor = ConsoleColor.Gray;
}
public static void WriteLine(params string[] input)
{
Write(input);
WriteLine();
}
// ввод цветного текста
public static string Read(ConsoleColor color)
{
System.Console.ForegroundColor = color;
var r = System.Console.ReadLine();
System.Console.ForegroundColor = ConsoleColor.Gray;
return r;
}
public static string Read(string color) => Read(ParseColor(color));
}

View File

@@ -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<string>? 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<string> 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}}}";
}

View File

@@ -0,0 +1,110 @@
namespace DTLib.Console;
public class LaunchArgumentParser
{
private Dictionary<string, LaunchArgument> argDict = new();
private List<LaunchArgument> 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<LaunchArgument> 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; a<arg.Aliases.Length; a++)
argDict.Add(arg.Aliases[a], arg);
}
public LaunchArgument Get(string argAlias)
{
// different argument providing patterns
if (!argDict.TryGetValue(argAlias, out var arg) && // arg
!(argAlias.StartsWith("--") && argDict.TryGetValue(argAlias.Substring(2), out arg)) && // --arg
!(argAlias.StartsWith('-') && argDict.TryGetValue(argAlias.Substring(1), out arg)) && // -arg
!(argAlias.StartsWith('/') && argDict.TryGetValue(argAlias.Substring(1), out arg))) // /arg
throw new Exception($"invalid argument: {argAlias}\n{CreateHelpMessage()}");
return arg;
}
/// <param name="args">program launch args</param>
/// <exception cref="Exception">argument {args[i]} should have a parameter after it</exception>
/// <exception cref="NullReferenceException">argument hasn't got any handlers</exception>
/// <exception cref="ExitAfterHelpException">happens after help message is displayed</exception>
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");
}
}
}