RequiresAnyAttribute Class

Validates whether at least one of the specified arguments is supplied.

Definition

Namespace: Ookii.CommandLine.Validation
Assembly: Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 4.2.0+a4d3631e4dcc0970081ed435288efdebc1325f83
C#
public class RequiresAnyAttribute : ClassValidationAttribute
Inheritance
Object    Attribute    ClassValidationAttribute    RequiresAnyAttribute

Remarks

This is a class validator, which should be applied to the class that defines arguments, not to a specific argument.

Use this attribute if you have multiple arguments, only one of which needs to be supplied at a time.

This attribute is useful when combined with the ProhibitsAttribute attribute. If you have two mutually exclusive attribute, you cannot mark either of them as required. For example, given arguments A and B, if B prohibits A but A is required, then B can never be used.

Instead, you can use the RequiresAnyAttribute attribute to indicate that the user must supply either A or B, and the ProhibitsAttribute attribute to indicate that they cannot supply both at once.

C#
[RequiresAny(nameof(Address), nameof(Path))]
class Arguments
{
    [CommandLineArgument]
    public Uri Address { get; set; }

    [CommandLineArgument]
    [Prohibits(nameof(Address))]
    public string Path { get; set; }
}

  Note

You can only use nameof if the name of the argument matches the name of the property. Be careful if you have explicit names or are using a NameTransform.

The names of the arguments are not validated when the attribute is created. If one of the specified arguments does not exist, an exception is thrown during validation.

Constructors

RequiresAnyAttribute(String) Initializes a new instance of the RequiresAnyAttribute class.
RequiresAnyAttribute(String, String) Initializes a new instance of the RequiresAnyAttribute class.

Properties

Arguments Gets the names of the arguments, one of which must be supplied on the command line.
ErrorCategory Gets the error category used for the CommandLineArgumentException when validation fails.
(Overrides ClassValidationAttributeErrorCategory)
IncludeInUsageHelp Gets or sets a value that indicates whether this validator's help should be included in the usage help.
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)
GetArguments Resolves the argument names in the Arguments property to their actual CommandLineArgument instances.
GetErrorMessage Gets the error message to display if validation failed.
(Overrides ClassValidationAttributeGetErrorMessage(CommandLineParser))
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.
(Overrides ClassValidationAttributeGetUsageHelp(CommandLineParser))
IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.
(Inherited from Attribute)
IsValid Determines if at least one of the arguments in the Arguments property was supplied on the command line.
(Overrides ClassValidationAttributeIsValid(CommandLineParser))
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)
Validate Validates the argument value, and throws an exception if validation failed.
(Inherited from ClassValidationAttribute)

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