Maison  >  Article  >  développement back-end  >  C# | Création d'une application de ligne de commande (CLI) à l'aide de la bibliothèque System.CommandLine

C# | Création d'une application de ligne de commande (CLI) à l'aide de la bibliothèque System.CommandLine

王林
王林original
2024-07-24 00:34:041152parcourir

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

Dans ce guide, nous explorerons comment créer une application d'interface de ligne de commande (CLI) à l'aide de la bibliothèque System.CommandLine en C# et .NET. System.CommandLine simplifie le processus de création d'interfaces de ligne de commande robustes et riches en fonctionnalités pour vos applications.

Conditions préalables

Avant de commencer, assurez-vous que les éléments suivants sont installés :

  • SDK .NET (version 5.0 ou ultérieure)

Étape 1 : Créer une nouvelle application console

dotnet new console -n MyCommandLineApp
cd MyCommandLineApp

Étape 2 : ajouter le package NuGet System.CommandLine

dotnet add package System.CommandLine --version 2.0.0-beta1.21308.1

Étape 3 : définir les options de ligne de commande

Dans votre Program.cs, définissez les options de ligne de commande à l'aide de System.CommandLine :

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

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

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

        return rootCommand.Invoke(args);
    }
}

Étape 4 : exécutez l'application CLI

dotnet run -- --number 42 --flag true "Hello, CLI!"

Remplacez les valeurs par les vôtres et voyez le résultat.

Étape 5 : Personnaliser le texte d'aide

Ajoutez des descriptions à vos options et arguments pour un meilleur texte d'aide :

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

rootCommand.Description = "A simple CLI app";
rootCommand.Handler = CommandHandler.Create<int, bool, string>((number, flag, input) =>
{
    Console.WriteLine($"Number: {number}");
    Console.WriteLine($"Flag: {flag}");
    Console.WriteLine($"Input: {input}");
});

Et ensuite ?

Vous avez créé avec succès une application de base d'interface de ligne de commande (CLI) à l'aide de la bibliothèque System.CommandLine en C# et .NET. Personnalisez et étendez l'application en fonction de vos besoins spécifiques.
Pour plus d'informations, reportez-vous à la documentation officielle : System.CommandLine GitHub

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn