Skip to content

Commit 641ee79

Browse files
committed
fix help error when an option is required
1 parent 217225e commit 641ee79

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

src/System.CommandLine.Tests/UseHelpTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.CommandLine.Tests.Utility;
99
using System.Threading.Tasks;
1010
using FluentAssertions;
11-
using FluentAssertions.Primitives;
1211
using Xunit;
1312

1413
namespace System.CommandLine.Tests
@@ -144,6 +143,25 @@ public void There_are_no_parse_errors_when_help_is_invoked_on_a_command_with_sub
144143
result.Errors.Should().BeEmpty();
145144
}
146145

146+
[Fact]
147+
public void There_are_no_parse_errors_when_help_is_invoked_on_a_command_with_required_options()
148+
{
149+
var command = new RootCommand
150+
{
151+
new Option<string>("-x")
152+
{
153+
Required = true
154+
},
155+
};
156+
157+
var result = new CommandLineBuilder(command)
158+
.UseHelp()
159+
.Build()
160+
.Parse("-h");
161+
162+
result.Errors.Should().BeEmpty();
163+
}
164+
147165
[Theory]
148166
[InlineData("-h")]
149167
[InlineData("inner -h")]

src/System.CommandLine/Builder/CommandLineBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public CommandLineBuilder(Command? rootCommand = null)
2727

2828
internal Func<BindingContext, IHelpBuilder>? HelpBuilderFactory { get; set; }
2929

30-
internal Option? HelpOption { get; set; }
30+
internal HelpOption? HelpOption { get; set; }
3131

3232
internal ValidationMessages? ValidationMessages { get; set; }
3333

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static CommandLineBuilder UseHelp(this CommandLineBuilder builder)
282282

283283
internal static CommandLineBuilder UseHelp(
284284
this CommandLineBuilder builder,
285-
Option helpOption)
285+
HelpOption helpOption)
286286
{
287287
if (builder.HelpOption is null)
288288
{

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,18 @@ protected override void VisitUnknownNode(SyntaxNode node)
154154

155155
protected override void Stop(SyntaxNode node)
156156
{
157+
var helpWasRequested =
158+
_innermostCommandResult
159+
?.Children
160+
.Any(o => o.Symbol is HelpOption) == true;
161+
162+
if (helpWasRequested)
163+
{
164+
return;
165+
}
166+
157167
ValidateCommandHandler();
158-
168+
159169
PopulateDefaultValues();
160170

161171
ValidateCommandResult();
@@ -229,7 +239,7 @@ private void ValidateCommandResult()
229239

230240
private void ValidateCommandHandler()
231241
{
232-
if (!(_innermostCommandResult!.Command is Command cmd) ||
242+
if (!(_innermostCommandResult!.Command is Command cmd) ||
233243
cmd.Handler != null)
234244
{
235245
return;
@@ -240,15 +250,11 @@ private void ValidateCommandHandler()
240250
return;
241251
}
242252

243-
if (!_innermostCommandResult
244-
.Children
245-
.Select(o => o.Symbol is HelpOption).Any())
246-
{
247-
_errors.Insert(0,
248-
new ParseError(
249-
_innermostCommandResult.ValidationMessages.RequiredCommandWasNotProvided(),
250-
_innermostCommandResult));
251-
}
253+
_errors.Insert(
254+
0,
255+
new ParseError(
256+
_innermostCommandResult.ValidationMessages.RequiredCommandWasNotProvided(),
257+
_innermostCommandResult));
252258
}
253259

254260
private void ValidateOptionResult(OptionResult optionResult)

0 commit comments

Comments
 (0)