Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Command line call help text from inside handler

Đang mở
#2,514 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
cli

Hướng nghiên cứu

Bắt đầu với trình tạo dòng lệnh của Program và BarcodeCommand.BarcodeCommandHandler.InvokeAsync được trình bày trong issue. Xác định cách lệnh phải hiển thị trợ giúp khi Barcode được gọi mà không có --list, đồng thời giữ nguyên hành vi của Barcode --list; hoàn thành khi cấu trúc lệnh được tài liệu hóa hoạt động như mô tả.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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!

Ngôn ngữ chính
C#
Star
3.7k
Fork
434
Merge trung bình
6 giờ 27 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/command-line-api

Tất cả issue của dotnet/command-line-api

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.