Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomizeLayout method in CliHelpBuilder is not working as expected. #24

Open
moh-hassan opened this issue Feb 11, 2025 · 0 comments
Open

Comments

@moh-hassan
Copy link

moh-hassan commented Feb 11, 2025

I tried to append a new section to the current generated help using the method CustomizeLayout, but i can't and has no effect.

Something like:

  public void ShowCustomHelp()
  {
      var settings = new CliSettings
      {
          Output = new StringWriter(),
          Theme = CliTheme.Default
      };

      var parseResult = Cli.Parse<RootCliCommand>(["--help"], settings);
      if (parseResult.Action is HelpAction helpAction
          && helpAction.Builder is CliHelpBuilder cliBuilder)
          {
              cliBuilder.CustomizeLayout(context =>
              {
                  var defaultLayout = cliBuilder.GetLayout(context);
                  return defaultLayout.Concat([
                      ctx =>
                      {
                          ctx.Output.WriteLine("This is the footer text.");
                          return true;
                      }
                  ]);
              });

          (helpAction as SynchronousCommandLineAction)?.Invoke(parseResult);
      }

      Console.WriteLine(settings.Output.ToString());
      settings.Output.ToString().Should().Contain("This is the footer text.");
  }

HelpBuilder class in System.CommandLine provide a mechanism to Customize help layout by passing a delegate of type
Func<HelpContext, IEnumerable<Func<HelpContext, bool>>> to the method CustomizeLayout

The customization allowing add or replace a new section to the generated help at runtime.

The Root cause:
The method CustomizeLayout store the getLayout delegate in a private field _getLayout

This private field _getLayout is in the base class HelpBuilder in System.Commandline is not accessible by the class CliHelpBuilder.

CliHelpBuilder need to be modified to support CustomizeLayout

I can provide PR to fix this issue if you like.

moh-hassan added a commit to moh-hassan/command-line that referenced this issue Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant