Skip to content

DotMake.CommandLine v1.7.2

Compare
Choose a tag to compare
@calacayir calacayir released this 29 Jan 00:59
· 26 commits to main since this release
  • Added advanced validation. In [CliOption] and [CliArgument] attributes, removed AllowExisting property and added ValidationRules, ValidationPattern, ValidationMessage properties.
    ValidationRules property allows setting predefined validation rules such as ExistingFile, NonExistingFile, ExistingDirectory,
    NonExistingDirectory, ExistingFileOrDirectory, NonExistingFileOrDirectory, LegalPath, LegalFileName, LegalUri, LegalUrl.
    Validation rules can be combined.
    ValidationPattern property allows setting a regular expression pattern for custom validation,
    and ValidationMessage property allows setting a custom error message to show when ValidationPattern does not match.

    [CliCommand]
    public class ValidationCliCommand
    {
        [CliOption(Required = false, ValidationRules = CliValidationRules.ExistingFile)]
        public FileInfo OptFile1 { get; set; }
    
        [CliOption(Required = false, ValidationRules = CliValidationRules.NonExistingFile | CliValidationRules.LegalPath)]
        public string OptFile2 { get; set; }
    
        [CliOption(Required = false, ValidationRules = CliValidationRules.ExistingDirectory)]
        public DirectoryInfo OptDir { get; set; }
    
        [CliOption(Required = false, ValidationPattern = @"(?i)^[a-z]+$")]
        public string OptPattern1 { get; set; }
    
        [CliOption(Required = false, ValidationPattern = @"(?i)^[a-z]+$", ValidationMessage = "Custom error message")]
        public string OptPattern2 { get; set; }
    
        [CliOption(Required = false, ValidationRules = CliValidationRules.LegalUrl)]
        public string OptUrl { get; set; }
    
        [CliOption(Required = false, ValidationRules = CliValidationRules.LegalUri)]
        public string OptUri { get; set; }
    
        [CliArgument(Required = false, ValidationRules = CliValidationRules.LegalFileName)]
        public string OptFileName { get; set; }
    
        public void Run(InvocationContext context)
        {
            context.ShowValues();
        }
    }