Table of Contents

Class ValidationExtensions

Namespace
McMaster.Extensions.CommandLineUtils
Assembly
McMaster.Extensions.CommandLineUtils.dll

Extension methods for adding validation rules to options and arguments.

public static class ValidationExtensions
Inheritance
object
ValidationExtensions

Methods

Accepts(CommandArgument)

Creates a builder for specifying a set of rules used to determine if input is valid.

public static IArgumentValidationBuilder Accepts(this CommandArgument argument)

Parameters

argument CommandArgument

The argument.

Returns

IArgumentValidationBuilder

The builder.

Accepts(CommandArgument, Action<IArgumentValidationBuilder>)

Specifies a set of rules used to determine if input is valid.

public static CommandArgument Accepts(this CommandArgument argument, Action<IArgumentValidationBuilder> configure)

Parameters

argument CommandArgument

The argument.

configure Action<IArgumentValidationBuilder>

A function to configure rules on the validation builder.

Returns

CommandArgument

The argument.

Accepts(CommandOption)

Creates a builder for specifying a set of rules used to determine if input is valid.

public static IOptionValidationBuilder Accepts(this CommandOption option)

Parameters

option CommandOption

The option.

Returns

IOptionValidationBuilder

The builder.

Accepts(CommandOption, Action<IOptionValidationBuilder>)

Specifies a set of rules used to determine if input is valid.

public static CommandOption Accepts(this CommandOption option, Action<IOptionValidationBuilder> configure)

Parameters

option CommandOption

The option.

configure Action<IOptionValidationBuilder>

A function to configure rules on the validation builder.

Returns

CommandOption

The option.

Accepts<T>(CommandArgument<T>)

Creates a builder for specifying a set of rules used to determine if input is valid.

public static IArgumentValidationBuilder<T> Accepts<T>(this CommandArgument<T> argument)

Parameters

argument CommandArgument<T>

The argument.

Returns

IArgumentValidationBuilder<T>

The builder.

Type Parameters

T

Accepts<T>(CommandArgument<T>, Action<IArgumentValidationBuilder<T>>)

Specifies a set of rules used to determine if input is valid.

public static CommandArgument<T> Accepts<T>(this CommandArgument<T> argument, Action<IArgumentValidationBuilder<T>> configure)

Parameters

argument CommandArgument<T>

The argument.

configure Action<IArgumentValidationBuilder<T>>

A function to configure rules on the validation builder.

Returns

CommandArgument<T>

The argument.

Type Parameters

T

Accepts<T>(CommandOption<T>)

Creates a builder for specifying a set of rules used to determine if input is valid.

public static IOptionValidationBuilder<T> Accepts<T>(this CommandOption<T> option)

Parameters

option CommandOption<T>

The option.

Returns

IOptionValidationBuilder<T>

The builder.

Type Parameters

T

Accepts<T>(CommandOption<T>, Action<IOptionValidationBuilder<T>>)

Specifies a set of rules used to determine if input is valid.

public static CommandOption<T> Accepts<T>(this CommandOption<T> option, Action<IOptionValidationBuilder<T>> configure)

Parameters

option CommandOption<T>

The option.

configure Action<IOptionValidationBuilder<T>>

A function to configure rules on the validation builder.

Returns

CommandOption<T>

The option.

Type Parameters

T

EmailAddress(IValidationBuilder, string?)

Specifies that values must be a valid email address.

public static IValidationBuilder EmailAddress(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

Enum<TEnum>(IValidationBuilder, bool)

Specifies that values must be one of the values in a given set.

By default, value comparison is case-sensitive. To make matches case-insensitive, set ignoreCase to true.

public static IValidationBuilder Enum<TEnum>(this IValidationBuilder builder, bool ignoreCase = false) where TEnum : struct

Parameters

builder IValidationBuilder

The builder.

ignoreCase bool

Ignore case when parsing enums.

Returns

IValidationBuilder

The builder.

Type Parameters

TEnum

Exceptions

ArgumentException

When TEnum is not an enum.

ExistingDirectory(IValidationBuilder, string?)

Specifies that values must be a path to a directory that already exists.

public static IValidationBuilder ExistingDirectory(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

ExistingFile(IValidationBuilder, string?)

Specifies that values must be a path to a file that already exists.

public static IValidationBuilder ExistingFile(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

ExistingFileOrDirectory(IValidationBuilder, string?)

Specifies that values must be a valid file path or directory, and the file path must already exist.

public static IValidationBuilder ExistingFileOrDirectory(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

IsRequired(CommandArgument, bool, string?)

Indicates the argument is required.

public static CommandArgument IsRequired(this CommandArgument argument, bool allowEmptyStrings = false, string? errorMessage = null)

Parameters

argument CommandArgument

The argument.

allowEmptyStrings bool

Indicates whether an empty string is allowed.

errorMessage string

The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.

Returns

CommandArgument

The argument.

IsRequired(CommandOption, bool, string?)

Indicates the option is required.

public static CommandOption IsRequired(this CommandOption option, bool allowEmptyStrings = false, string? errorMessage = null)

Parameters

option CommandOption

The option.

allowEmptyStrings bool

Indicates whether an empty string is allowed.

errorMessage string

The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.

Returns

CommandOption

The option.

IsRequired<T>(CommandArgument<T>, bool, string?)

Indicates the argument is required.

public static CommandArgument<T> IsRequired<T>(this CommandArgument<T> argument, bool allowEmptyStrings = false, string? errorMessage = null)

Parameters

argument CommandArgument<T>

The argument.

allowEmptyStrings bool

Indicates whether an empty string is allowed.

errorMessage string

The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.

Returns

CommandArgument<T>

The argument.

Type Parameters

T

IsRequired<T>(CommandOption<T>, bool, string?)

Indicates the option is required.

public static CommandOption<T> IsRequired<T>(this CommandOption<T> option, bool allowEmptyStrings = false, string? errorMessage = null)

Parameters

option CommandOption<T>

The option.

allowEmptyStrings bool

Indicates whether an empty string is allowed.

errorMessage string

The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.

Returns

CommandOption<T>

The option.

Type Parameters

T

LegalFilePath(IValidationBuilder, string?)

Specifies that values must be legal file paths.

public static IValidationBuilder LegalFilePath(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

MaxLength(IValidationBuilder, int, string?)

Specifies that values must be a string no more than length characters long.

public static IValidationBuilder MaxLength(this IValidationBuilder builder, int length, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

length int

The maximum length.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

MinLength(IValidationBuilder, int, string?)

Specifies that values must be a string at least length characters long.

public static IValidationBuilder MinLength(this IValidationBuilder builder, int length, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder

length int

The minimum length.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

NonExistingDirectory(IValidationBuilder, string?)

Specifies that values must be a path to a directory that does not already exist.

public static IValidationBuilder NonExistingDirectory(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

NonExistingFile(IValidationBuilder, string?)

Specifies that values must be a path to a file that does not already exist.

public static IValidationBuilder NonExistingFile(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

NonExistingFileOrDirectory(IValidationBuilder, string?)

Specifies that values must be a valid file path or directory, and the file path must not already exist.

public static IValidationBuilder NonExistingFileOrDirectory(this IValidationBuilder builder, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

OnValidate(CommandArgument, Func<ValidationContext, ValidationResult>)

Adds a validator that runs after parsing is complete and before command execution.

public static CommandArgument OnValidate(this CommandArgument argument, Func<ValidationContext, ValidationResult> validate)

Parameters

argument CommandArgument

The argument.

validate Func<ValidationContext, ValidationResult>

The callback. Return Success if there is no error.

Returns

CommandArgument

OnValidate(CommandLineApplication, Func<ValidationContext, ValidationResult>)

Adds a validator that runs after parsing is complete and before command execution.

public static CommandLineApplication OnValidate(this CommandLineApplication command, Func<ValidationContext, ValidationResult> validate)

Parameters

command CommandLineApplication

The command.

validate Func<ValidationContext, ValidationResult>

The callback. Return Success if there is no error.

Returns

CommandLineApplication

OnValidate(CommandOption, Func<ValidationContext, ValidationResult>)

Adds a validator that runs after parsing is complete and before command execution.

public static CommandOption OnValidate(this CommandOption option, Func<ValidationContext, ValidationResult> validate)

Parameters

option CommandOption

The option.

validate Func<ValidationContext, ValidationResult>

The callback. Return Success if there is no error.

Returns

CommandOption

Range(IValidationBuilder<double>, double, double, string?)

Specifies that values must be in a given range.

public static IValidationBuilder<double> Range(this IValidationBuilder<double> builder, double minimum, double maximum, string? errorMessage = null)

Parameters

builder IValidationBuilder<double>

The builder.

minimum double

The minimum allowed value.

maximum double

The maximum allowed value.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder<double>

The builder.

Range(IValidationBuilder<int>, int, int, string?)

Specifies that values must be in a given range.

public static IValidationBuilder<int> Range(this IValidationBuilder<int> builder, int minimum, int maximum, string? errorMessage = null)

Parameters

builder IValidationBuilder<int>

The builder.

minimum int

The minimum allowed value.

maximum int

The maximum allowed value.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder<int>

The builder.

RegularExpression(IValidationBuilder, string, string?)

Specifies that values must match a regular expression.

public static IValidationBuilder RegularExpression(this IValidationBuilder builder, string pattern, string? errorMessage = null)

Parameters

builder IValidationBuilder

The builder.

pattern string

The regular expression.

errorMessage string

A custom error message to display.

Returns

IValidationBuilder

The builder.

Satisfies<TAttribute>(IValidationBuilder, string?, params object[])

Specifies that values must satisfy the requirements of the validation attribute of type TAttribute.

public static IValidationBuilder Satisfies<TAttribute>(this IValidationBuilder builder, string? errorMessage = null, params object[] ctorArgs) where TAttribute : ValidationAttribute

Parameters

builder IValidationBuilder

The builder.

errorMessage string

A custom error message to display.

ctorArgs object[]

Constructor arguments for TAttribute.

Returns

IValidationBuilder

The builder.

Type Parameters

TAttribute

The validation attribute type.

Values(IValidationBuilder, bool, params string[])

Specifies that values must be one of the values in a given set.

public static IValidationBuilder Values(this IValidationBuilder builder, bool ignoreCase, params string[] allowedValues)

Parameters

builder IValidationBuilder

The builder.

ignoreCase bool

Ignore case when comparing inputs to allowedValues.

allowedValues string[]

Allowed values.

Returns

IValidationBuilder

The builder.

Values(IValidationBuilder, StringComparison, params string[])

Specifies that values must be one of the values in a given set.

public static IValidationBuilder Values(this IValidationBuilder builder, StringComparison comparer, params string[] allowedValues)

Parameters

builder IValidationBuilder

The builder.

comparer StringComparison

The comparer used to determine if values match.

allowedValues string[]

Allowed values.

Returns

IValidationBuilder

The builder.

Values(IValidationBuilder, params string[])

Specifies that values must be one of the values in a given set.

By default, value comparison is case-sensitive. To make matches case-insensitive, use Values(IValidationBuilder, bool, params string[]).

public static IValidationBuilder Values(this IValidationBuilder builder, params string[] allowedValues)

Parameters

builder IValidationBuilder

The builder.

allowedValues string[]

Allowed values.

Returns

IValidationBuilder

The builder.