Skip to content

Commit 1093825

Browse files
Added GetValue to PipelineResults and added tests
Not sure where along the way GetValue got lost :(
1 parent ce616d7 commit 1093825

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,61 @@
55
using System.CommandLine.Directives;
66
using System.CommandLine.Parsing;
77
using Xunit;
8+
using static System.CommandLine.Subsystems.Tests.TestData;
89

910
namespace System.CommandLine.Subsystems.Tests;
1011

1112
public class ValueSubsystemTests
1213
{
14+
[Fact]
15+
public void Values_that_are_entered_are_retrieved()
16+
{
17+
var option = new CliOption<int>("--intOpt");
18+
var rootCommand = new CliRootCommand { option };
19+
var configuration = new CliConfiguration(rootCommand);
20+
var pipeline = Pipeline.Create();
21+
var consoleHack = new ConsoleHack().RedirectToBuffer(true);
22+
var input = "--intOpt 42";
23+
24+
var parseResult = CliParser.Parse(rootCommand, input, configuration);
25+
var pipelineResult = new PipelineResult(parseResult, input, pipeline);
26+
27+
pipelineResult.Should().NotBeNull();
28+
var optionValueResult = pipelineResult.GetValueResult(option);
29+
var optionValue = pipelineResult.GetValue<int>(option);
30+
optionValueResult.Should().NotBeNull();
31+
optionValue.Should().Be(42);
32+
}
33+
34+
[Fact]
35+
public void Values_that_are_not_entered_are_type_default_with_no_default_values()
36+
{
37+
var stringOption = new CliOption<string>("--stringOption");
38+
var intOption = new CliOption<int>("--intOption");
39+
var dateOption = new CliOption<DateTime>("--dateOption");
40+
var nullableIntOption = new CliOption<int?>("--nullableIntOption");
41+
var guidOption = new CliOption<Guid>("--guidOption");
42+
var rootCommand = new CliRootCommand { stringOption, intOption, dateOption, nullableIntOption, guidOption };
43+
var configuration = new CliConfiguration(rootCommand);
44+
var pipeline = Pipeline.Create();
45+
var input = "";
46+
47+
var parseResult = CliParser.Parse(rootCommand, input, configuration);
48+
var pipelineResult = new PipelineResult(parseResult, input, pipeline);
49+
50+
pipelineResult.Should().NotBeNull();
51+
var stringOptionValue = pipelineResult.GetValue<string>(stringOption);
52+
var intOptionValue = pipelineResult.GetValue<int>(intOption);
53+
var dateOptionValue = pipelineResult.GetValue<DateTime>(dateOption);
54+
var nullableIntOptionValue = pipelineResult.GetValue<int?>(nullableIntOption);
55+
var guidOptionValue = pipelineResult.GetValue<Guid>(guidOption);
56+
stringOptionValue.Should().BeNull();
57+
intOptionValue.Should().Be(0);
58+
dateOptionValue.Should().Be(DateTime.MinValue);
59+
nullableIntOptionValue.Should().BeNull();
60+
guidOptionValue.Should().Be(Guid.Empty);
61+
}
62+
1363
// TODO: Add various default value tests
1464

1565
/* Hold these tests until we determine if ValueSubsystem is replaceable

src/System.CommandLine.Subsystems/PipelineResult.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace System.CommandLine;
77

88
public class PipelineResult(ParseResult? parseResult, string rawInput, Pipeline? pipeline, ConsoleHack? consoleHack = null)
99
{
10+
// TODO: Try to build workflow so it is illegal to create this without a ParseResult
1011
private readonly List<ParseError> errors = [];
1112
public ParseResult? ParseResult { get; } = parseResult;
1213
private ValueProvider valueProvider { get; } = new ValueProvider(parseResult);
@@ -19,6 +20,16 @@ public class PipelineResult(ParseResult? parseResult, string rawInput, Pipeline?
1920
public bool AlreadyHandled { get; set; }
2021
public int ExitCode { get; set; }
2122

23+
public T? GetValue<T>(CliValueSymbol dataSymbol)
24+
=> valueProvider.GetValue<T>(dataSymbol);
25+
26+
public object? GetValue(CliValueSymbol option)
27+
=> valueProvider.GetValue<object?>(option);
28+
29+
public CliValueResult GetValueResult(CliValueSymbol dataSymbol)
30+
=> parseResult.GetValueResult(dataSymbol);
31+
32+
2233
public void AddErrors(IEnumerable<ParseError> errors)
2334
{
2435
if (errors is not null)

src/System.CommandLine/ParseResult.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,12 @@ CommandLineText is null
203203
*/
204204

205205
/// <summary>
206-
/// Gets the ValueResult, if any, for the specified option.
206+
/// Gets the ValueResult, if any, for the specified option or argument
207207
/// </summary>
208208
/// <param name="option">The option for which to find a result.</param>
209209
/// <returns>A result for the specified option, or <see langword="null"/> if it was not entered by the user.</returns>
210-
public CliValueResult? GetValueResult(CliOption option)
211-
=> GetValueResultInternal(option);
212-
213-
/// <summary>
214-
/// Gets the result, if any, for the specified argument.
215-
/// </summary>
216-
/// <param name="argument">The argument for which to find a result.</param>
217-
/// <returns>A result for the specified argument, or <see langword="null"/> if it was not entered by the user.</returns>
218-
public CliValueResult? GetValueResult(CliArgument argument)
219-
=> GetValueResultInternal(argument);
210+
public CliValueResult? GetValueResult(CliValueSymbol valueSymbol)
211+
=> GetValueResultInternal(valueSymbol);
220212

221213
private CliValueResult? GetValueResultInternal(CliSymbol symbol)
222214
=> valueResultDictionary.TryGetValue(symbol, out var result)

0 commit comments

Comments
 (0)