How to build an interactive command-line application in .NET
Tags: dotnet, csharp, commandline, cli
Published: 2021-08-23 16:27:19

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7s37za7bmnq19f2r7kv.gif)

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 shibayan/Sharprompt %}

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

```
Install-Package Sharprompt
or
dotnet add package Sharprompt
```

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.

```csharp
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}");
    }
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uezzix1p0khwf8d79b97.gif)

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.

```csharp
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; }
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aput1ca7upy09z2n2xyi.gif)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efmq8ncle1pz5h8w6i66.gif)

This mock source code is here!

{% github shibayan/create-function-app-mock no-readme %}

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

Enjoy the development!