Skip to content

Commit a4531f1

Browse files
committed
Strongly type SetRange, add GetRange
1 parent a9e6daa commit a4531f1

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

src/System.CommandLine.Subsystems/ValueConditionAnnotationExtensions.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,52 @@ namespace System.CommandLine;
1313
public static class ValueConditionAnnotationExtensions
1414
{
1515
/// <summary>
16-
/// Set the upper and/or lower bound values of the range.
16+
/// Set upper and/or lower bounds on the range of values that the symbol value may have.
1717
/// </summary>
18-
/// <typeparam name="T">The type of the bounds.</typeparam>
18+
/// <typeparam name="TValueSymbol">The type of the symbol whose value is bounded by the range.</typeparam>
19+
/// <typeparam name="TValue">The type of the value that is bounded by the range.</typeparam>
1920
/// <param name="symbol">The option or argument the range applies to.</param>
2021
/// <param name="lowerBound">The lower bound of the range.</param>
2122
/// <param name="upperBound">The upper bound of the range.</param>
2223
// TODO: Add RangeBounds
2324
// TODO: You should not have to set both...why not nullable?
24-
public static void SetRange<T>(this CliValueSymbol symbol, T lowerBound, T upperBound)
25-
where T : IComparable<T>
25+
public static void SetRange<TValueSymbol, TValue>(this TValueSymbol symbol, TValue lowerBound, TValue upperBound)
26+
where TValueSymbol : CliValueSymbol, ICliValueSymbol<TValue>
27+
where TValue : IComparable<TValue>
2628
{
27-
var range = new Range<T>(lowerBound, upperBound);
29+
var range = new Range<TValue>(lowerBound, upperBound);
2830

2931
symbol.SetValueCondition(range);
3032
}
3133

3234
/// <summary>
33-
/// Set the upper and/or lower bound via ValueSource. Implicit conversions means this
34-
/// generally just works with any <see cref="ValueSource">.
35+
/// Set upper and/or lower <see cref="ValueSource"> bounds on the range of values that the symbol value may have.
36+
/// Implicit conversions means this generally just works with any <see cref="ValueSource">.
3537
/// </summary>
36-
/// <typeparam name="T">The type of the bounds.</typeparam>
38+
/// <typeparam name="TValueSymbol">The type of the symbol whose value is bounded by the range.</typeparam>
39+
/// <typeparam name="TValue">The type of the value that is bounded by the range.</typeparam>
3740
/// <param name="symbol">The option or argument the range applies to.</param>
3841
/// <param name="lowerBound">The <see cref="ValueSource"> that is the lower bound of the range.</param>
3942
/// <param name="upperBound">The <see cref="ValueSource"> that is the upper bound of the range.</param>
4043
// TODO: Add RangeBounds
4144
// TODO: You should not have to set both...why not nullable?
42-
public static void SetRange<T>(this CliValueSymbol symbol, ValueSource<T> lowerBound, ValueSource<T> upperBound)
43-
where T : IComparable<T>
45+
public static void SetRange<TValueSymbol, TValue>(this TValueSymbol symbol, ValueSource<TValue> lowerBound, ValueSource<TValue> upperBound)
46+
where TValueSymbol : CliValueSymbol, ICliValueSymbol<TValue>
47+
where TValue : IComparable<TValue>
4448
// TODO: You should not have to set both...why not nullable?
4549
{
46-
var range = new Range<T>(lowerBound, upperBound);
50+
var range = new Range<TValue>(lowerBound, upperBound);
4751

4852
symbol.SetValueCondition(range);
4953
}
5054

55+
/// <summary>
56+
/// Get the upper and/or lower bound of the symbol's value.
57+
/// </summary>
58+
/// <param name="symbol">The option or argument the range applies to.</param>
59+
public static ValueConditions.Range? GetRange(this CliValueSymbol symbol)
60+
=> symbol.GetValueCondition<ValueConditions.Range>();
61+
5162
/// <summary>
5263
/// Indicates that there is an inclusive group of options and arguments for the command. All
5364
/// members of an inclusive must be present, or none can be present.

src/System.CommandLine/CliArgument{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Collections.Generic;
55
using System.CommandLine.Parsing;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.IO;
87

98
namespace System.CommandLine
109
{
10+
1111
/// <inheritdoc cref="CliArgument" />
12-
public class CliArgument<T> : CliArgument
12+
public class CliArgument<T> : CliArgument, ICliValueSymbol<T>
1313
{
1414
// TODO: custom parser
1515
/*

src/System.CommandLine/CliOption{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace System.CommandLine
77
{
88
/// <inheritdoc cref="CliOption" />
99
/// <typeparam name="T">The <see cref="System.Type"/> that the option's arguments are expected to be parsed as.</typeparam>
10-
public class CliOption<T> : CliOption
10+
public class CliOption<T> : CliOption, ICliValueSymbol<T>
1111
{
1212
// TODO: do not expose private fields
1313
internal readonly CliArgument<T> _argument;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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;
5+
6+
/// <summary>
7+
/// This is applied to <see cref="CliArgument{T}"/> and <see cref="CliArgument{T}"/>, and allows
8+
/// allows methods with a <see cref="CliValueSymbol"/> argument to apply constraints based on the
9+
/// value type.
10+
/// </summary>
11+
/// <typeparam name="TValue">The value type</typeparam>
12+
public interface ICliValueSymbol<TValue>
13+
{
14+
}

src/System.CommandLine/System.CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Compile Include="ArgumentArity.cs" />
2828
<Compile Include="Binding\ArgumentConversionResult.cs" />
2929
<Compile Include="CliValueSymbol.cs" />
30+
<Compile Include="ICliValueSymbol.cs" />
3031
<Compile Include="Parsing\CliCommandResult.cs" />
3132
<Compile Include="Parsing\CliSymbolResult.cs" />
3233
<Compile Include="Parsing\SymbolLookupByName.cs" />

0 commit comments

Comments
 (0)