monorepo/utils/plate/ArgParser.cs

76 lines
1.7 KiB
C#

using System;
using System.Linq;
using System.Reflection;
namespace Plate;
class Flag : Attribute {
public string longName;
public string shortname;
public Flag(string longName, string shortname)
{
this.longName = longName;
this.shortname = shortname;
System.Console.WriteLine($"--{longName} -{shortname}");
}
}
class Command : Attribute {}
class CommandMethod : Attribute {}
[Command]
class TestClass {
[CommandMethod]
[Flag("verbose", "v")]
public void help()
{
}
}
class ArgParser
{
private readonly string[] args;
public ArgParser(string[] args)
{
// Match these args with the commands and their flags
this.args = args;
// Find all the commands via Command attribute
var commands = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetCustomAttributes(false).Any(a => a is Command)
select t;
foreach (Type t in commands)
{
var commandMethods = from m in t.GetMethods()
where m.GetCustomAttributes(false).Any(a => a is CommandMethod)
select m;
var flags = from m in t.GetMethods()
where m.GetCustomAttributes(false).Any(a => a is Flag && a is CommandMethod)
select m;
var instance = Activator.CreateInstance(t);
foreach (MethodInfo mInfo in commandMethods)
{
foreach (MethodInfo wFlag in flags)
{
if(wFlag.Name == mInfo.Name){
System.Console.WriteLine("Flags: ");
}
}
mInfo.Invoke(instance, new object[0]);
}
}
}
}