A system for calling funcions via text input (made for Unity, can be used outside)
- install Command Sytem package (https://github.com/BarionLP/CommandSystem.git)
CommandManager.Execute("command");CommandManager.SetLogger(newUnityDebugCommandLogger());// print logs in the unity console// Custom LoggerpublicclassCustomLogger:ICommandLogger{publicvoidLog(stringmessage)=>Debug.Log(message);publicvoidLogWarning(stringmessage)=>Debug.LogWarning(message);publicvoidLogError(stringmessage)=>Debug.LogError(message);}CommandManager.Commands.Register(newSimpleCommand("test",()->
{// Do something// LoggingCommandManager.Log();
CommandManager.LogWarning();
CommandManager.LogError();}));// register static methods with the Command attribute// automatically generates syntax hintsCommandManager.Commands.Register<TestCommands>();// register command "cmd" with the methods as subcommands (e.g. "cmd add 4 2")CommandManager.Commands.RegisterGroup<TestCommands>("cmd");publicclassTestCommands{[Command("add")]// automatically parses primitive argumentspublicstaticvoidAdd(intleft,intright)=>CommandManager.Log($"{left} + {right} is {left+right}");[Command("wait")]// supports asyncpublicstaticasyncTaskTest(floatseconds)=>awaitTask.Delay((int)(seconds*1000));[Command("give")]// other types require custom parsers (see below)publicstaticvoidGiveItem(Itemitem)=>Player.Give(item);}// has built in parsers for most primitives (float, int, long, short, ...) see `ArgumentParser.cs`ArgumentParsers.Register<CustomType>(newCustomParser());// register a custom parserArgumentParsers.Register<CustomEnum>(newEnumArgumentParser<CustomEnum>());// generates an enum parser (parses by name)publicclassCustomParser:IArgumentParser<CustomType>{publicCustomTypeParse(ReadOnlySpan<char>raw)=>// however you want, e.g. from a registry (`null` if fails)publicIEnumerable<string> GetSuggestions()=>Enumerable.Empty<string>();// or all possible values}