LaunchArgument handler(string,string)
This commit is contained in:
parent
a4014c0b7f
commit
48eab00a37
@ -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<string>? HandlerWithArg;
|
||||
public Action<string>? HandlerWithArg1;
|
||||
public Action<string, string>? HandlerWithArg2;
|
||||
public int RequiredArgsCount;
|
||||
public int Priority;
|
||||
|
||||
private LaunchArgument(string[] aliases, string description, int priority)
|
||||
@ -17,14 +20,30 @@ public class LaunchArgument
|
||||
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<string> 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<string> handler, string paramName1, int priority=0)
|
||||
: this(aliases, description, priority)
|
||||
{
|
||||
HandlerWithArg1 = handler;
|
||||
ParamName1 = paramName1;
|
||||
RequiredArgsCount = 1;
|
||||
}
|
||||
public LaunchArgument(string[] aliases, string description,
|
||||
Action<string, string> 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(']');
|
||||
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}}}";
|
||||
}
|
||||
@ -103,16 +103,33 @@ public class LaunchArgumentParser
|
||||
{
|
||||
LaunchArgument arg = Parse(args[i]);
|
||||
|
||||
if (arg.HandlerWithArg is not null)
|
||||
switch (arg.RequiredArgsCount)
|
||||
{
|
||||
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");
|
||||
i++; // next arg
|
||||
var i1 = i;
|
||||
arg.Handler = () => arg.HandlerWithArg(args[i1]);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>DTLib</PackageId>
|
||||
<Version>1.1.7</Version>
|
||||
<Version>1.1.8</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>Library for all my C# projects</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user