Skip to content

Commit f174cf3

Browse files
committed
Fix #79 - remove ParseResult.ValidationResult
This property had side-effects that changed the order in which validation was called. The recommended replacement is ParseResult.SelectedCommand.GetValidationResult(), and it should be called after OnParsingComplete handlers are finished.
1 parent a69501f commit f174cf3

File tree

6 files changed

+11
-17
lines changed

6 files changed

+11
-17
lines changed
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System.ComponentModel.DataAnnotations;
5-
64
namespace McMaster.Extensions.CommandLineUtils.Abstractions
75
{
86
/// <summary>
@@ -14,10 +12,5 @@ public class ParseResult
1412
/// The application or subcommand that matches the command line arguments.
1513
/// </summary>
1614
public CommandLineApplication SelectedCommand { get; set; }
17-
18-
/// <summary>
19-
/// The result of executing all valiation on selected options and arguments.
20-
/// </summary>
21-
public ValidationResult ValidationResult { get; set; }
2215
}
2316
}

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,10 @@ public int Execute(params string[] args)
602602
return HelpExitCode;
603603
}
604604

605-
if (parseResult.ValidationResult != ValidationResult.Success)
605+
var validationResult = command.GetValidationResult();
606+
if (validationResult != ValidationResult.Success)
606607
{
607-
return command.ValidationErrorHandler(parseResult.ValidationResult);
608+
return command.ValidationErrorHandler(validationResult);
608609
}
609610

610611
return command.Invoke();

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public ParseResult Process()
4444

4545
finished:
4646
parseResult.SelectedCommand = _currentCommand;
47-
parseResult.ValidationResult = _currentCommand.GetValidationResult();
4847
return parseResult;
4948
}
5049

test/CommandLineUtils.Tests/CustomValidationAttributeTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void CustomValidationAttributePasses(params string[] args)
2020
var app = new CommandLineApplication<RedBlueProgram>();
2121
app.Conventions.UseDefaultConventions();
2222
var result = app.Parse(args ?? new string[0]);
23-
Assert.Equal(ValidationResult.Success, result.ValidationResult);
23+
Assert.Equal(ValidationResult.Success, result.SelectedCommand.GetValidationResult());
2424
var program = Assert.IsType<CommandLineApplication<RedBlueProgram>>(result.SelectedCommand);
2525
Assert.Same(app, program);
2626
if (args != null)
@@ -38,14 +38,15 @@ public void CustomValidationAttributeFails(params string[] args)
3838
var app = new CommandLineApplication<RedBlueProgram>();
3939
app.Conventions.UseAttributes();
4040
var result = app.Parse(args);
41-
Assert.NotEqual(ValidationResult.Success, result.ValidationResult);
41+
var validationResult = result.SelectedCommand.GetValidationResult();
42+
Assert.NotEqual(ValidationResult.Success, validationResult);
4243
var program = Assert.IsType<CommandLineApplication<RedBlueProgram>>(result.SelectedCommand);
4344
Assert.Same(app, program);
4445
if (args != null)
4546
{
4647
Assert.Equal(args[1], app.Model.Color);
4748
}
48-
Assert.Equal("The value for --color must be 'red' or 'blue'", result.ValidationResult.ErrorMessage);
49+
Assert.Equal("The value for --color must be 'red' or 'blue'", validationResult.ErrorMessage);
4950
}
5051

5152
private class RedBlueProgram

test/CommandLineUtils.Tests/ValidateMethodConventionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private ValidationResult OnValidate(ValidationContext context, CommandLineContex
5757
}
5858

5959
Assert.Equal(typeof(CommandLineApplication<MainValidate>), context.ObjectInstance.GetType());
60-
60+
6161
return ValidationResult.Success;
6262
}
6363
}
@@ -110,7 +110,7 @@ public void ValidatorShouldGetDeserailizedModelInSubcommands(string args, string
110110
// this tests that the model is actually given values before it passed to command validation
111111
var parseResult = app.Parse(args.Split(' '));
112112

113-
var result = parseResult.ValidationResult;
113+
var result = parseResult.SelectedCommand.GetValidationResult();
114114

115115
if (error == null)
116116
{

test/CommandLineUtils.Tests/ValidationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ public void ValidatesRange(int input, bool isValid)
339339
var result = app.Parse(input.ToString());
340340
if (isValid)
341341
{
342-
Assert.Equal(ValidationResult.Success, result.ValidationResult);
342+
Assert.Equal(ValidationResult.Success, result.SelectedCommand.GetValidationResult());
343343
}
344344
else
345345
{
346-
Assert.NotEqual(ValidationResult.Success, result.ValidationResult);
346+
Assert.NotEqual(ValidationResult.Success, result.SelectedCommand.GetValidationResult());
347347
}
348348
}
349349
}

0 commit comments

Comments
 (0)