File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
System.CommandLine/Invocation Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -60,5 +60,46 @@ public void User_can_customize_parse_error_result_code()
60
60
61
61
result . Should ( ) . Be ( 42 ) ;
62
62
}
63
+
64
+ [ Fact ]
65
+ public void User_can_customize_help_printed_on_parse_error ( )
66
+ {
67
+ CustomHelpBuilder customHelpBuilder = new ( ) ;
68
+
69
+ CliRootCommand root = new ( ) ;
70
+ root . Options . Clear ( ) ;
71
+ root . Options . Add ( new HelpOption ( )
72
+ {
73
+ Action = new HelpAction ( )
74
+ {
75
+ Builder = customHelpBuilder
76
+ }
77
+ } ) ;
78
+
79
+ CliConfiguration config = new ( root )
80
+ {
81
+ EnableParseErrorReporting = true
82
+ } ;
83
+
84
+ customHelpBuilder . WasUsed . Should ( ) . BeFalse ( ) ;
85
+
86
+ ParseResult parseResult = root . Parse ( "-bla" , config ) ;
87
+
88
+ int result = parseResult . Invoke ( ) ;
89
+ result . Should ( ) . Be ( 1 ) ;
90
+ customHelpBuilder . WasUsed . Should ( ) . BeTrue ( ) ;
91
+ }
92
+
93
+ private sealed class CustomHelpBuilder : HelpBuilder
94
+ {
95
+ internal bool WasUsed = false ;
96
+
97
+ public override void Write ( HelpContext context )
98
+ {
99
+ WasUsed = true ;
100
+
101
+ base . Write ( context ) ;
102
+ }
103
+ }
63
104
}
64
105
}
Original file line number Diff line number Diff line change 2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . CommandLine . Help ;
5
+ using System . Linq ;
5
6
using System . Threading ;
6
7
using System . Threading . Tasks ;
7
8
@@ -23,7 +24,9 @@ public override int Invoke(ParseResult parseResult)
23
24
24
25
ConsoleHelpers . ResetTerminalForegroundColor ( ) ;
25
26
26
- new HelpOption ( ) . Action ! . Invoke ( parseResult ) ;
27
+ HelpOption helpOption = parseResult . RootCommandResult . Command . Options . FirstOrDefault ( option => option is HelpOption ) as HelpOption ?? new HelpOption ( ) ;
28
+
29
+ helpOption . Action ! . Invoke ( parseResult ) ;
27
30
28
31
return 1 ;
29
32
}
You can’t perform that action at this time.
0 commit comments