@@ -17,7 +17,8 @@ namespace System.CommandLine
17
17
/// </summary>
18
18
public class CommandLineConfiguration
19
19
{
20
- private readonly SymbolSet _symbols = new SymbolSet ( ) ;
20
+ private readonly SymbolSet _symbols = new ( ) ;
21
+ private Func < BindingContext , IHelpBuilder > ? _helpBuilderFactory ;
21
22
22
23
/// <summary>
23
24
/// Initializes a new instance of the CommandLineConfiguration class.
@@ -60,17 +61,18 @@ public CommandLineConfiguration(
60
61
else
61
62
{
62
63
// Reuse existing auto-generated root command, if one is present, to prevent repeated mutations
63
- RootCommand ? parentRootCommand =
64
+ RootCommand ? parentRootCommand =
64
65
symbols . SelectMany ( s => s . Parents )
65
- . OfType < RootCommand > ( )
66
- . FirstOrDefault ( ) ;
66
+ . OfType < RootCommand > ( )
67
+ . FirstOrDefault ( ) ;
67
68
68
69
if ( parentRootCommand is null )
69
70
{
70
71
parentRootCommand = new RootCommand ( ) ;
71
72
72
- foreach ( var symbol in symbols )
73
+ for ( var i = 0 ; i < symbols . Count ; i ++ )
73
74
{
75
+ var symbol = symbols [ i ] ;
74
76
parentRootCommand . Add ( symbol ) ;
75
77
}
76
78
}
@@ -87,19 +89,13 @@ public CommandLineConfiguration(
87
89
Resources = resources ?? Resources . Instance ;
88
90
ResponseFileHandling = responseFileHandling ;
89
91
Middleware = middlewarePipeline ?? new List < InvocationMiddleware > ( ) ;
90
- HelpBuilderFactory = helpBuilderFactory ?? ( context =>
91
- {
92
- int maxWidth = int . MaxValue ;
93
- if ( context . Console is SystemConsole systemConsole )
94
- {
95
- maxWidth = systemConsole . GetWindowWidth ( ) ;
96
- }
97
- return new HelpBuilder ( context . Console , maxWidth ) ;
98
- } ) ;
92
+
93
+ _helpBuilderFactory = helpBuilderFactory ;
94
+
99
95
if ( configureHelp != null )
100
96
{
101
97
var factory = HelpBuilderFactory ;
102
- HelpBuilderFactory = context =>
98
+ _helpBuilderFactory = context =>
103
99
{
104
100
IHelpBuilder helpBuilder = factory ( context ) ;
105
101
configureHelp ( helpBuilder ) ;
@@ -108,6 +104,17 @@ public CommandLineConfiguration(
108
104
}
109
105
}
110
106
107
+ private static IHelpBuilder DefaultHelpBuilderFactory ( BindingContext context )
108
+ {
109
+ int maxWidth = int . MaxValue ;
110
+ if ( context . Console is SystemConsole systemConsole )
111
+ {
112
+ maxWidth = systemConsole . GetWindowWidth ( ) ;
113
+ }
114
+
115
+ return new HelpBuilder ( context . Console , maxWidth ) ;
116
+ }
117
+
111
118
private void AddGlobalOptionsToChildren ( Command parentCommand )
112
119
{
113
120
for ( var childIndex = 0 ; childIndex < parentCommand . Children . Count ; childIndex ++ )
@@ -151,7 +158,7 @@ private void AddGlobalOptionsToChildren(Command parentCommand)
151
158
/// </summary>
152
159
public Resources Resources { get ; }
153
160
154
- internal Func < BindingContext , IHelpBuilder > HelpBuilderFactory { get ; }
161
+ internal Func < BindingContext , IHelpBuilder > HelpBuilderFactory => _helpBuilderFactory ??= DefaultHelpBuilderFactory ;
155
162
156
163
internal IReadOnlyCollection < InvocationMiddleware > Middleware { get ; }
157
164
0 commit comments