Skip to content

006. CLI‐Command Router

Alex Hagl edited this page Nov 24, 2023 · 2 revisions

How to use it

To register a new command, make sure it is present in the AvailableCommands Enum. Then implement a Controller for the command:

[SessionOnly]
public class FooController : IController {

    public ExitCondition Handle(string[] args) {
        // Your code goes here
    }
}

The SessionOnly attribute secures the controller, so the user has to be logged in into an active session. To gain further information about the user, inject the CliEnvironment class into your controller via DI.

To register your controller go to the RunnerExtensions file and first add your Controller to DI:

services.AddTransient<FooController>();

After that map the controller to the command enum

runner.MapCommand<FooController>(AvailableCommands.FOO);

How it works

The controller type gets added into an Array, storing all controllers indexed by their enum value. When a user inputs a command, it gets parsed, the type gets fetched from the array, the controller gets instantiated by the ServiceProvider and finally the Controller Handle function gets executed.

If the commmand entered is not specified, the command parser defaults to AvailableCommands.HELP

UML Diagram of the CLI architecture

Clone this wiki locally