Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Command line call help text from inside handler

Abierto
#2,514 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
csharp
Área
cli

Línea de trabajo

Comienza con el constructor de línea de comandos de Program y BarcodeCommand.BarcodeCommandHandler.InvokeAsync que se muestran en la incidencia. Determina cómo debe mostrar ayuda el comando cuando se invoca Barcode sin --list, preservando al mismo tiempo el comportamiento de Barcode --list; se considerará terminado cuando la estructura de comandos documentada funcione tal como se describe.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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!

Lenguaje dominante
C#
Estrellas
3.7k
Forks
434
Merge medio
6 h 27 min
PR fusionados (30 d)
2

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/command-line-api

Todos los issues de dotnet/command-line-api

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.