Skip to content

Commit e657c9d

Browse files
committed
Add GetUsageInfo() method so you can obtain the help screen as a ConsoleColoredString without outputting it to the console
1 parent 4d1d33c commit e657c9d

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

Src/CommandLine.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22
using System.Reflection;
33
using RT.Internal;
44
using RT.PostBuild;
@@ -1395,11 +1395,27 @@ public CommandLineParseException(ConsoleColoredString message, Func<int, Console
13951395
/// understand.</summary>
13961396
public virtual void WriteUsageInfoToConsole()
13971397
{
1398-
ConsoleUtil.Write(GenerateHelp(ConsoleUtil.WrapToWidth()));
1398+
ConsoleUtil.Write(GetUsageInfo());
1399+
}
13991400

1400-
Console.WriteLine();
1401-
ConsoleUtil.Write(GenerateErrorText(ConsoleUtil.WrapToWidth()));
1401+
/// <summary>
1402+
/// Generates and returns usage information, followed by an error message describing to the user what it was that the
1403+
/// parser didn't understand.</summary>
1404+
public ConsoleColoredString GetUsageInfo()
1405+
{
1406+
var s = GenerateHelp(ConsoleUtil.WrapToWidth());
1407+
if (WriteErrorText)
1408+
s += Environment.NewLine + GenerateErrorText(ConsoleUtil.WrapToWidth());
1409+
return s;
14021410
}
1411+
1412+
/// <summary>
1413+
/// Determines whether <see cref="WriteUsageInfoToConsole"/> and <see cref="GetUsageInfo"/> should call <see
1414+
/// cref="GenerateErrorText"/> and output it to the console. Default is <c>true</c>.</summary>
1415+
/// <remarks>
1416+
/// Only set this to <c>false</c> if the user explicitly asked to see the help screen. Otherwise its appearance
1417+
/// without explanation is confusing.</remarks>
1418+
protected internal virtual bool WriteErrorText => true;
14031419
}
14041420

14051421
/// <summary>Indicates that the user supplied one of the standard options we recognize as a help request.</summary>
@@ -1409,6 +1425,8 @@ public sealed class CommandLineHelpRequestedException(Func<int, ConsoleColoredSt
14091425
{
14101426
/// <summary>Prints usage information.</summary>
14111427
public override void WriteUsageInfoToConsole() => ConsoleUtil.Write(GenerateHelp(ConsoleUtil.WrapToWidth()));
1428+
/// <inheritdoc/>
1429+
protected internal override bool WriteErrorText => false;
14121430
}
14131431

14141432
/// <summary>Specifies that the arguments specified by the user on the command-line do not pass the custom validation checks.</summary>

0 commit comments

Comments
 (0)