|
|
|
|
@ -2,135 +2,32 @@ namespace DTLib.Console;
|
|
|
|
|
|
|
|
|
|
public class LaunchArgumentParser
|
|
|
|
|
{
|
|
|
|
|
public string HelpMessageHeader = "USAGE:";
|
|
|
|
|
public bool AllowedNoArguments;
|
|
|
|
|
public bool AllowedUnknownArguments;
|
|
|
|
|
// ReSharper disable once CollectionNeverQueried.Global
|
|
|
|
|
public readonly List<string> UnknownArguments = new();
|
|
|
|
|
public bool IsAllowedNoArguments;
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<string, LaunchArgument> argDict = new();
|
|
|
|
|
private readonly List<LaunchArgument> argList = new();
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser()
|
|
|
|
|
public class ExitAfterHelpException : Exception
|
|
|
|
|
{
|
|
|
|
|
var help = new LaunchArgument(new[] { "h", "help" },
|
|
|
|
|
"shows help message", HelpHandler);
|
|
|
|
|
Add(help);
|
|
|
|
|
var helpArg = new LaunchArgument(new[] { "ha", "helparg" },
|
|
|
|
|
"shows help message for specific argument",
|
|
|
|
|
HelpArgHandler, "argument");
|
|
|
|
|
Add(helpArg);
|
|
|
|
|
internal ExitAfterHelpException() : base("your program can use this exception to exit after displaying help message")
|
|
|
|
|
{ }
|
|
|
|
|
}
|
|
|
|
|
public LaunchArgumentParser(ICollection<LaunchArgument> arguments) : this() => WithArgs(arguments);
|
|
|
|
|
public LaunchArgumentParser(params LaunchArgument[] arguments) : this() => WithArgs(arguments);
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser WithArgs(IEnumerable<LaunchArgument> args)
|
|
|
|
|
{
|
|
|
|
|
foreach (var arg in args)
|
|
|
|
|
Add(arg);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser WithArgs(params LaunchArgument[] args)
|
|
|
|
|
{
|
|
|
|
|
foreach (var arg in args)
|
|
|
|
|
Add(arg);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser WithHelpMessageHeader(string header)
|
|
|
|
|
{
|
|
|
|
|
HelpMessageHeader = header;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser AllowNoArguments()
|
|
|
|
|
{
|
|
|
|
|
AllowedNoArguments = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser AllowUnknownArguments()
|
|
|
|
|
{
|
|
|
|
|
AllowedUnknownArguments = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string CreateHelpMessage()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder b = new(HelpMessageHeader);
|
|
|
|
|
foreach (var arg in argList)
|
|
|
|
|
{
|
|
|
|
|
b.Append('\n');
|
|
|
|
|
arg.AppendHelpInfo(b);
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
if(!TryParseArg(argAlias, out var arg))
|
|
|
|
|
throw new Exception($"unknown argument '{argAlias}'");
|
|
|
|
|
var arg = Parse(argAlias);
|
|
|
|
|
arg.AppendHelpInfo(b);
|
|
|
|
|
return b.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(LaunchArgument arg)
|
|
|
|
|
{
|
|
|
|
|
argList.Add(arg);
|
|
|
|
|
foreach (string alias in arg.Aliases)
|
|
|
|
|
argDict.Add(alias, arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryParseArg(string argAlias, out LaunchArgument arg)
|
|
|
|
|
{
|
|
|
|
|
// different argument providing patterns
|
|
|
|
|
arg = null!;
|
|
|
|
|
return 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
// show help message and throw ExitAfterHelpException
|
|
|
|
|
if (args.Length == 0 && !AllowedNoArguments)
|
|
|
|
|
HelpHandler();
|
|
|
|
|
|
|
|
|
|
List<LaunchArgument> execQueue = new();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!TryParseArg(args[i], out var arg))
|
|
|
|
|
{
|
|
|
|
|
if (!AllowedUnknownArguments)
|
|
|
|
|
throw new Exception($"unknown argument '{args[i]}'");
|
|
|
|
|
UnknownArguments.Add(args[i]);
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < arg.Params.Length; j++)
|
|
|
|
|
{
|
|
|
|
|
if (++i >= args.Length)
|
|
|
|
|
throw new Exception(
|
|
|
|
|
$"argument '{arg.Aliases[0]}' should have parameter '{arg.Params[j]}' after it");
|
|
|
|
|
arg.Params[j].Value = args[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execQueue.Add(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ascending sort by priority
|
|
|
|
|
execQueue.Sort((a0, a1) => a0.Priority - a1.Priority);
|
|
|
|
|
// finally executing handlers
|
|
|
|
|
foreach (var a in execQueue)
|
|
|
|
|
a.Handle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HelpHandler()
|
|
|
|
|
{
|
|
|
|
|
System.Console.WriteLine(CreateHelpMessage());
|
|
|
|
|
@ -143,11 +40,83 @@ public class LaunchArgumentParser
|
|
|
|
|
throw new ExitAfterHelpException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ExitAfterHelpException : Exception
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser()
|
|
|
|
|
{
|
|
|
|
|
public ExitAfterHelpException()
|
|
|
|
|
: base("your program can use this exception to exit after displaying help message")
|
|
|
|
|
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 AllowNoArguments()
|
|
|
|
|
{
|
|
|
|
|
IsAllowedNoArguments = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgumentParser(ICollection<LaunchArgument> arguments) : this()
|
|
|
|
|
{
|
|
|
|
|
foreach (var arg in arguments)
|
|
|
|
|
Add(arg);
|
|
|
|
|
}
|
|
|
|
|
public LaunchArgumentParser(params LaunchArgument[] arguments) : this()
|
|
|
|
|
{
|
|
|
|
|
foreach (var arg in arguments)
|
|
|
|
|
Add(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(LaunchArgument arg)
|
|
|
|
|
{
|
|
|
|
|
argList.Add(arg);
|
|
|
|
|
foreach (string alias in arg.Aliases)
|
|
|
|
|
argDict.Add(alias, arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LaunchArgument Parse(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)
|
|
|
|
|
{
|
|
|
|
|
// show help message and throw ExitAfterHelpException
|
|
|
|
|
if (args.Length == 0 && !IsAllowedNoArguments)
|
|
|
|
|
HelpHandler();
|
|
|
|
|
|
|
|
|
|
List<LaunchArgument> execQueue = new();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
LaunchArgument arg = Parse(args[i]);
|
|
|
|
|
for (int j = 0; j < arg.Params.Length; j++)
|
|
|
|
|
{
|
|
|
|
|
if (++i >= args.Length)
|
|
|
|
|
throw new Exception($"argument '{arg.Aliases[0]}' should have parameter '{arg.Params[j]}' after it");
|
|
|
|
|
arg.Params[j].Value = args[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execQueue.Add(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ascending sort by priority
|
|
|
|
|
execQueue.Sort((a0, a1) => a0.Priority-a1.Priority);
|
|
|
|
|
// finally executing handlers
|
|
|
|
|
foreach (var a in execQueue)
|
|
|
|
|
a.Handle();
|
|
|
|
|
}
|
|
|
|
|
}
|