namespace Spectre.Console.Cli;
///
/// Implementation of a flag with an optional value.
///
/// The flag's element type.
public sealed class FlagValue : IFlagValue
{
///
/// Gets or sets a value indicating whether or not the flag was set or not.
///
public bool IsSet { get; set; }
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
///
/// Gets or sets the flag's value.
///
public T Value { get; set; }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
///
Type IFlagValue.Type => typeof(T);
///
object? IFlagValue.Value
{
get => Value;
set
{
#pragma warning disable CS8601 // Possible null reference assignment.
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
Value = (T)value;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning restore CS8601 // Possible null reference assignment.
}
}
///
public override string ToString()
{
var flag = (IFlagValue)this;
if (flag.Value != null)
{
return string.Format(
CultureInfo.InvariantCulture,
"Set={0}, Value={1}",
IsSet,
flag.Value.ToString());
}
return string.Format(
CultureInfo.InvariantCulture,
"Set={0}", IsSet);
}
}