You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Skip showing help when the parser fails parsing the provided args #1873
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.
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.
Thank @jonsequitur for the suggestion, with that I was able to build the following.
varparser=newCommandLineBuilder(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())returnnewList<HelpSectionDelegate>();returnHelpBuilder.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 :)
Activity
KalleOlaviNiemitalo commentedon Oct 13, 2022
It's called from ParseErrorResult.Apply
command-line-api/src/System.CommandLine/Invocation/ParseErrorResult.cs
Line 35 in 209b724
which is added by CommandLineBuilderExtensions.UseParseErrorReporting
command-line-api/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Lines 495 to 497 in 209b724
which is called by CommandLineBuilderExtensions.UseDefaults
command-line-api/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Line 276 in 209b724
I imagine it could be disabled in any of the following ways:
--helpfrom working in command lines that have parse errors.jonsequitur commentedon Oct 13, 2022
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.
qui8t commentedon Oct 13, 2022
Thank @jonsequitur for the suggestion, with that I was able to build the following.
I appreciate how holistic and expressive
command-line-apiis, but I would appreciate it more if I could have done this type of adjustments much simpler :)