CLI - Command Line Interface App

Writing a CLI app is very, very straightforward with H.Necessaire

CLI - Command Line Interface App
Photo by Joan Gamell / Unsplash

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

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 (help command 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.

Full sample on GitHub Samples Repo:

H.Necessaire.Usage.Samples/Src/H.Necessaire.Samples/H.Necessaire.Samples.Runtime.CLI.Basic at master ยท hinteadan/H.Necessaire.Usage.Samples
Usage Samples for H.Necessaire Docs WebSite. Contribute to hinteadan/H.Necessaire.Usage.Samples development by creating an account on GitHub.