Skip to content

Skip showing help when the parser fails parsing the provided args #1873

Closed

Description

@qui8t

Currently, if you miss any of the required args, or provide invalid values for the arguments, the parser will fail to parse and shows why it failed, plus it displays the help message. I was wondering if I can avoid showing help.

Activity

  1. KalleOlaviNiemitalo commented on Oct 13, 2022

    @KalleOlaviNiemitalo

    It's called from ParseErrorResult.Apply

    which is added by CommandLineBuilderExtensions.UseParseErrorReporting

    if (context.ParseResult.Errors.Count > 0)
    {
    context.InvocationResult = new ParseErrorResult(errorExitCode);

    which is called by CommandLineBuilderExtensions.UseDefaults

    I imagine it could be disabled in any of the following ways:

    • Define custom middleware that is similar to the parse-error-reporting middleware but does not display help. Add it in one of these ways:
      • Don't let CommandLineBuilderExtensions.UseDefaults be called. Instead add each middleware explicitly.
      • Let CommandLineBuilderExtensions.UseDefaults be called, but add the custom middlware with order a little lower than MiddlewareOrderInternal.ParseErrorReporting, so that it can stop the original parse-error-reporting middleware from being called.
    • Call CommandLineBuilderExtensions.UseHelpBuilder to set a custom HelpBuilder that does not output anything if there are parse errors. However, this might stop --help from working in command lines that have parse errors.
  2. jonsequitur commented on Oct 13, 2022

    @jonsequitur
    Contributor

    Here's how I did this recently in the context of a custom HelpBuilder:

    https://github.com/jonsequitur/dotnet-repl/blob/743cb22edd61e0b0e6cae08c01e02db9b9f428a2/src/dotnet-repl/SpectreHelpBuilder.cs#L18-L22

    We'll come up with something simpler, but is one approach that works currently.

  3. qui8t commented on Oct 13, 2022

    @qui8t
    Author

    Thank @jonsequitur for the suggestion, with that I was able to build the following.

    var parser = new CommandLineBuilder(rootCommand)
        .UseVersionOption()
        .UseHelp(context =>
        {
            context.HelpBuilder.CustomizeLayout(
                x =>
                {
                    // this will avoid showing help
                    // if the parser fails to parse arguments.
                    if (x.ParseResult.Errors.Any())
                        return new List<HelpSectionDelegate>();
    
                    return HelpBuilder.Default.GetLayout();
                });
        })
        .Build();

    I appreciate how holistic and expressive command-line-api is, but I would appreciate it more if I could have done this type of adjustments much simpler :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions