DEV Community

Cover image for How to build an interactive command-line application in .NET
Tatsuro Shibamura
Tatsuro Shibamura

Posted on

How to build an interactive command-line application in .NET

Development using Node.js often provides highly interactive command line applications such as vue-cli and create-nuxt-app.

As an example, create-nuxt-app has an interactive interface that allows even beginners to start development right away by simply answering questions.

create-nuxt-app

I find this interface to be very beautiful and easy to use.

On the other hand, C# / .NET developers are provided with a powerful integrated development environment called Visual Studio, so they do not need to operate on the command line to start development, but Windows Terminal and WSL 2 increasingly require a command line interface.

Since there were not many ways to implement an interactive command line interface for C# like Node.js, I developed a library to implement it easily.

GitHub logo shibayan / Sharprompt

Interactive command-line based application framework for C#

Sharprompt

Build Downloads NuGet License

Interactive command-line based application framework for C#

sharprompt

Features

  • Multi-platform support
  • Supports the popular prompts (Input / Password / Select / etc)
  • Supports model-based prompts
  • Validation of input value
  • Automatic generation of data source using Enum type
  • Customizable symbols and color schema
  • Unicode support (Multi-byte characters and Emoji😀🎉)

Installation

Install-Package Sharprompt
dotnet add package Sharprompt
// Simple input
var name = Prompt.Input<string>("What's your name?")
Console.WriteLine($"Hello, {name}!");

// Password input
var secret = Prompt.Password("Type new password", validators: new[] { Validators.Required(), Validators.MinLength(8) });
Console.WriteLine("Password OK");

// Confirmation
var answer = Prompt.Confirm("Are you ready?", defaultValue: true);
Console.
…
Enter fullscreen mode Exit fullscreen mode

It can be easily installed in a console application project from NuGet.

Install-Package Sharprompt
or
dotnet add package Sharprompt
Enter fullscreen mode Exit fullscreen mode

First, let's create an application with two very simple input fields.

In Sharprompt, all the APIs are defined in the class Prompt, so your application will use only the methods you need. This time we will use Prompt.Input<T> because I need an arbitrary value entered by the user.

class Program
{
    static void Main(string[] args)
    {
        var name = Prompt.Input<string>("What's your name?");
        var number = Prompt.Input<int>("Enter any number");

        Console.WriteLine($"Hello, {name}!");
        Console.WriteLine($"The number entered is {number}");
    }
}
Enter fullscreen mode Exit fullscreen mode

The type parameter specifies the type of the final value required. Sharprompt automatically performs type conversion and error handling according to the specified type.

You now have a console application that allows you to enter a name and an arbitrary number.

Simple application

The APIs include password input, selectable from a list, etc., so that complex console applications can be developed.

As a NET / C# developer, you often want to define input values and validation on a class and attribute-based.

For such use cases, Sharprompt provides an API to create forms automatically on a model-based.

class Program
{
    static void Main(string[] args)
    {
        var model = Prompt.AutoForms<PromptModel>();

        Console.WriteLine($"Hello, {model.Name}!");
        Console.WriteLine($"The number entered is {model.Number}");
    }
}

public class PromptModel
{
    [Display(Prompt = "What's your name?")]
    [Required]
    public string Name { get; set; }

    [Display(Prompt = "Enter any number")]
    [Required]
    [Range(0, 100)]
    public int? Number { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

If you have experience developing with ASP.NET Core, you are probably familiar with this class definition.

If you call Prompt.AutoForms<T> with this class definition, it will automatically create an input form with validation.

Model-based application

Since the validation uses standard .NET attributes, it is also possible to easily add custom validations.

Now you can see how easy it is to development an interactive console application in .NET / C#.

Example: create-function-app

Finally, here is a mock of create-function-app, a CLI tool for Azure Functions with an interface like create-nuxt-app.

Being able to interactively create Azure Functions projects will make it easier to start developing.

create-function-app

This mock source code is here!

GitHub logo shibayan / create-function-app-mock

Sharprompt demo application

If you have any feedback, please feel free to visit GitHub.

Enjoy the development!

Top comments (0)