Skip to content

Commit ce31135

Browse files
committed
remove arity parameter from Option<T> constructor, expose it as a property
1 parent 997ec58 commit ce31135

File tree

8 files changed

+76
-66
lines changed

8 files changed

+76
-66
lines changed

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
505505
.BeFalse();
506506
}
507507

508+
[Fact]
509+
public void Arity_of_non_generic_option_defaults_to_zero()
510+
{
511+
var option = new Option("-x");
512+
513+
option.Arity.Should().BeEquivalentTo(ArgumentArity.Zero);
514+
}
515+
508516
protected override Symbol CreateSymbol(string name) => new Option(name);
509517
}
510518
}

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

src/System.CommandLine/Argument.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public Argument()
3232
/// Initializes a new instance of the Argument class.
3333
/// </summary>
3434
/// <param name="name">The name of the argument.</param>
35-
public Argument(string name)
35+
public Argument(string name)
3636
{
37-
if (!string.IsNullOrWhiteSpace(name))
37+
if (string.IsNullOrWhiteSpace(name))
3838
{
39-
Name = name!;
39+
throw new ArgumentException("Value cannot be null or whitespace.", nameof(name));
4040
}
41+
42+
Name = name!;
4143
}
4244

4345
internal HashSet<string>? AllowedValues { get; private set; }
@@ -194,7 +196,7 @@ public void SetDefaultValueFactory(Func<ArgumentResult, object?> getDefaultValue
194196
/// </summary>
195197
public bool HasDefaultValue => _defaultValueFactory != null;
196198

197-
internal static Argument None => new Argument { Arity = ArgumentArity.Zero };
199+
internal static Argument None() => new Argument { Arity = ArgumentArity.Zero };
198200

199201
internal void AddAllowedValues(IEnumerable<string> values)
200202
{

src/System.CommandLine/Command.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ private protected override void AddSymbol(Symbol symbol)
135135
_globalOptions.ThrowIfAnyAliasIsInUse(option);
136136
}
137137

138-
symbol.AddParent(this);
139-
140138
base.AddSymbol(symbol);
141139
}
142140

src/System.CommandLine/Help/HelpOption.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public HelpOption() : base(new[]
1717
DisallowBinding = true;
1818
}
1919

20-
internal override Argument Argument
21-
{
22-
get => Argument.None;
23-
}
20+
internal override Argument Argument => Argument.None();
2421

2522
public override bool Equals(object obj)
2623
{

src/System.CommandLine/Option.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ internal virtual Argument Argument
9393
switch (Children.Arguments.Count)
9494
{
9595
case 0:
96-
var none = Argument.None;
97-
Children.Add(none);
96+
var none = Argument.None();
97+
AddSymbol(none);
9898
return none;
9999

100100
default:
@@ -111,7 +111,13 @@ public string ArgumentHelpName
111111
set => Argument.Name = value;
112112
}
113113

114-
internal bool DisallowBinding { get; set; }
114+
public IArgumentArity Arity
115+
{
116+
get => Argument.Arity;
117+
set => Argument.Arity = value;
118+
}
119+
120+
internal bool DisallowBinding { get; set; }
115121

116122
public override string Name
117123
{

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ public class Option<T> : Option
99
{
1010
public Option(
1111
string alias,
12-
string? description = null,
13-
IArgumentArity? arity = null)
14-
: base(new[] { alias }, description, new Argument<T> { Arity = arity })
12+
string? description = null)
13+
: base(new[] { alias }, description, new Argument<T>())
1514
{ }
1615

1716
public Option(

src/System.CommandLine/Symbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ internal void AddParent(Symbol symbol)
4848
private protected virtual void AddSymbol(Symbol symbol)
4949
{
5050
Children.Add(symbol);
51+
symbol.AddParent(this);
5152
}
5253

5354
private protected void AddArgumentInner(Argument argument)
5455
{
5556
argument.AddParent(this);
56-
5757
Children.Add(argument);
5858
}
5959

0 commit comments

Comments
 (0)