ValidateEnumValueAttribute Class

Controls validation rules for arguments with enumeration values.

Definition

Namespace: Ookii.CommandLine.Validation
Assembly: Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 5.0.0+724ca9b7fa5edc075ec2ae65044e61b0d494fd1c
C#
public class ValidateEnumValueAttribute : ArgumentValidationWithHelpAttribute
Inheritance
Object    Attribute    ArgumentValidationAttribute    ArgumentValidationWithHelpAttribute    ValidateEnumValueAttribute

Remarks

Conversion from a string to an enumeration value can use either the name of the enumeration member, the numeric value of the member, or a comma-separated list of values. By default, however, the EnumConverter class only allows names of enumeration members, and comma-separated values are only allowed if the enumeration has the FlagsAttribute attribute.

Using the ValidateEnumValueAttribute, you can change these behaviors. You can allow numeric values with the AllowNumericValues property, and you can force whether comma-separated values are allowed with the AllowCommaSeparatedValues property.

If numeric values are allowed, they are restricted to values that are defined in the enumeration, unless the enumeration has the FlagsAttribute applied or you set the AllowNonDefinedValues property to TriStateTrue. In that case, for example using the DayOfWeek enumeration, converting a string value of "9" would result in a value of (DayOfWeek)9, even though there is no enumeration member with that value.

In addition, this validator provides usage help listing all the possible values. If the enumeration has a lot of values, you may wish to turn this off by setting the ArgumentValidationWithHelpAttributeIncludeInUsageHelp property to . Similarly, you can avoid listing all the values in the error message by setting the IncludeValuesInErrorMessage property to .

If this validator is used without changing any of its properties on an argument that uses the default EnumConverter, its only effect is to list the values in the usage help. The default behavior of the EnumConverter class is the same as the defaults of this validator.

Constructors

ValidateEnumValueAttributeInitializes a new instance of the ValidateEnumValueAttribute class

Properties

AllowCommaSeparatedValues Gets or sets a value that indicates whether the value provided by the user can use commas to provide multiple values that will be combined with bitwise-or.
AllowNonDefinedValues Gets or sets a value that indicates whether values that do not match one of the enumeration's defined values are allowed.
AllowNumericValues Gets or sets a value that indicates whether the value provided by the user can the underlying numeric type of the enumeration.
CaseSensitive Gets or sets a value that indicates whether enumeration value conversion is case sensitive.
ErrorCategory Gets the error category used for the CommandLineArgumentException when validation fails.
(Inherited from ArgumentValidationAttribute)
IncludeInUsageHelp Gets or sets a value that indicates whether this validator's help should be included in the argument's description.
(Inherited from ArgumentValidationWithHelpAttribute)
IncludeValuesInErrorMessage Gets or sets a value that indicates whether the possible values of the enumeration should be included in the error message if validation fails.
TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute.
(Inherited from Attribute)

Methods

EqualsReturns a value that indicates whether this instance is equal to a specified object.
(Inherited from Attribute)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetErrorMessage Gets the error message to display if validation failed.
(Overrides ArgumentValidationAttributeGetErrorMessage(CommandLineArgument, Object))
GetHashCodeReturns the hash code for this instance.
(Inherited from Attribute)
GetTypeGets the Type of the current instance.
(Inherited from Object)
GetUsageHelp Gets the usage help message for this validator.
(Inherited from ArgumentValidationWithHelpAttribute)
GetUsageHelpCore Gets the usage help message for this validator.
(Overrides ArgumentValidationWithHelpAttributeGetUsageHelpCore(CommandLineArgument))
IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.
(Inherited from Attribute)
IsValidPostConversionDetermines if the argument's value is defined.
(Overrides ArgumentValidationAttributeIsValidPostConversion(CommandLineArgument, Object))
IsValidPostParsing Determines if the argument's value is valid after all arguments have been parsed.
(Inherited from ArgumentValidationAttribute)
IsValidPreConversionDetermines if the argument's value contains commas or numbers if not allowed.
(Overrides ArgumentValidationAttributeIsValidPreConversion(CommandLineArgument, ReadOnlyMemoryChar))
MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object.
(Inherited from Attribute)
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
ToStringReturns a string that represents the current object.
(Inherited from Object)
ValidatePostConversion Validates the argument value after it was converted to the argument's type, and throws an exception if validation failed.
(Inherited from ArgumentValidationAttribute)
ValidatePostParsing Validates the argument value after it was converted to the argument's type, and throws an exception if validation failed.
(Inherited from ArgumentValidationAttribute)
ValidatePreConversion Validates the argument raw argument value, and throws an exception if validation failed.
(Inherited from ArgumentValidationAttribute)

Thread Safety

Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.

See Also