Heim > Backend-Entwicklung > C++ > Hauptteil

C# | Building a Command-Line (CLI) App using System.CommandLine Library

王林
Freigeben: 2024-07-24 00:34:04
Original
826 人浏览过

C# | Building a Command-Line (CLI) App using System.CommandLine Library

Note
You can check other posts on my personal website: https://hbolajraf.net

Introduction

In this guide, we will explore how to build a Command-Line Interface (CLI) application using the System.CommandLine library in C# and .NET. System.CommandLine simplifies the process of creating robust and feature-rich command-line interfaces for your applications.

Prerequisites

Before getting started, make sure you have the following installed:

  • .NET SDK (version 5.0 or later)

Step 1: Create a new Console Application

dotnet new console -n MyCommandLineApp
cd MyCommandLineApp
Nach dem Login kopieren

Step 2: Add System.CommandLine NuGet Package

dotnet add package System.CommandLine --version 2.0.0-beta1.21308.1
Nach dem Login kopieren

Step 3: Define Command-Line Options

In your Program.cs, define the command-line options using System.CommandLine:

using System.CommandLine;
using System.CommandLine.Invocation;

class Program
{
    static int Main(string[] args)
    {
        var rootCommand = new RootCommand
        {
            new Option("--number", "An integer option"),
            new Option("--flag", "A boolean option"),
            new Argument("input", "A required input argument")
        };

        rootCommand.Handler = CommandHandler.Create((number, flag, input) =>
        {
            // Your application logic goes here
            Console.WriteLine($"Number: {number}");
            Console.WriteLine($"Flag: {flag}");
            Console.WriteLine($"Input: {input}");
        });

        return rootCommand.Invoke(args);
    }
}
Nach dem Login kopieren

Step 4: Run the CLI App

dotnet run -- --number 42 --flag true "Hello, CLI!"
Nach dem Login kopieren

Replace the values with your own and see the output.

Step 5: Customize Help Text

Add descriptions to your options and arguments for better help text:

var rootCommand = new RootCommand
{
    new Option("--number", "An integer option"),
    new Option("--flag", "A boolean option"),
    new Argument("input", "A required input argument")
};

rootCommand.Description = "A simple CLI app";
rootCommand.Handler = CommandHandler.Create((number, flag, input) =>
{
    Console.WriteLine($"Number: {number}");
    Console.WriteLine($"Flag: {flag}");
    Console.WriteLine($"Input: {input}");
});
Nach dem Login kopieren

What Next?

You have successfully created a basic Command-Line Interface (CLI) application using the System.CommandLine library in C# and .NET. Customize and extend the application based on your specific requirements.
For more information, refer to the official documentation: System.CommandLine GitHub

以上是C# | Building a Command-Line (CLI) App using System.CommandLine Library的详细内容。更多信息请关注PHP中文网其他相关文章!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!