Skip to content

Commit 3e11e14

Browse files
committed
remove Option.Argument setter
1 parent 62e1003 commit 3e11e14

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

src/System.CommandLine.Tests/Utility/ConsoleAssertions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// 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

4-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
5-
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
6-
74
using FluentAssertions.Primitives;
85

96
namespace System.CommandLine.Tests.Utility

src/System.CommandLine/DebugAssert.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.Diagnostics;
5+
6+
namespace System.CommandLine
7+
{
8+
internal static class DebugAssert
9+
{
10+
[Conditional("DEBUG")]
11+
public static void ThrowIf(bool condition, string message)
12+
{
13+
if (condition)
14+
{
15+
Throw(message);
16+
}
17+
}
18+
19+
[Conditional("DEBUG")]
20+
public static void Throw(string message)
21+
{
22+
throw new Exception(message);
23+
}
24+
}
25+
}

src/System.CommandLine/Help/HelpOption.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public HelpOption() : base(new[]
2020
internal override Argument Argument
2121
{
2222
get => Argument.None;
23-
set { }
2423
}
2524

2625
public override bool Equals(object obj)

src/System.CommandLine/Option.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Option(
3030
Type? argumentType = null,
3131
Func<object?>? getDefaultValue = null,
3232
IArgumentArity? arity = null)
33-
: this(aliases, description, GetArgument(argumentType, getDefaultValue, arity))
33+
: this(aliases, description, CreateArgument(argumentType, getDefaultValue, arity))
3434
{ }
3535

3636
internal Option(
@@ -54,13 +54,14 @@ internal Option(
5454
var alias = aliases[i];
5555
AddAlias(alias);
5656
}
57+
5758
if (argument != null)
5859
{
59-
Argument = argument;
60+
AddArgumentInner(argument);
6061
}
6162
}
6263

63-
private static Argument? GetArgument(Type? argumentType, Func<object?>? getDefaultValue, IArgumentArity? arity)
64+
private static Argument? CreateArgument(Type? argumentType, Func<object?>? getDefaultValue, IArgumentArity? arity)
6465
{
6566
if (argumentType is null &&
6667
getDefaultValue is null &&
@@ -87,8 +88,20 @@ getDefaultValue is null &&
8788

8889
internal virtual Argument Argument
8990
{
90-
get => Arguments.FirstOrDefault() ?? Argument.None;
91-
set => AddArgumentInner(value); // FIX: delete?
91+
get
92+
{
93+
switch (Children.Arguments.Count)
94+
{
95+
case 0:
96+
var none = Argument.None;
97+
Children.Add(none);
98+
return none;
99+
100+
default:
101+
DebugAssert.ThrowIf(Children.Arguments.Count > 1, $"Unexpected number of option arguments: {Children.Arguments.Count}");
102+
return Children.Arguments[0];
103+
}
104+
}
92105
}
93106

94107
//TODO: Guard against Argument.None?
@@ -98,8 +111,6 @@ public string ArgumentHelpName
98111
set => Argument.Name = value;
99112
}
100113

101-
private IEnumerable<Argument> Arguments => Children.OfType<Argument>();
102-
103114
internal bool DisallowBinding { get; set; }
104115

105116
public override string Name

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,5 @@ public Option(
5151
string? description = null)
5252
: base(aliases, description, new Argument<T>(getDefaultValue ?? throw new ArgumentNullException(nameof(getDefaultValue))))
5353
{ }
54-
55-
internal override Argument Argument
56-
{
57-
set
58-
{
59-
if (!(value is Argument<T>))
60-
{
61-
62-
throw new ArgumentException($"{nameof(Argument)} must be of type {typeof(Argument<T>)} but was {value?.GetType().ToString() ?? "null"}");
63-
}
64-
65-
base.Argument = value;
66-
}
67-
}
6854
}
6955
}

src/System.CommandLine/TypeExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
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+
44
using System.Runtime.CompilerServices;
5-
using System.Text;
6-
using System.Threading.Tasks;
75

86
namespace System.CommandLine
97
{

0 commit comments

Comments
 (0)