Skip to content

Required command line arguments

Alex Kamsteeg edited this page Nov 12, 2017 · 4 revisions

AtleX.CommmandLineArguments has the notion of optional and required arguments. By default, all arguments are optional. The user is not required to supply them and parsing and validation succeeds when they're omitted from the command line.

Required arguments however, are just like the name suggests: Required. The user must specify them otherwise validation will fail and the result of CommandLineArguments.TryParse() will be false.

To mark an argument as required, decorate the property in the Arguments class with a [Required] attribute:

internal sealed class CliArguments : Arguments
{
    [Required]
    public string Name
    {
        get;
        set;
    }
}

The [Required] attribute has a parameter ErrorMessage which you can use to get a custom error message if the value was not set. Also, for required arguments without a value (/enableStatus) you can set the AllowEmptyString parameter to false.

internal sealed class CliArguments : Arguments
{
    [Required(ErrorMessage = "EnableStatus is required", AllowEmptyStrings = true)]
    public bool EnableStatus
    {
        get;
        set;
    }
}

Validating required arguments is done by the built-in RequiredArgumentValidator which must be set in the configuration.

Clone this wiki locally