Skip to content

Commit

Permalink
Add GetUsageInfo() method so you can obtain the help screen as a Cons…
Browse files Browse the repository at this point in the history
…oleColoredString without outputting it to the console
  • Loading branch information
Timwi committed Sep 27, 2024
1 parent 4d1d33c commit e657c9d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Src/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Reflection;
using RT.Internal;
using RT.PostBuild;
Expand Down Expand Up @@ -1395,11 +1395,27 @@ public CommandLineParseException(ConsoleColoredString message, Func<int, Console
/// understand.</summary>
public virtual void WriteUsageInfoToConsole()
{
ConsoleUtil.Write(GenerateHelp(ConsoleUtil.WrapToWidth()));
ConsoleUtil.Write(GetUsageInfo());
}

Console.WriteLine();
ConsoleUtil.Write(GenerateErrorText(ConsoleUtil.WrapToWidth()));
/// <summary>
/// Generates and returns usage information, followed by an error message describing to the user what it was that the
/// parser didn't understand.</summary>
public ConsoleColoredString GetUsageInfo()
{
var s = GenerateHelp(ConsoleUtil.WrapToWidth());
if (WriteErrorText)
s += Environment.NewLine + GenerateErrorText(ConsoleUtil.WrapToWidth());
return s;
}

/// <summary>
/// Determines whether <see cref="WriteUsageInfoToConsole"/> and <see cref="GetUsageInfo"/> should call <see
/// cref="GenerateErrorText"/> and output it to the console. Default is <c>true</c>.</summary>
/// <remarks>
/// Only set this to <c>false</c> if the user explicitly asked to see the help screen. Otherwise its appearance
/// without explanation is confusing.</remarks>
protected internal virtual bool WriteErrorText => true;
}

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

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

0 comments on commit e657c9d

Please sign in to comment.