utils for JSON files and RNG/rolling, abstracted arg parsing for commands

This commit is contained in:
Kyle Belanger
2024-02-18 23:11:58 -05:00
parent d2552b74b7
commit 7445a1f5d8
9 changed files with 232 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ public class commandHandler : Attribute
Description = description;
Example = example;
}
}
[AttributeUsage(AttributeTargets.Property)]
@@ -48,6 +49,22 @@ public abstract class Command
prop.SetValue(this, arg.Value);
}
}
protected T Parse<T>(string? value, T fallback = default)
{
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
if (tryParseMethod != null)
{
var parameters = new object[] { value, null };
bool success = (bool)tryParseMethod.Invoke(null, parameters);
if (success)
return (T)parameters[1];
}
return fallback;
}
}
public static class CommandHandler