feat: add command usage examples

Add an Examples string array property to CommandAttribute so command authors can document example invocations. Each example contains only the arguments that follow the command name; the executable and command name are prepended automatically when the example is rendered.

Examples are rendered as extra lines in the USAGE section of the help text. The source generator resolves the values from the attribute and emits them into the command descriptor consumed at runtime.
This commit is contained in:
dualfroz committed 2026-09-06 01:19:59 +02:00
1 parent e3e64a40e4
commit 8f11b8ff38
6 files changed
+132 -3

No files matched your search

+12 -1
View File
@@ -3,6 +3,7 @@ using System.Linq;
using CliFx.Generators.Utils;
using CliFx.Generators.Utils.Extensions;
using Microsoft.CodeAnalysis;
using PowerKit.Extensions;
namespace CliFx.Generators.Binding;
@@ -10,6 +11,7 @@ internal partial record CommandSymbol(
INamedTypeSymbol Type,
string? Name,
string? Description,
IReadOnlyList<string> Examples,
IReadOnlyList<CommandInputSymbol> Inputs
)
{
@@ -71,10 +73,19 @@ internal partial record CommandSymbol
attribute?.NamedArguments.FirstOrDefault(a => a.Key == "Description").Value.Value
as string;
var examples =
attribute
?.NamedArguments.Where(a => a.Key == "Examples")
.SelectMany(a => a.Value.Values)
.Select(v => v.Value as string)
.WhereNotNull()
.ToArray()
?? [];
var properties = type.GetProperties().ToArray();
var parameters = CommandParameterSymbol.Resolve(properties, diagnostics);
var options = CommandOptionSymbol.Resolve(properties, diagnostics);
return new CommandSymbol(type, name, description, [.. parameters, .. options]);
return new CommandSymbol(type, name, description, examples, [.. parameters, .. options]);
}
}