Prologue

Before you start creating CLI for your application, you need to design commands, options, arguments, well-explained guidance for it.

Think you have an application converts CSV to Excel files and backward.

Commands

Convert

Print

Help

Global options

Input file

Verbose

Convert sub-command options

The first row as header (CSV only).

Output name.

Print sub-command options

Rows

Tail

Examples

# For converting the CSV/Excel files

csv-util convert --first-row-header --input:file.csv --output:output_file # For printing only the 10 first rows

csv-util print --rows:10 --input:file.csv # For priting only the 10 tailing rows

csv-util print --rows:10 --tail --input:file.csv

We have drafted a design for our CLI. It’s time to make it real.

CLI applications in .NET

There are a couple of libraries in .NET Core for creating CLI. CommandLineUtils by Nate McMaster is one of the best in my opinion. CommandLineUtils is a fork of Microsoft.Extensions.CommandLineUtils, which is no longer under active development.

Why CommandLineUtils library?

It supports dependency injection in CLI application.

Supports attribute and API builder for creating CLI.

In this article, we focus on this library to implement CSVUtil CLI.