Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Command line call help text from inside handler

Aperta
#2,514 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
cli

Direzione di ricerca

Inizia con il builder della riga di comando di Program e BarcodeCommand.BarcodeCommandHandler.InvokeAsync mostrati nell’issue. Determina come il comando debba visualizzare l’help quando Barcode viene invocato senza --list, preservando al contempo il comportamento di Barcode --list; il lavoro è completato quando la struttura del comando documentata funziona come descritto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hello,

I hope you can help me. I am trying to create a command line application, but I am having trouble with the handling of the commands.

I have a command: Barcode. This command has an option: --list.
When Barcode is called, I want the command to display the help text.
When --list is added, I want it to do something.

However I am running into the issue that I cant call the help from inside the command handler. Is there a way to do this? In my main project I have DI injection set up, but I wouldn't know what I would have to inject to get to the help part of the command.

Here is a simplifyed bit of code showing what I want to do:

using Microsoft.Extensions.Hosting;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;
using UtTools.Controller.Commands.Barcode;

namespace UtToolsCommandLineTest
{
	internal class Program
	{
		static void Main(string[] args)
		{
			RootCommand root = new RootCommand("Root");
			root.AddCommand(new BarcodeCommand());

			CommandLineBuilder builder = new CommandLineBuilder(root)
				.UseDefaults()
				.UseHost(_ => Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostContext, config) =>
				{

				}),
				(builder) =>
				{
					builder.ConfigureServices(services =>
					{

					});

					builder.UseCommandHandler<BarcodeCommand, BarcodeCommand.BarcodeCommandHandler>();
				});

			builder.Build().InvokeAsync(args);
		}
	}
}
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Invocation;

namespace UtTools.Controller.Commands.Barcode
{
	internal class BarcodeCommand : Command
	{
		public BarcodeCommand(string name = "Barcode", string description = "The base command for generating ccsg and q13 barcodes.") : base(name, description)
		{
			Option<bool> listOption = new Option<bool>(
				aliases: new string[] { "--list", "-l" },
				description: "List all available barcodes");

			AddOption(listOption);
		}

		public class BarcodeCommandHandler : ICommandHandler
		{
			public bool List { get; set; }

			public BarcodeCommandHandler()
			{
			}

			public int Invoke(InvocationContext context)
			{
				return InvokeAsync(context).GetAwaiter().GetResult();
			}

			public Task<int> InvokeAsync(InvocationContext context)
			{
				if(!List)
				{		
                                        /////////////////////////////////
                                        // Should call the help text //
                                        /////////////////////////////////

					return Task.FromResult(1);
				}

                               // Do stuff
				return Task.FromResult(0);
			}
		}
	}
}
The end goal is a command like this:
Barcode
├── --List                                                          Show all barcodes available
├──Generate
            ├── --OutputDirectoryPath                  The output directory
            ├── --Name
            ├──All                                                   Generate all barcodes
                    ├── --OutputDirectoryPath          The output directory

Thanks for any help you can give!

Lingua principale
C#
Stelle
3.7k
Fork
434
Merge medio
6h 27m
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/command-line-api

Tutte le issue di dotnet/command-line-api

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.