SnapCLI.DataAnnotations 1.0.1-pre

This is a prerelease version of SnapCLI.DataAnnotations.
dotnet add package SnapCLI.DataAnnotations --version 1.0.1-pre                
NuGet\Install-Package SnapCLI.DataAnnotations -Version 1.0.1-pre                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SnapCLI.DataAnnotations" Version="1.0.1-pre" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SnapCLI.DataAnnotations --version 1.0.1-pre                
#r "nuget: SnapCLI.DataAnnotations, 1.0.1-pre"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SnapCLI.DataAnnotations as a Cake Addin
#addin nuget:?package=SnapCLI.DataAnnotations&version=1.0.1-pre&prerelease

// Install SnapCLI.DataAnnotations as a Cake Tool
#tool nuget:?package=SnapCLI.DataAnnotations&version=1.0.1-pre&prerelease                

SnapCLI.DataAnnotations

This extension for the SnapCLIđź—— library enables validation of command-line arguments using DataAnnotationsđź—— validation attributes. It allows you to ensure that command-line input meets specified criteria before executing the command.

Prerequisite

Your CLI application must be built using the SnapCLIđź—— library and its API.

Usage

To enable validation, add the following code to your CLI application:

using SnapCLI;

[Startup]
public static void Startup()
{
    CLI.BeforeCommand += (args) => args.ParseResult.ValidateDataAnnotations();
}

With this code, any argument or option declared with the SnapCLI API and annotated with validation attributes will be validated before the command is executed.

If validation fails, a ValidationExceptionđź—— will be thrown, detailing the failed option or argument. Note that the CLI application may implement a custom exception handler to process exceptions according to the application's needs.

Validation Attributes

The System.ComponentModel.DataAnnotationsđź—— library provides a variety of validation attributes, such as [Range], [StringLength], and [FileExtensions], that can be used for argument validation. Note that the System.ComponentModel.DataAnnotations NuGet package must be installed in your project to utilize these attributes.

Additionally, the SnapCLI.DataAnnotations namespace provides several extra validation attributes useful for command-line arguments validation:

  • [FileExists]
  • [FileNotExists]
  • [DirectoryExists]
  • [DirectoryNotExists]

Example

The sample code below demonstrates the usage of various validation attributes, including custom validation via the [CustomValidation]đź—— attribute.

using SnapCLI;
using SnapCLI.DataAnnotations;
using System.ComponentModel.DataAnnotations;

[Startup]
public static void Startup()
{
    // enable validation
    CLI.BeforeCommand += (args) => args.ParseResult.ValidateDataAnnotations();
}

[Command(name: "quotes read", description: "Read and display the file.")]
public static async Task ReadQuotesFile(
    [Argument(name: "file", description: "File containing quotes")]
    [FileExtensions(Extensions = "txt,quotes")]
    [FileExists]
    [CustomValidation(typeof(FileCustomValidation), "FileNotEmpty")]
    FileInfo file,

    [Option(description: "Delay between lines, specified as milliseconds per character in a line.")]
    [Range(0, 1000)]
    int delay = 42,

    [Option(name: "fgcolor", description: "Foreground color of text displayed on the console.")]
    [AllowedValues(ConsoleColor.White, ConsoleColor.Red, ConsoleColor.Blue, ConsoleColor.Green)]
    ConsoleColor fgColor = ConsoleColor.White)
{
    Console.ForegroundColor = fgColor;
    foreach (string line in File.ReadLines(file.FullName))
    {
        Console.WriteLine(line);
        await Task.Delay(delay * line.Length);
    };
}

public static class FileCustomValidation
{
    public static ValidationResult FileNotEmpty(object? obj)
    {
        ArgumentNullException.ThrowIfNull(obj);
        if (obj is string s)
            obj = new FileInfo(s);
        if (obj is not FileInfo fileInfo)
            throw new NotSupportedException($"FileNotEmpty validation doesn't support {obj.GetType()}");
        if (!fileInfo.Exists)
            return new ValidationResult($"File {fileInfo.FullName} doesn't exist");
        if (fileInfo.Length == 0)
            return new ValidationResult($"File {fileInfo.FullName} is empty");
        return ValidationResult.Success!;
    }
}

NuGet Package

The library is available as a NuGet package:

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1-pre 38 11/13/2024
1.0.0-pre 46 11/13/2024

Fix links in package description