File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed
System.CommandLine.ApiCompatibility.Tests Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,7 @@ System.CommandLine.Completions
178178System.CommandLine.Help
179179 public class HelpAction : System.CommandLine.Invocation.SynchronousCommandLineAction
180180 .ctor()
181+ public System.Int32 MaxWidth { get; set; }
181182 public System.Int32 Invoke(System.CommandLine.ParseResult parseResult)
182183 public class HelpOption : System.CommandLine.Option
183184 .ctor()
Original file line number Diff line number Diff line change @@ -230,6 +230,31 @@ public void The_users_can_print_help_output_of_a_subcommand()
230230 output . ToString ( ) . Should ( ) . NotContain ( RootDescription ) ;
231231 }
232232
233+ [ Fact ]
234+ public void The_users_can_set_max_width ( )
235+ {
236+ string firstPart = new ( 'a' , count : 50 ) ;
237+ string secondPart = new ( 'b' , count : 50 ) ;
238+ string description = firstPart + secondPart ;
239+
240+ RootCommand rootCommand = new ( description ) ;
241+ rootCommand . Options . Clear ( ) ;
242+ rootCommand . Options . Add ( new HelpOption ( )
243+ {
244+ Action = new HelpAction ( )
245+ {
246+ MaxWidth = 2 /* each line starts with two spaces */ + description . Length / 2
247+ }
248+ } ) ;
249+ StringWriter output = new ( ) ;
250+
251+ rootCommand . Parse ( "--help" ) . Invoke ( new ( ) { Output = output } ) ;
252+
253+ output . ToString ( ) . Should ( ) . NotContain ( description ) ;
254+ output . ToString ( ) . Should ( ) . Contain ( $ " { firstPart } { Environment . NewLine } ") ;
255+ output . ToString ( ) . Should ( ) . Contain ( $ " { secondPart } { Environment . NewLine } ") ;
256+ }
257+
233258 private sealed class CustomizedHelpAction : SynchronousCommandLineAction
234259 {
235260 internal const string CustomUsageText = "This is custom command usage example." ;
Original file line number Diff line number Diff line change @@ -8,13 +8,47 @@ namespace System.CommandLine.Help
88 public sealed class HelpAction : SynchronousCommandLineAction
99 {
1010 private HelpBuilder ? _builder ;
11+ private int _maxWidth = - 1 ;
12+
13+ /// <summary>
14+ /// The maximum width in characters after which help output is wrapped.
15+ /// </summary>
16+ /// <remarks>It defaults to <see cref="Console.WindowWidth"/>.</remarks>
17+ public int MaxWidth
18+ {
19+ get
20+ {
21+ if ( _maxWidth < 0 )
22+ {
23+ try
24+ {
25+ _maxWidth = Console . IsOutputRedirected ? int . MaxValue : Console . WindowWidth ;
26+ }
27+ catch ( Exception )
28+ {
29+ _maxWidth = int . MaxValue ;
30+ }
31+ }
32+
33+ return _maxWidth ;
34+ }
35+ set
36+ {
37+ if ( value <= 0 )
38+ {
39+ throw new ArgumentOutOfRangeException ( nameof ( value ) ) ;
40+ }
41+
42+ _maxWidth = value ;
43+ }
44+ }
1145
1246 /// <summary>
1347 /// Specifies an <see cref="Builder"/> to be used to format help output when help is requested.
1448 /// </summary>
1549 internal HelpBuilder Builder
1650 {
17- get => _builder ??= new HelpBuilder ( Console . IsOutputRedirected ? int . MaxValue : Console . WindowWidth ) ;
51+ get => _builder ??= new HelpBuilder ( MaxWidth ) ;
1852 set => _builder = value ?? throw new ArgumentNullException ( nameof ( value ) ) ;
1953 }
2054
@@ -32,4 +66,4 @@ public override int Invoke(ParseResult parseResult)
3266 return 0 ;
3367 }
3468 }
35- }
69+ }
You can’t perform that action at this time.
0 commit comments