Skip to content

Commit da0b357

Browse files
Merge pull request #2412 from KathleenDollard/RemoveGetSetDescriptionFromHelpSubsystem
refactor: Remove Get/Set Description on Helper subsystem; add Get to AnnotationAccessor
2 parents 1e494b8 + fae9a17 commit da0b357

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/System.CommandLine.Subsystems.Tests/AlternateSubsystems.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.CommandLine.Directives;
5-
using System.CommandLine.Subsystems;
6-
using System.CommandLine.Subsystems.Annotations;
75

86
namespace System.CommandLine.Subsystems.Tests
97
{
@@ -33,9 +31,9 @@ public VersionThatUsesHelpData(CliSymbol symbol)
3331
protected override CliExit Execute(PipelineContext pipelineContext)
3432
{
3533
var help = pipelineContext.Pipeline.Help ?? throw new InvalidOperationException("Help cannot be null for this subsystem to work");
36-
var data = help.GetDescription(Symbol);
34+
help.Description.TryGet(Symbol, out var description);
3735

38-
pipelineContext.ConsoleHack.WriteLine(data);
36+
pipelineContext.ConsoleHack.WriteLine(description);
3937
pipelineContext.AlreadyHandled = true;
4038
return CliExit.SuccessfullyHandled(pipelineContext.ParseResult);
4139
}
@@ -68,11 +66,11 @@ protected override CliExit TearDown(CliExit cliExit)
6866
}
6967

7068
internal class StringDirectiveSubsystem(IAnnotationProvider? annotationProvider = null)
71-
: DirectiveSubsystem("other",SubsystemKind.Diagram, annotationProvider)
69+
: DirectiveSubsystem("other", SubsystemKind.Diagram, annotationProvider)
7270
{ }
7371

7472
internal class BooleanDirectiveSubsystem(IAnnotationProvider? annotationProvider = null)
75-
: DirectiveSubsystem("diagram", SubsystemKind.Diagram, annotationProvider)
73+
: DirectiveSubsystem("diagram", SubsystemKind.Diagram, annotationProvider)
7674
{ }
7775

7876
}

src/System.CommandLine.Subsystems/HelpSubsystem.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.CommandLine.Subsystems.Annotations;
@@ -24,14 +24,6 @@ public class HelpSubsystem(IAnnotationProvider? annotationProvider = null)
2424
Arity = ArgumentArity.Zero
2525
};
2626

27-
public void SetDescription(CliSymbol symbol, string description)
28-
=> SetAnnotation(symbol, HelpAnnotations.Description, description);
29-
30-
public string GetDescription(CliSymbol symbol)
31-
=> TryGetAnnotation<string>(symbol, HelpAnnotations.Description, out var value)
32-
? value
33-
: "";
34-
3527
public AnnotationAccessor<string> Description
3628
=> new(this, HelpAnnotations.Description);
3729

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,31 @@ namespace System.CommandLine.Subsystems.Annotations;
88
/// <summary>
99
/// Allows associating an annotation with a <see cref="CliSymbol"/>. The annotation will be stored by the accessor's owner <see cref="CliSubsystem"/>.
1010
/// </summary>
11+
/// <remarks>
12+
/// The annotation will be stored by the accessor's owner <see cref="CliSubsystem"/>.
13+
/// </summary>
14+
/// <typeparam name="TValue">The type of value to be stored</typeparam>
15+
/// <param name="owner">The subsystem that this annotation store data for.</param>
16+
/// <param name="id">The identifier for this annotation, since subsystems may have multiple annotations.</param>
1117
public struct AnnotationAccessor<TValue>(CliSubsystem owner, AnnotationId<TValue> id)
1218
{
1319
/// <summary>
14-
/// The ID of the annotation
20+
/// The identifier for this annotation, since subsystems may have multiple annotations.
21+
/// </summary>
22+
public AnnotationId<TValue> Id { get; } = id;
23+
24+
/// <summary>
25+
/// Store a value for the annotation and symbol
26+
/// </summary>
27+
/// <param name="symbol">The CliSymbol the value is for.</param>
28+
/// <param name="value">The value to store.</param>
29+
public readonly void Set(CliSymbol symbol, TValue value) => owner.SetAnnotation(symbol, Id, value);
30+
31+
/// <summary>
32+
/// Retrieve the value for the annotation and symbol
1533
/// </summary>
16-
public AnnotationId<TValue> Id { get; }
17-
public readonly void Set(CliSymbol symbol, TValue value) => owner.SetAnnotation(symbol, id, value);
18-
public readonly bool TryGet(CliSymbol symbol, [NotNullWhen(true)] out TValue? value) => owner.TryGetAnnotation(symbol, id, out value);
34+
/// <param name="symbol">The CliSymbol the value is for.</param>
35+
/// <param name="value">The value to retrieve.</param>
36+
/// <returns>True if the value was found, false otherwise.</returns>
37+
public readonly bool TryGet(CliSymbol symbol, [NotNullWhen(true)] out TValue? value) => owner.TryGetAnnotation(symbol, Id, out value);
1938
}

0 commit comments

Comments
 (0)