dotnet / dotnet/command-line-api

How can I customise help for an option within a command?

Open
#1,593 8 comments 0 reactions 0 assignees View on GitHub
Area-Help question
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

I was reading the release for the notes about [Customizing help for a single option or argument](https://github.com/dotnet/command-line-api/issues/1537#:~:text=Customizing%20help%20for%20a%20single%20option%20or%20argument) and all the examples use an instance of the CommandLineBuilder. Is it possible to customise the help when declaring a new Option or in the constructor for a class. At the moment the project I am developing basically looks like this:

```csharp
// Commands are added as a Singleton to IServiceCollection and then added to the root command.
internal static Parser Build(ServiceProvider serviceProvider)
{
RootCommand rootCommand = new RootCommand(description: "Repository management tool.");
foreach (Command command in serviceProvider.GetServices())
{
rootCommand.AddCommand(command);
}

// Program.cs invokes the result of this async with the args.
return new CommandLineBuilder(rootCommand).UseDefaults().Build();
}

public class ConfigCommand : Command
{
private readonly string[] Force = new string[] { "-f", "--force" };

private readonly string[] Generate = new string[] { "-g", "--generate" };

public ConfigCommand() : base(
name: "config",
description: "Options for creating, getting or setting app configuration.")
{
Option forceOption = new Option(
aliases: this.Force,
description: "Specifies that an existing config file should be overwritten.");

Option generateOption = new Option(
aliases: this.Generate,
description: "Creates a new config file in the current directory.");

this.AddOption(generateOption);
this.AddOption(forceOption);

this.SetHandler(
(bool generate, bool force) =>
{
ConfigHandler.Invoke(generate, force);
},
generateOption,
forceOption);
}
}
```

Unless I am missing something, this is not currently possible unless the CommandLineBuilder is used, and I assume I shouldn't be using that for anything other than a single command which includes any subcommands, in this case the RootCommand. It would be cool to be able to apply a [customisation object directly to the option constructor](https://github.com/dotnet/command-line-api/blob/main/src/System.CommandLine/Help/HelpBuilder.cs#L87-L100).

I have seen the example in the tests [here](https://github.com/dotnet/command-line-api/blob/main/src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs#L75-L109), but this I do not think I can apply it to an unknown number of commands being passed via the ServiceProvider.

Apologies if anything is unclear, there's a learning curve involved for me here that extends outside of System.CommandLine :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.