Skip to content

Commit 4e2e2d2

Browse files
committed
Fix #32 - don't execute validation check when HelpOption or VersionOption are matched
1 parent 8519138 commit 4e2e2d2

File tree

6 files changed

+64
-6
lines changed

6 files changed

+64
-6
lines changed

build.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ $artifacts = "$PSScriptRoot/artifacts/"
3939

4040
Remove-Item -Recurse $artifacts -ErrorAction Ignore
4141

42-
exec dotnet restore @MSBuildArgs
43-
exec dotnet build --no-restore --configuration $Configuration @MSBuildArgs
42+
exec dotnet build --configuration $Configuration @MSBuildArgs
4443
exec dotnet pack --no-restore --no-build --configuration $Configuration -o $artifacts @MSBuildArgs
4544
exec dotnet test --no-restore --no-build --configuration $Configuration '-clp:Summary' `
4645
"$PSScriptRoot/test/CommandLineUtils.Tests/McMaster.Extensions.CommandLineUtils.Tests.csproj" `

releasenotes.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<Project>
22
<PropertyGroup>
3+
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.1.1'">
4+
2.1.1:
5+
Bug fixes:
6+
- Do not show validation error messages when --help or --version are specified
7+
- Fix help text to show correct short option when OptionAttribute.ShortName is overriden
8+
</PackageReleaseNotes>
39
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.1.0'">
410
2.1.0:
511
New features:

src/CommandLineUtils/CommandLineApplication.Execute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static int Execute<TApp>(IConsole console, params string[] args)
4848
var bindResult = Bind<TApp>(console, args);
4949
if (bindResult.Command.IsShowingInformation)
5050
{
51-
return 0;
51+
return HelpExitCode;
5252
}
5353

5454
if (bindResult.ValidationResult != ValidationResult.Success)
@@ -101,7 +101,7 @@ public static async Task<int> ExecuteAsync<TApp>(IConsole console, params string
101101
var bindResult = Bind<TApp>(console, args);
102102
if (bindResult.Command.IsShowingInformation)
103103
{
104-
return 0;
104+
return HelpExitCode;
105105
}
106106

107107
if (bindResult.ValidationResult != ValidationResult.Success)
@@ -142,7 +142,7 @@ private static int HandleValidationError(IConsole console, BindResult bindResult
142142
return (int)result;
143143
}
144144

145-
return 1;
145+
return ValidationErrorExitCode;
146146
}
147147

148148
private static BindResult Bind<TApp>(IConsole console, string[] args) where TApp : class, new()

src/CommandLineUtils/CommandLineApplication.Validation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private int DefaultValidationErrorHandler(ValidationResult result)
108108
_console.Error.WriteLine(result.ErrorMessage);
109109
_console.ResetColor();
110110
ShowHint();
111-
return 1;
111+
return ValidationErrorExitCode;
112112
}
113113

114114
private sealed class ServiceProvider : IServiceProvider

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace McMaster.Extensions.CommandLineUtils
2020
/// </summary>
2121
public partial class CommandLineApplication
2222
{
23+
private const int HelpExitCode = 0;
24+
private const int ValidationErrorExitCode = 1;
25+
2326
private IConsole _console;
2427
private IHelpTextGenerator _helpTextGenerator;
2528

@@ -379,6 +382,11 @@ public int Execute(params string[] args)
379382
var processor = new CommandLineProcessor(this, args);
380383
var command = processor.Process();
381384

385+
if (command.IsShowingInformation)
386+
{
387+
return HelpExitCode;
388+
}
389+
382390
var result = command.GetValidationResult();
383391

384392
if (result != ValidationResult.Success)

test/CommandLineUtils.Tests/ValidationTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System;
55
using System.ComponentModel.DataAnnotations;
6+
using System.IO;
7+
using System.Text;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -305,5 +307,48 @@ public void ParentOptionsAreValidated_AttributeBinding()
305307
var rc = CommandLineApplication.Execute<ValidationParent>("sub");
306308
Assert.Equal(10, rc);
307309
}
310+
311+
[Fact]
312+
public void DoesNotValidate_WhenShowingInfo()
313+
{
314+
var sb = new StringBuilder();
315+
var console = new TestConsole(_output)
316+
{
317+
Out = new StringWriter(sb),
318+
};
319+
var app = new CommandLineApplication(console);
320+
app.HelpOption();
321+
var errorMessage = "Version arg is required";
322+
app.Argument("version", "Arg").IsRequired(errorMessage: errorMessage);
323+
app.OnValidationError((_) => Assert.False(true, "Validation callbacks should not be executed"));
324+
325+
Assert.Equal(0, app.Execute("--help"));
326+
Assert.DoesNotContain(errorMessage, sb.ToString());
327+
}
328+
329+
[HelpOption]
330+
private class ProgramWithRequiredArg
331+
{
332+
[Argument(0), Required(ErrorMessage = "Arg is required")]
333+
public string Version { get; }
334+
335+
private void OnValidationError()
336+
{
337+
throw new InvalidOperationException("Validation callback should not be executed");
338+
}
339+
}
340+
341+
[Fact]
342+
public void DoesNotValidate_WhenShowingInfo_AttributeBinding()
343+
{
344+
var sb = new StringBuilder();
345+
var console = new TestConsole(_output)
346+
{
347+
Out = new StringWriter(sb),
348+
};
349+
350+
Assert.Equal(0, CommandLineApplication.Execute<ProgramWithRequiredArg>(console, "--help"));
351+
Assert.DoesNotContain("Arg is required", sb.ToString());
352+
}
308353
}
309354
}

0 commit comments

Comments
 (0)