Command Line Tools Using .NET 5

Introduction

This article presents how simple it is to create CLI/Command-line tools using C# with .Net 5.0.

The concept used in this article helps the developers to focus only on their business needs rather than thinking about how to get the parameters, how to parse them and map with methods etc.

Prerequisites

Table of Contents

  1. Creating .Net Core Console application.
  2. Adding CommandLineTool Nuget to the application reference.
  3. Implementing Class and the method attribute for the CLI class.
  4. Running the tool and testing.
  5. Conclusion

Creating .Net Core Console application

Open the Visual Studio and search for Console App. Click on the Next button,

Commandline tools using .Net 5

Define the project name, path, and solution name. Click on the Next button,

Commandline tools using .Net 5

Choose the Framework and Click on Create,

Commandline tools using .Net 5

Adding CommandLineTool Nuget to the application reference

Right-click on the project and click on "Manage Nuget Packages". Search for "CommandLineTool" in browse tab.

Commandline tools using .Net 5

Implementing Class and the method attribute for the CLI class

Add a new class to the project like below,

using CommandLineTool.Attributes;
using System;
using System.Collections.Generic;
using System.IO;

namespace CLISample
{
    [App("my application")]
    public class MyCli
    {
        /// <summary>
        /// Private static method Example
        /// </summary>
        /// <param name="v">simple string</param>
        [Command("privateprint", "Private static command")]
        private static void PrivatePrint(
            [ParamArgument()] string v) => Console.WriteLine($"type: {v.GetType()}, {v}");

        /// <summary>
        /// public method sample
        /// </summary>
        /// <param name="v"></param>
        [Command("publicprint", "public method command")]
        public void PublicPrint(
            [ParamArgument()] string v)
            => Console.WriteLine($"type: {v.GetType()}, {v}");

        /// <summary>
        /// List of string as Arguments
        /// Ex: lst val1 val2 -> List<string>{val1,val2}
        /// </summary>
        /// <param name="lst"></param>
        [Command("listprint", "mapping arguments with list")]
        public void Listprint(
            [ParamArgument()] List<string> lst)
            => lst.ForEach(v => Console.WriteLine($"type: {v.GetType()}, {v}"));

        /// <summary>
        /// get custom object list 
        /// Ex: files <file1_path> <file2_path> -> List<FileInfo>{file1_path,file2_path}
        /// </summary>
        /// <param name="lst"></param>
        [Command("files", "mapping arguments with complex type arguments")]
        public void MethodFiles(
            [ParamArgument()] List<FileInfo> lst)
            => lst.ForEach(v => Console.WriteLine($"type: {v.GetType()}, {v.FullName}"));

        /// <summary>
        /// option sample 
        /// Ex: files <file1_path> <file2_path> -> List<FileInfo>{file1_path,file2_path}
        /// </summary>
        /// <param name="lst"></param>
        [Command("options", "using options")]
        public void MethodOptions(
            [ParamArgument()] string v, [ParamOption("-a")] string op1)
        {
            Console.WriteLine($"type: {v.GetType()}, {v}");
            Console.WriteLine($"type: {op1.GetType()}, {op1}");
        }
    }
}

Instantiate the class in Program.cs,

using CommandLineTool;
using System;

namespace CLISample
{
    class Program
    {
        static void Main(string[] args)
        {
            Cli cli = new(typeof(MyCli))
            {
                Introduction = "my intro",
                PromptText = "CLI"
            };
            //optional: set list of keys to exit from the command loop
            cli.SetCancellationKeys(new() { "exit" });
            cli.Start();
        }
    }
}

Running the tool and testing

Run the application the console will be launched with the title and the intro given by you.

Test commands like below in the console,

Commandline tools using .Net 5

Conclusion

Thank you for reading, please let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.

Keep learning....!

 I'm sharing the project source which has more samples and code.


Similar Articles