pretty-enum 3.0.0

dotnet add package pretty-enum --version 3.0.0                
NuGet\Install-Package pretty-enum -Version 3.0.0                
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="pretty-enum" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pretty-enum --version 3.0.0                
#r "nuget: pretty-enum, 3.0.0"                
#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 pretty-enum as a Cake Addin
#addin nuget:?package=pretty-enum&version=3.0.0

// Install pretty-enum as a Cake Tool
#tool nuget:?package=pretty-enum&version=3.0.0                

pretty-enum

NuGet

A .NET Standard library for pretty-printing enum values

Usage

pretty-enum aims to be as easy to use out-of-the-box as possible.

All you need for the examples below is using PrettyEnum;.

To use the default pretty-printer to format an enum value, just use enum.PrettyPrint(). It will automatically format PascalCase, camelCase, Snake_Case, and UPPER_SNAKE_CASE (as well as MixedVersions_OfThem) to Title Case.

Examples:

StringSplitOptions.RemoveEmptyEntries.PrettyPrint() == "Remove Empty Entries"
SomeEnum.UPPER_SNAKE_CASE.PrettyPrint() == "Upper Snake Case"
SomeEnum.MixedCamel_AndSnake.PrettyPrint() == "Mixed Camel And Snake"

To specify a custom value to be used when pretty-printng, annotate the enum field with PrettyNameAttribute, like so:

using PrettyEnum;

public enum Color {
  Red,
  DarkRed,
  [PrettyName("Even Darker Red")]
  ReallyDarkRed
}

// PrettyPrint() will use the value specified in the attribute:
Color.ReallyDarkRed.PrettyPrint() == "Even Darker Red"

PrettyEnum also recognizes DescriptionAttribute from System.ComponentModel (although PrettyNameAttribute takes precedence if both are present).

You can also annotate either enum fields or whole enum types with the IgnorePrettyPrintAttribute, in which case the pretty-printer will just use the name of the enum field(s).

It's also possible to get a ReadOnlySpan containing all the pretty names of a particular enum type:

Pretty.GetNames<DeliveryOptions>() == ["Same Day", "Fragile", "Contactless"]
Pretty.GetNames<Color>() == ["Red", "Dark Red", "Even Darker Red"]

Flag-like Enums

The library can also handle flag-like enums (i.e. enums annotated wtih FlagsAttribute).

Each individual field is formatted using the above-described rules, and values that contain multiple flags are formatted by joining the names of each flag with a user-provided separator (or, by default, |).

Example:

[Flags]
enum DeliveryOptions {
  SameDay,
  [PrettyName("Fragile")]
  ExtraPackaging,
  Contactless
}

(DeliveryOptions.SameDay | DeliveryOptions.ExtraPackaging).PrettyPrint() == "Same Day | Fragile"
(DeliveryOptions.SameDay | DeliveryOptions.Contactless).PrettyPrint(", ") == "Same Day, Contactless"

Parsing

pretty-enum also supports parsing pretty-printed strings back into their corresponding enum values, including values containing multiple flags.

Two methods are provided for this purpse: Pretty.TryParse (which returns a bool indicating success or failure, and writes the parsed enum value to an out parameter), and Pretty.Parse (which directly returns the parsed enum value, or throws an exception on failure).

Example:

Pretty.Parse<DeliveryOptions>("Same Day | Fragile") == (DeliveryOptions.SameDay | DeliveryOptions.ExtraPackaging)
Pretty.Parse<BindingFlags>("Static & Public", " & ") == (BindingFlags.Public | BindingFlags.Static)
Pretty.TryParse<DeliveryOptions>("Same Day, Contactless", out var value, flagSeparator: ", ") // returns true, value == (DeliveryOptions.SameDay | DeliveryOptions.Contactless)

License

This repository is licensed under the terms of the MIT License. For more details, see the license file.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.
  • .NETStandard 2.1

    • No dependencies.
  • net7.0

    • No dependencies.

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
3.0.0 203 6/16/2023
2.0.0 398 3/28/2021
1.3.0 352 3/13/2021
1.2.3 503 10/2/2020
1.2.2 454 10/2/2020
1.2.1 440 9/30/2020
1.2.0 439 9/30/2020
1.1.0 586 9/30/2020
1.0.0 419 9/29/2020