Skip to content

Commit fae097a

Browse files
Removed RangeBound, added enum RangeBounds to Range
This removes a layer by directly using ValueSource as it becomes a first class concept
1 parent 5ba9513 commit fae097a

File tree

6 files changed

+44
-44
lines changed

6 files changed

+44
-44
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private CliOption GetOptionWithSimpleRange<T>(T lowerBound, T upperBound)
2222
return option;
2323
}
2424

25-
private CliOption GetOptionWithRangeBounds<T>(RangeBound<T> lowerBound, RangeBound<T> upperBound)
25+
private CliOption GetOptionWithRangeBounds<T>(ValueSource<T> lowerBound, ValueSource<T> upperBound)
2626
where T : IComparable<T>
2727
{
2828
var option = new CliOption<int>("--intOpt");
@@ -107,7 +107,7 @@ public void Int_values_on_upper_range_bound_do_not_report_error()
107107
[Fact]
108108
public void Values_below_calculated_lower_bound_report_error()
109109
{
110-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(() => 1), 50);
110+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => 1), 50);
111111

112112
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 0");
113113

@@ -121,7 +121,7 @@ public void Values_below_calculated_lower_bound_report_error()
121121
[Fact]
122122
public void Values_within_calculated_range_do_not_report_error()
123123
{
124-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(() => 1), RangeBound<int>.Create(() => 50));
124+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => 1), ValueSource<int>.Create(() => 50));
125125

126126
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42");
127127

@@ -132,7 +132,7 @@ public void Values_within_calculated_range_do_not_report_error()
132132
[Fact]
133133
public void Values_above_calculated_upper_bound_report_error()
134134
{
135-
var option = GetOptionWithRangeBounds(0,RangeBound<int>.Create(() => 40));
135+
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(() => 40));
136136

137137
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42");
138138

@@ -146,7 +146,7 @@ public void Values_above_calculated_upper_bound_report_error()
146146
public void Values_below_relative_lower_bound_report_error()
147147
{
148148
var otherOption = new CliOption<int>("-a");
149-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(otherOption, o => (int)o + 1), 50);
149+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (int)o + 1), 50);
150150
var command = new CliCommand("cmd") { option, otherOption };
151151

152152
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 0 -a 0");
@@ -162,7 +162,7 @@ public void Values_below_relative_lower_bound_report_error()
162162
public void Values_within_relative_range_do_not_report_error()
163163
{
164164
var otherOption = new CliOption<int>("-a");
165-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(otherOption, o => (int)o + 1), RangeBound<int>.Create(otherOption, o => (int)o + 10));
165+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (int)o + 1), ValueSource<int>.Create(otherOption, o => (int)o + 10));
166166
var command = new CliCommand("cmd") { option, otherOption };
167167

168168
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 11 -a 3");
@@ -175,7 +175,7 @@ public void Values_within_relative_range_do_not_report_error()
175175
public void Values_above_relative_upper_bound_report_error()
176176
{
177177
var otherOption = new CliOption<int>("-a");
178-
var option = GetOptionWithRangeBounds(0, RangeBound<int>.Create(otherOption, o => (int)o + 10));
178+
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(otherOption, o => (int)o + 10));
179179
var command = new CliCommand("cmd") { option, otherOption };
180180

181181
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 9 -a -2");
@@ -191,7 +191,7 @@ public void Values_below_environment_lower_bound_report_error()
191191
{
192192
var envName = "SYSTEM_COMMANDLINE_LOWERBOUND";
193193
Environment.SetEnvironmentVariable(envName, "2");
194-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(envName, s => int.Parse(s) + 1), 50);
194+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(envName, s => int.Parse(s) + 1), 50);
195195

196196
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 2");
197197
Environment.SetEnvironmentVariable(envName, null);
@@ -208,7 +208,7 @@ public void Values_within_environment_range_do_not_report_error()
208208
{
209209
var envName = "SYSTEM_COMMANDLINE_LOWERBOUND";
210210
Environment.SetEnvironmentVariable(envName, "2");
211-
var option = GetOptionWithRangeBounds(RangeBound<int>.Create(envName, s => int.Parse(s) + 1), 50);
211+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(envName, s => int.Parse(s) + 1), 50);
212212

213213
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 11");
214214
Environment.SetEnvironmentVariable(envName, null);
@@ -222,7 +222,7 @@ public void Values_above_environment_upper_bound_report_error()
222222
{
223223
var envName = "SYSTEM_COMMANDLINE_LOWERBOUND";
224224
Environment.SetEnvironmentVariable(envName, "2");
225-
var option = GetOptionWithRangeBounds(0,RangeBound<int>.Create(envName, s => int.Parse(s) + 1));
225+
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(envName, s => int.Parse(s) + 1));
226226

227227
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 4");
228228
Environment.SetEnvironmentVariable(envName, null);

src/System.CommandLine.Subsystems/ValueConditionAnnotationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ public static void SetRange<T>(this CliValueSymbol symbol, T lowerBound, T upper
1414
symbol.SetValueCondition(range);
1515
}
1616

17-
public static void SetRange<T>(this CliValueSymbol symbol, RangeBound<T> lowerBound, T upperBound)
17+
public static void SetRange<T>(this CliValueSymbol symbol, ValueSource<T> lowerBound, T upperBound)
1818
where T : IComparable<T>
1919
{
2020
var range = new Range<T>(lowerBound, upperBound);
2121

2222
symbol.SetValueCondition(range);
2323
}
2424

25-
public static void SetRange<T>(this CliValueSymbol symbol, T lowerBound, RangeBound<T> upperBound)
25+
public static void SetRange<T>(this CliValueSymbol symbol, T lowerBound, ValueSource<T> upperBound)
2626
where T : IComparable<T>
2727
{
2828
var range = new Range<T>(lowerBound, upperBound);
2929

3030
symbol.SetValueCondition(range);
3131
}
3232

33-
public static void SetRange<T>(this CliValueSymbol symbol, RangeBound<T> lowerBound, RangeBound<T> upperBound)
33+
public static void SetRange<T>(this CliValueSymbol symbol, ValueSource<T> lowerBound, ValueSource<T> upperBound)
3434
where T : IComparable<T>
3535
{
3636
var range = new Range<T>(lowerBound, upperBound);

src/System.CommandLine.Subsystems/ValueConditions/Range.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected Range(Type valueType)
1616
public Type ValueType { get; }
1717
}
1818

19-
public class Range<T>(RangeBound<T>? lowerBound, RangeBound<T>? upperBound)
19+
public class Range<T>(ValueSource<T>? lowerBound, ValueSource<T>? upperBound, RangeBounds rangeBound = 0)
2020
: Range(typeof(T)), IValueValidator
2121
where T : IComparable<T>
2222
{
@@ -34,7 +34,7 @@ public void Validate(object? value,
3434
// TODO: Replace the strings we are comparing with a diagnostic ID when we update ParseError
3535
if (LowerBound is not null)
3636
{
37-
var lowerValue = LowerBound.ValueSource.GetTypedValue(validationContext.PipelineResult);
37+
var lowerValue = LowerBound.GetTypedValue(validationContext.PipelineResult);
3838
if (comparableValue.CompareTo(lowerValue) < 0)
3939
{
4040
validationContext.PipelineResult.AddError(new ParseError($"The value for '{valueSymbol.Name}' is below the lower bound of {LowerBound}"));
@@ -43,14 +43,16 @@ public void Validate(object? value,
4343

4444
if (UpperBound is not null)
4545
{
46-
var upperValue = UpperBound.ValueSource.GetTypedValue(validationContext.PipelineResult);
46+
var upperValue = UpperBound.GetTypedValue(validationContext.PipelineResult);
4747
if (comparableValue.CompareTo(upperValue) > 0)
4848
{
4949
validationContext.PipelineResult.AddError(new ParseError($"The value for '{valueSymbol.Name}' is above the upper bound of {UpperBound}"));
5050
}
5151
}
5252
}
5353

54-
public RangeBound<T>? LowerBound { get; init; } = lowerBound;
55-
public RangeBound<T>? UpperBound { get; init; } = upperBound;
54+
public ValueSource<T>? LowerBound { get; init; } = lowerBound;
55+
public ValueSource<T>? UpperBound { get; init; } = upperBound;
56+
public RangeBounds RangeBound { get; } = rangeBound;
57+
5658
}

src/System.CommandLine.Subsystems/ValueConditions/RangeBound.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
namespace System.CommandLine.ValueConditions;
5+
6+
[Flags]
7+
public enum RangeBounds
8+
{
9+
Inclusive = 0,
10+
ExclusiveLowerBound = 1,
11+
ExclusiveUpperBound = 2,
12+
ExclusiveUpperAndLowerBounds = 3,
13+
}

src/System.CommandLine.Subsystems/ValueConditions/ValueSource.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public abstract class ValueSource<T> : ValueSource
2525
public static implicit operator ValueSource<T>(T value) => new SimpleValueSource<T>(value);
2626
public static implicit operator ValueSource<T>(Func<T> calculated) => new CalculatedValueSource<T>(calculated);
2727

28+
public static ValueSource<T> Create(T value, string? description = null)
29+
=> new SimpleValueSource<T>(value, description);
30+
31+
public static ValueSource<T> Create(Func<T> calculation, string? description = null)
32+
=> new CalculatedValueSource<T>(calculation);
33+
34+
public static ValueSource<T> Create(CliValueSymbol otherSymbol, Func<object, T> calculation, string? description = null)
35+
=> new RelativeToSymbolValueSource<T>(otherSymbol, calculation, description);
36+
37+
public static ValueSource<T> Create(string environmentVariableName, Func<string, T> calculation, string? description = null)
38+
=> new RelativeToEnvironmentVariableValueSource<T>(environmentVariableName, calculation, description);
2839
}
2940

3041
public class SimpleValueSource<T>(T value, string? description = null)

0 commit comments

Comments
 (0)