mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-20 13:48:16 +08:00
Add Spectre.Cli to Spectre.Console
* Renames Spectre.Cli to Spectre.Console.Cli. * Now uses Verify with Spectre.Console.Cli tests. * Removes some duplicate definitions. Closes #168
This commit is contained in:

committed by
Patrik Svensson

parent
0bbf9b81a9
commit
0ae419326d
57
src/Spectre.Console/Cli/FlagValue.cs
Normal file
57
src/Spectre.Console/Cli/FlagValue.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Spectre.Console.Cli
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of a flag with an optional value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The flag's element type.</typeparam>
|
||||
public sealed class FlagValue<T> : IFlagValue
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the flag was set or not.
|
||||
/// </summary>
|
||||
public bool IsSet { get; set; }
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
|
||||
/// <summary>
|
||||
/// Gets or sets the flag's value.
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
|
||||
|
||||
/// <inheritdoc/>
|
||||
Type IFlagValue.Type => typeof(T);
|
||||
|
||||
/// <inheritdoc/>
|
||||
object? IFlagValue.Value
|
||||
{
|
||||
get => Value;
|
||||
set
|
||||
{
|
||||
#pragma warning disable CS8601 // Possible null reference assignment.
|
||||
Value = (T)value;
|
||||
#pragma warning restore CS8601 // Possible null reference assignment.
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user