Home Basic usage Handle validation errors
Post
Cancel

Handle validation errors

During the initialization, Cliargs.NET execute all the defined rules for each argument, if any rule fails to get validated, Cliargs.NET registers the errors.

Check if validation failed

To know if any rule fails to get validated during the validation process, use the static property AppCliArgs.HasValidationErrors.

1
2
3
4
if(AppCliArgs.HasValidationErrors) 
{
    // Do stuff
}

Get validation errors

The validation errors could be retrieved by calling the static method AppCliArgs.GetValidationResults(). This method returns only validation results for rules failed to get validated.

1
2
3
4
5
6
7
8
9
10
if(AppCliArgs.HasValidationErrors)
{
    // Here you get the validation results for errors
    var validationResults = AppCliArgs.GetValidationResults();
    foreach (var result in validationResults)
    {
        // do stuff 
    }
    return;
}

Validation results

The validation result contains several information about the validation, such as the rule name, the error message… or a global report.

The validation result Report is a string contains all the information, rule, message, the usage of the argumet… More info here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(AppCliArgs.HasValidationErrors)
{
    var validationResults = AppCliArgs.GetValidationResults();
    foreach (var result in validationResults)
    {
        // Here you access to the results 
        Console.WriteLine(result.GetReport());
        Console.WriteLine(result.ValidationError);
        Console.WriteLine(result.RuleName);
        Console.WriteLine(result.ArgName);
        Console.WriteLine(result.Usage);
    }

    return;
}