Flag to hide description
Hi,
It would be helpful to have a flag or property to control whether the description is included in the generated HelpText output.
In our use case, we are using the HelpText in our documentation, and we don’t want to include the description section—especially when the description is empty.
Here’s a minimal example:
RootCommand rootCommand = new RootCommand();
CommandLineConfiguration config = new(rootCommand) {Output = new StringWriter()};
config.Invoke("--help");
Console.Out.WriteLine(config.Output);
Current Output:
Description:
Usage:
ConsoleApp1 [options]
Options:
-?, -h, --help Show help and usage information
--version Show version information
It would be great to have a way to suppress the Description: section entirely, for example:
rootCommand.HideDescription = true;
Thanks for the great library!
In System.CommandLine 2.0.0-beta4.22272.1, this can be done by using CommandLineBuilderExtensions.UseHelp to register an Action<HelpContext> that calls HelpBuilder.CustomizeLayout to register a Func<HelpContext, IEnumerable<HelpSectionDelegate>> that calls HelpBuilder.Default.GetLayout() and removes HelpBuilder.Default.SynopsisSection() from the result.
Many thanks for the tip! ❤ It works as a temporary solution. A flag would be more elegant for later.
I saw that beta5 has many changes compared to beta4, and so I used beta5 (2.0.0-beta5.25174.1) straight away.
Example with beta5:
RootCommand rootCommand = new RootCommand();
CommandLineConfiguration config = new(rootCommand) {Output = new StringWriter()};
Option helpOption = rootCommand.Options.First(option => option is HelpOption);
HelpAction helpAction = helpOption.Action as HelpAction;
helpAction.Builder.CustomizeLayout(context =>
{
IEnumerable<Func<HelpContext, bool>> layout = HelpBuilder.Default.GetLayout();
List<Func<HelpContext, bool>> list = layout.ToList();
list.Remove(HelpBuilder.Default.SynopsisSection());
layout = list;
return layout;
});
config.Invoke("--help");
Console.Out.WriteLine(config.Output);
Output:
Usage:
ConsoleApp1 [options]
Options:
-?, -h, --help Show help and usage information
--version Show version information
In System.CommandLine 2.0.0-beta4.22272.1, this can be done by using CommandLineBuilderExtensions.UseHelp to register an
Action<HelpContext>that calls HelpBuilder.CustomizeLayout to register aFunc<HelpContext, IEnumerable<HelpSectionDelegate>>that calls HelpBuilder.Default.GetLayout() and removes HelpBuilder.Default.SynopsisSection() from the result.
Now that HelpBuilder is internal how can customization of the HelpAction like this be implemented in beta5?
Currently you have to provide a custom action to replace the HelpAction entirely.
The old HelpBuilder API is now internal. It was very complex and hard to evolve. You could pretty easily copy and adapt it as a short-term workaround. I think that going forward, people will release packages that extend the new API to provide richer help features.