-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
DelegateValidator.cs
34 lines (28 loc) · 1.34 KB
/
DelegateValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.ComponentModel.DataAnnotations;
namespace McMaster.Extensions.CommandLineUtils.Validation
{
/// <summary>
/// Implements a validator with an anonymous function
/// </summary>
public class DelegateValidator : ICommandValidator, IArgumentValidator, IOptionValidator
{
private readonly Func<ValidationContext, ValidationResult> _validator;
/// <summary>
/// Initializes an instance of <see cref="DelegateValidator"/>.
/// </summary>
/// <param name="validator"></param>
public DelegateValidator(Func<ValidationContext, ValidationResult> validator)
{
_validator = validator ?? throw new ArgumentNullException(nameof(validator));
}
ValidationResult ICommandValidator.GetValidationResult(CommandLineApplication command, ValidationContext context)
=> _validator(context);
ValidationResult IArgumentValidator.GetValidationResult(CommandArgument argument, ValidationContext context)
=> _validator(context);
ValidationResult IOptionValidator.GetValidationResult(CommandOption option, ValidationContext context)
=> _validator(context);
}
}