Skip to content

Commit a282612

Browse files
committed
Add a new AutoBuild overload method in the HelpText class.
1 parent fe9e602 commit a282612

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/CommandLine/Text/HelpText.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public static HelpText AutoBuild<T>(
389389
}
390390

391391
/// <summary>
392-
/// Creates a new instance of the <see cref="CommandLine.Text.HelpText"/> class,
392+
/// Creates a default instance of the <see cref="CommandLine.Text.HelpText"/> class,
393393
/// automatically handling verbs or options scenario.
394394
/// </summary>
395395
/// <param name='parserResult'>The <see cref="CommandLine.ParserResult{T}"/> containing the instance that collected command line arguments parsed with <see cref="CommandLine.Parser"/> class.</param>
@@ -400,6 +400,23 @@ public static HelpText AutoBuild<T>(
400400
/// <remarks>This feature is meant to be invoked automatically by the parser, setting the HelpWriter property
401401
/// of <see cref="CommandLine.ParserSettings"/>.</remarks>
402402
public static HelpText AutoBuild<T>(ParserResult<T> parserResult, int maxDisplayWidth = DefaultMaximumLength)
403+
{
404+
return AutoBuild<T>(parserResult, h => h, maxDisplayWidth);
405+
}
406+
407+
/// <summary>
408+
/// Creates a custom instance of the <see cref="CommandLine.Text.HelpText"/> class,
409+
/// automatically handling verbs or options scenario.
410+
/// </summary>
411+
/// <param name='parserResult'>The <see cref="CommandLine.ParserResult{T}"/> containing the instance that collected command line arguments parsed with <see cref="CommandLine.Parser"/> class.</param>
412+
/// <param name='onError'>A delegate used to customize the text block of reporting parsing errors text block.</param>
413+
/// <param name="maxDisplayWidth">The maximum width of the display.</param>
414+
/// <returns>
415+
/// An instance of <see cref="CommandLine.Text.HelpText"/> class.
416+
/// </returns>
417+
/// <remarks>This feature is meant to be invoked automatically by the parser, setting the HelpWriter property
418+
/// of <see cref="CommandLine.ParserSettings"/>.</remarks>
419+
public static HelpText AutoBuild<T>(ParserResult<T> parserResult, Func<HelpText, HelpText> onError,int maxDisplayWidth = DefaultMaximumLength)
403420
{
404421
if (parserResult.Tag != ParserResultType.NotParsed)
405422
throw new ArgumentException("Excepting NotParsed<T> type.", "parserResult");
@@ -410,13 +427,25 @@ public static HelpText AutoBuild<T>(ParserResult<T> parserResult, int maxDisplay
410427
return new HelpText($"{HeadingInfo.Default}{Environment.NewLine}") { MaximumDisplayWidth = maxDisplayWidth }.AddPreOptionsLine(Environment.NewLine);
411428

412429
if (!errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError))
413-
return AutoBuild(parserResult, current => DefaultParsingErrorsHandler(parserResult, current), e => e, maxDisplayWidth: maxDisplayWidth);
430+
return AutoBuild(parserResult, current =>
431+
{
432+
onError?.Invoke(current);
433+
return DefaultParsingErrorsHandler(parserResult, current);
434+
}, e => e, maxDisplayWidth: maxDisplayWidth);
414435

415436
var err = errors.OfType<HelpVerbRequestedError>().Single();
416437
var pr = new NotParsed<object>(TypeInfo.Create(err.Type), Enumerable.Empty<Error>());
417438
return err.Matched
418-
? AutoBuild(pr, current => DefaultParsingErrorsHandler(pr, current), e => e, maxDisplayWidth: maxDisplayWidth)
419-
: AutoBuild(parserResult, current => DefaultParsingErrorsHandler(parserResult, current), e => e, true, maxDisplayWidth);
439+
? AutoBuild(pr, current =>
440+
{
441+
onError?.Invoke(current);
442+
return DefaultParsingErrorsHandler(pr, current);
443+
}, e => e, maxDisplayWidth: maxDisplayWidth)
444+
: AutoBuild(parserResult, current =>
445+
{
446+
onError?.Invoke(current);
447+
return DefaultParsingErrorsHandler(parserResult, current);
448+
}, e => e, true, maxDisplayWidth);
420449
}
421450

422451
/// <summary>

0 commit comments

Comments
 (0)