diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
index 80214bd154..cfac3fae65 100644
--- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
+++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
@@ -178,6 +178,7 @@ System.CommandLine.Completions
System.CommandLine.Help
public class HelpAction : System.CommandLine.Invocation.SynchronousCommandLineAction
.ctor()
+ public System.Int32 MaxWidth { get; set; }
public System.Int32 Invoke(System.CommandLine.ParseResult parseResult)
public class HelpOption : System.CommandLine.Option
.ctor()
diff --git a/src/System.CommandLine.Tests/HelpOptionTests.cs b/src/System.CommandLine.Tests/HelpOptionTests.cs
index cfd85d795a..27552bf8ac 100644
--- a/src/System.CommandLine.Tests/HelpOptionTests.cs
+++ b/src/System.CommandLine.Tests/HelpOptionTests.cs
@@ -230,6 +230,31 @@ public void The_users_can_print_help_output_of_a_subcommand()
output.ToString().Should().NotContain(RootDescription);
}
+ [Fact]
+ public void The_users_can_set_max_width()
+ {
+ string firstPart = new('a', count: 50);
+ string secondPart = new('b', count: 50);
+ string description = firstPart + secondPart;
+
+ RootCommand rootCommand = new(description);
+ rootCommand.Options.Clear();
+ rootCommand.Options.Add(new HelpOption()
+ {
+ Action = new HelpAction()
+ {
+ MaxWidth = 2 /* each line starts with two spaces */ + description.Length / 2
+ }
+ });
+ StringWriter output = new ();
+
+ rootCommand.Parse("--help").Invoke(new() { Output = output });
+
+ output.ToString().Should().NotContain(description);
+ output.ToString().Should().Contain($" {firstPart}{Environment.NewLine}");
+ output.ToString().Should().Contain($" {secondPart}{Environment.NewLine}");
+ }
+
private sealed class CustomizedHelpAction : SynchronousCommandLineAction
{
internal const string CustomUsageText = "This is custom command usage example.";
diff --git a/src/System.CommandLine/Help/HelpAction.cs b/src/System.CommandLine/Help/HelpAction.cs
index c04a8db53a..8b6460631a 100644
--- a/src/System.CommandLine/Help/HelpAction.cs
+++ b/src/System.CommandLine/Help/HelpAction.cs
@@ -8,13 +8,47 @@ namespace System.CommandLine.Help
public sealed class HelpAction : SynchronousCommandLineAction
{
private HelpBuilder? _builder;
+ private int _maxWidth = -1;
+
+ ///
+ /// The maximum width in characters after which help output is wrapped.
+ ///
+ /// It defaults to .
+ public int MaxWidth
+ {
+ get
+ {
+ if (_maxWidth < 0)
+ {
+ try
+ {
+ _maxWidth = Console.IsOutputRedirected ? int.MaxValue : Console.WindowWidth;
+ }
+ catch (Exception)
+ {
+ _maxWidth = int.MaxValue;
+ }
+ }
+
+ return _maxWidth;
+ }
+ set
+ {
+ if (value <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(value));
+ }
+
+ _maxWidth = value;
+ }
+ }
///
/// Specifies an to be used to format help output when help is requested.
///
internal HelpBuilder Builder
{
- get => _builder ??= new HelpBuilder(Console.IsOutputRedirected ? int.MaxValue : Console.WindowWidth);
+ get => _builder ??= new HelpBuilder(MaxWidth);
set => _builder = value ?? throw new ArgumentNullException(nameof(value));
}
@@ -32,4 +66,4 @@ public override int Invoke(ParseResult parseResult)
return 0;
}
}
-}
\ No newline at end of file
+}
\ No newline at end of file