Command line call help text from inside handler
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/command-line-api
-
German localization is incompleteForse già presa @b-v-d-e-v l’ha presa 17 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
dotnet/command-line-api#2852 ·
-
Incomplete French (fr) translation: RequiredOptionWasNotProvided not translatedForse già presa @JPBlanc l’ha presa 115 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
dotnet/command-line-api#2822 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
dotnet/command-line-api#2792 · 2 commenti · 16 reazioni ·
-
`Argument.Arity` modify internal state on first get access.Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
dotnet/command-line-api#2704 ·
-
GetCompletions should check exit code of invoked applicationForse già presa @baradgur l’ha presa 1286 giorni fa. ApertaArea-Completions bug help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
dotnet/command-line-api#2137 · 1 commento · 3 reazioni ·
Tutte le issue di dotnet/command-line-api
Issue simili
-
bug priority: P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Parameterless constructor is not a must-provided by user when a struct has a field initializerAperta:watch: Not Triaged dotnet-csharp/svc fundamentals/subsvc
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
Wrong values of Kernel32.FileShare.Delete and Kernel32.FileAttributes.PosixSemanticsForse già presa @ElectLive l’ha presa oggi. Apertabug good first issue help wanted
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 83/100
ForNeVeR/TruePath#241 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
files-community/Files#19047 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno