mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Move Spectre.Console.Cli to it's own package
This commit is contained in:

committed by
Patrik Svensson

parent
b600832e00
commit
36ca22ffac
@ -0,0 +1,25 @@
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
|
||||
public sealed class EvenNumberValidatorAttribute : ParameterValidationAttribute
|
||||
{
|
||||
public EvenNumberValidatorAttribute(string errorMessage)
|
||||
: base(errorMessage)
|
||||
{
|
||||
}
|
||||
|
||||
public override ValidationResult Validate(CommandParameterContext context)
|
||||
{
|
||||
if (context.Value is int integer)
|
||||
{
|
||||
if (integer % 2 == 0)
|
||||
{
|
||||
return ValidationResult.Success();
|
||||
}
|
||||
|
||||
return ValidationResult.Error($"Number is not even ({context.Parameter.PropertyName}).");
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Parameter is not a number ({context.Parameter.PropertyName}).");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
|
||||
public sealed class PositiveNumberValidatorAttribute : ParameterValidationAttribute
|
||||
{
|
||||
public PositiveNumberValidatorAttribute(string errorMessage)
|
||||
: base(errorMessage)
|
||||
{
|
||||
}
|
||||
|
||||
public override ValidationResult Validate(CommandParameterContext context)
|
||||
{
|
||||
if (context.Value is int integer)
|
||||
{
|
||||
if (integer > 0)
|
||||
{
|
||||
return ValidationResult.Success();
|
||||
}
|
||||
|
||||
return ValidationResult.Error($"Number is not greater than 0 ({context.Parameter.PropertyName}).");
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Parameter is not a number ({context.Parameter.PropertyName}).");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user