Skip to content

Commit abb1226

Browse files
Add generic annotation accessor
This was later moved to its own file
1 parent b2ac225 commit abb1226

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/System.CommandLine.Subsystems/Subsystems/Annotations/AnnotationAccessor.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,21 @@ public struct AnnotationAccessor<TValue>(CliSubsystem owner, AnnotationId<TValue
3636
/// <returns>True if the value was found, false otherwise.</returns>
3737
public readonly bool TryGet(CliSymbol symbol, [NotNullWhen(true)] out TValue? value) => owner.TryGetAnnotation(symbol, Id, out value);
3838
}
39+
40+
/// <summary>
41+
/// Allows associating an annotation with a <see cref="CliSymbol"/>. The annotation will be stored by the accessor's owner <see cref="CliSubsystem"/>.
42+
/// </summary>
43+
public struct ValueAnnotationAccessor<TValue>(CliSubsystem owner, AnnotationId<TValue> id)
44+
{
45+
/// <summary>
46+
/// The ID of the annotation
47+
/// </summary>
48+
public AnnotationId<TValue> Id { get; }
49+
public readonly void Set<TSymbolValue>(CliOption<TSymbolValue> symbol, TSymbolValue value)
50+
where TSymbolValue : TValue
51+
=> owner.SetAnnotation(symbol, id, value);
52+
public readonly void Set<TSymbolValue>(CliArgument<TSymbolValue> symbol, TSymbolValue value)
53+
where TSymbolValue : TValue
54+
=> owner.SetAnnotation(symbol, id, value);
55+
public readonly bool TryGet(CliSymbol symbol, [NotNullWhen(true)] out TValue? value) => owner.TryGetAnnotation(symbol, id, out value);
56+
}

0 commit comments

Comments
 (0)