command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

Flag to hide description

Open TheBigNeo opened this issue 8 months ago • 4 comments

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!

TheBigNeo avatar Apr 03 '25 14:04 TheBigNeo

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.

KalleOlaviNiemitalo avatar Apr 03 '25 14:04 KalleOlaviNiemitalo

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

TheBigNeo avatar Apr 04 '25 10:04 TheBigNeo

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.

Now that HelpBuilder is internal how can customization of the HelpAction like this be implemented in beta5?

misirlou-tg avatar Jun 06 '25 01:06 misirlou-tg

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.

jonsequitur avatar Jun 06 '25 20:06 jonsequitur