CLI - Command Line Interface App
Writing a CLI app is very, very straightforward with H.Necessaire
A CLI application is an app that runs in the terminal of the OS and its user interaction is done via written commands and arguments. Like git, dotnet, ping, npm, node, etc.
With H.Necessaire, implementing a CLI is extremely easy.
The Very Basic
dotnet add package H.Necessaire.CLI
internal class Program
{
static async Task Main(string[] args)
{
await new CliApp()
.WithEverything()
.Run()
;
}
}That's it! โ .
This will give you:
- Obviously, the building blocks for implementing your own custom commands
- terminal command execution runtime (e.g.:
myapp.exe ping necessaire.dev) - command interpreter runtime, when no explicit arguments are provided
- autocomplete in command interpreter (
TAB/SHIFT + TAB) - help in command interpreter (
helpcommand will display known commands with known usage details) - a few other out-of-the-box commands
Custom command implementation
internal class HelloWorldCommand : CommandBase
{
public override Task<OperationResult> Run()
{
Console.WriteLine("Hello from the lovely H.Necessaire CLI ๐!");
return OperationResult.Win().AsTask();
}
}Doesn't get easier than this
That's it! โ
As you can see, implementing your own custom command is super easy, you create a class that derives from CommandBase, you implement the Run() method and done.
The command will be automatically registered an available both as command execution (myapp.exe helloworld) or via the command interpreter ((> helloworld). It will also be presented in the help command result and will be part of the autocomplete suggestions.