Skip to content

Commit 4457da4

Browse files
Merge pull request #2372 from KathleenDollard/powderhouse-valueresult
Support for ValueResult as public type to carry value and location
2 parents 84d40f3 + 9ae597c commit 4457da4

22 files changed

+523
-43
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ protected override CliExit TearDown(CliExit cliExit)
6868
}
6969

7070
internal class StringDirectiveSubsystem(IAnnotationProvider? annotationProvider = null)
71-
: DirectiveSubsystem("other",SubsystemKind.Other, annotationProvider)
71+
: DirectiveSubsystem("other",SubsystemKind.Diagram, annotationProvider)
7272
{ }
7373

7474
internal class BooleanDirectiveSubsystem(IAnnotationProvider? annotationProvider = null)
75-
: DirectiveSubsystem("diagram", SubsystemKind.Other, annotationProvider)
75+
: DirectiveSubsystem("diagram", SubsystemKind.Diagram, annotationProvider)
7676
{ }
7777

7878
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ public void Normal_pipeline_contains_no_subsystems()
173173
pipeline.Completion.Should().BeNull();
174174
}
175175

176-
177176
[Fact]
178177
public void Subsystems_can_access_each_others_data()
179178
{

src/System.CommandLine.Subsystems/Directives/DiagramSubsystem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ private static void Diagram(
6666
builder.Append('!');
6767
}
6868

69+
// TODO: Directives
6970
/*
7071
switch (symbolResult)
7172
{
72-
// TODO: Directives
7373
case DirectiveResult { Directive: not DiagramDirective }:
7474
break;
75+
*/
7576

7677
// TODO: This logic is deeply tied to internal types/properties. These aren't things we probably want to expose like SymbolNode. See #2349 for alternatives
78+
/*
7779
case ArgumentResult argumentResult:
7880
{
7981
var includeArgumentName =
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.CommandLine.Parsing;
5+
6+
namespace System.CommandLine.Subsystems.Annotations;
7+
8+
/// <summary>
9+
/// IDs for well-known Version annotations.
10+
/// </summary>
11+
public static class ValueAnnotations
12+
{
13+
public static string Prefix { get; } = nameof(SubsystemKind.Value);
14+
15+
public static AnnotationId<object> Explicit { get; } = new(Prefix, nameof(Explicit));
16+
public static AnnotationId<Func<ValueResult, object?>> Calculated { get; } = new(Prefix, nameof(Calculated));
17+
}

src/System.CommandLine.Subsystems/Subsystems/CliSubsystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected internal bool TryGetAnnotation<TValue>(CliSymbol symbol, AnnotationId<
6262
/// <param name="value">An out parameter to contain the result</param>
6363
/// <remarks>
6464
/// This value is protected because these values are always retrieved from derived classes that offer
65-
/// strongly typed explicit methods, such as help having `GAetDescription(Symbol symbol, "My help descrption")` method.
65+
/// strongly typed explicit methods, such as help having `GetDescription(Symbol symbol, "My help description")` method.
6666
/// </remarks>
6767
protected internal void SetAnnotation<TValue>(CliSymbol symbol, AnnotationId<TValue> id, TValue value)
6868
{

src/System.CommandLine.Subsystems/Subsystems/SubsystemKind.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ public enum SubsystemKind
88
Other = 0,
99
Help,
1010
Version,
11+
Value,
1112
ErrorReporting,
1213
Completion,
1314
Diagram,
14-
Response,
15+
Response
1516
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.CommandLine.Parsing;
5+
using System.CommandLine.Subsystems;
6+
using System.CommandLine.Subsystems.Annotations;
7+
8+
namespace System.CommandLine;
9+
10+
public class ValueSubsystem : CliSubsystem
11+
{
12+
private ParseResult? parseResult = null;
13+
14+
public ValueSubsystem(IAnnotationProvider? annotationProvider = null)
15+
: base(ValueAnnotations.Prefix, SubsystemKind.Version, annotationProvider)
16+
{ }
17+
18+
void SetExplicit(CliSymbol symbol, object value)
19+
=> SetAnnotation(symbol, ValueAnnotations.Explicit, value);
20+
object GetExplicit(CliSymbol symbol)
21+
=> TryGetAnnotation(symbol, ValueAnnotations.Explicit, out var value)
22+
? value
23+
: "";
24+
AnnotationAccessor<object> Explicit
25+
=> new(this, ValueAnnotations.Explicit);
26+
27+
void SetCalculated(CliSymbol symbol, Func<ValueResult, object?> factory)
28+
=> SetAnnotation(symbol, ValueAnnotations.Calculated, factory);
29+
Func<ValueResult, object?>? GetCalculatedValue(CliSymbol symbol)
30+
=> TryGetAnnotation<Func<ValueResult, object?>>(symbol, ValueAnnotations.Calculated, out var value)
31+
? value
32+
: null;
33+
34+
AnnotationAccessor<Func<ValueResult, object?>> Calculated
35+
=> new(this, ValueAnnotations.Calculated);
36+
37+
protected internal override bool GetIsActivated(ParseResult? parseResult)
38+
{
39+
this.parseResult = parseResult;
40+
return true;
41+
}
42+
}
43+

0 commit comments

Comments
 (0)