Skip to content

Commit bd69469

Browse files
refactor: Remove Get/Set Description on Helper subsystem
Add Get to AnnotationAccessor
1 parent 39c05a3 commit bd69469

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

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

Lines changed: 3 additions & 5 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,7 +31,7 @@ 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+
string data = help.Description.Get(Symbol);
3735

3836
pipelineContext.ConsoleHack.WriteLine(data);
3937
pipelineContext.AlreadyHandled = true;
@@ -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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ 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-
public struct AnnotationAccessor<TValue>(CliSubsystem owner, AnnotationId<TValue> id)
11+
public struct AnnotationAccessor<TValue>(CliSubsystem owner, AnnotationId<TValue> id, TValue? defaultValue = default)
1212
{
1313
/// <summary>
1414
/// The ID of the annotation
1515
/// </summary>
1616
public AnnotationId<TValue> Id { get; }
1717
public readonly void Set(CliSymbol symbol, TValue value) => owner.SetAnnotation(symbol, id, value);
1818
public readonly bool TryGet(CliSymbol symbol, [NotNullWhen(true)] out TValue? value) => owner.TryGetAnnotation(symbol, id, out value);
19+
public readonly TValue? Get(CliSymbol symbol)
20+
{
21+
if (TryGet(symbol, out var value))
22+
{
23+
return value ?? defaultValue;
24+
}
25+
return defaultValue;
26+
}
1927
}

0 commit comments

Comments
 (0)