Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

namespace Spectre.Console.Tests.Unit.Cli.Annotations;

public sealed partial class CommandOptionAttributeTests
{
[Fact]
public void Should_Write_Deprecation_Warning()
{
//Given, When
var fixture = new CommandAppTester();
fixture.Configure(configurator => configurator.AddCommand<DeprecatedOptionCommand>("cmd"));
var result = fixture.Run("cmd", "-d", "yes");

// Then
result.Output.ShouldContain("Warning");
result.Output.ShouldContain("This option is deprecated and subject to removal.");
}

private sealed class DeprecatedOptionSettings : CommandSettings
{
[CommandOption("-d|--deprecated <VALUE>")]
[Obsolete("This option is deprecated and subject to removal.")]
public string? Deprecated { get; set; }
}

private sealed class DeprecatedOptionCommand : Command<DeprecatedOptionSettings>
{
protected override int Execute(CommandContext context, DeprecatedOptionSettings settings, CancellationToken cancellationToken)
{
return 0;
}
}
}
38 changes: 38 additions & 0 deletions src/Spectre.Console.Cli/Internal/Binding/CommandValueResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public static CommandValueLookup GetParameterValues(CommandTree? tree, ITypeReso
var lookup = new CommandValueLookup();
var binder = new CommandValueBinder(lookup);

// Track which deprecated options we've warned about to avoid spamming the console.
var warnedDeprecatedOptions = new HashSet<string>(StringComparer.Ordinal);

CommandValidator.ValidateRequiredParameters(tree);

while (tree != null)
Expand Down Expand Up @@ -56,6 +59,41 @@ public static CommandValueLookup GetParameterValues(CommandTree? tree, ITypeReso
// Process mapped parameters.
foreach (var mapped in tree.Mapped)
{
if (mapped.Parameter is CommandOption commandOption)
{
string? deprecationMessage = null;
try
{
var prop = mapped.Parameter.Property;
var obsoleteAttr = prop.GetCustomAttribute<ObsoleteAttribute>(false);
if (obsoleteAttr is not null && !string.IsNullOrWhiteSpace(obsoleteAttr.Message))
{
deprecationMessage = obsoleteAttr.Message;
}
}
catch
{
// Just consume so we do not block binding
}

var isDeprecated = deprecationMessage != null;
var optionName = commandOption.LongNames.Count > 0 ? commandOption.LongNames[0]
: commandOption.ShortNames.Count > 0 ? commandOption.ShortNames[0]
: commandOption.GetOptionName();
if (isDeprecated && warnedDeprecatedOptions.Add(optionName))
{
try
{
if (resolver.Resolve(typeof(IAnsiConsole)) is IAnsiConsole console)
{
var msg = deprecationMessage ?? $"Option '{optionName}' is deprecated.";
console.MarkupLine($"[yellow]Warning: {msg.EscapeMarkup()}[/]");
}
}
catch { }
}
}

if (mapped.Parameter.WantRawValue)
{
// Just try to assign the raw value.
Expand Down