c# - System.CommandLine call help text from inside handler - Stack Overflow

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

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!

I also have an issue open on the github. If I get an awnser I will update here as well!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745301235a4621446.html

相关推荐

  • c# - System.CommandLine call help text from inside handler - Stack Overflow

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

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信