Skip to content

Commit acc2446

Browse files
Added CliDataSymbol to simplify code
This will remove a ton of places where Option and Argument had to be separately handled
1 parent 5166501 commit acc2446

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

src/System.CommandLine/CliArgument.cs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ namespace System.CommandLine
1010
/// <summary>
1111
/// A symbol defining a value that can be passed on the command line to a <see cref="CliCommand">command</see> or <see cref="CliOption">option</see>.
1212
/// </summary>
13-
public abstract class CliArgument : CliSymbol
13+
public abstract class CliArgument : CliDataSymbol
1414
{
1515
private ArgumentArity _arity;
16-
// TODO: custom parser, completion, validators
17-
/*
16+
// TODO: custom parser, completion, validators
17+
/*
1818
private TryConvertArgument? _convertArguments;
1919
private List<Func<CompletionContext, IEnumerable<CompletionItem>>>? _completionSources = null;
2020
private List<Action<ArgumentResult>>? _validators = null;
21-
*/
22-
private protected CliArgument(string name) : base(name, allowWhitespace: true)
21+
*/
22+
private protected CliArgument(string name)
23+
: base(name, allowWhitespace: true)
2324
{
2425
}
2526

@@ -39,24 +40,24 @@ public ArgumentArity Arity
3940
}
4041
set => _arity = value;
4142
}
42-
// TODO: help, completion
43-
/*
43+
// TODO: help, completion
44+
/*
4445
/// <summary>
4546
/// The name used in help output to describe the argument.
4647
/// </summary>
4748
public string? HelpName { get; set; }
48-
*/
49+
*/
4950
internal TryConvertArgument? ConvertArguments => ArgumentConverter.GetConverter(this);
50-
// TODO: custom parsers
51-
/*
51+
// TODO: custom parsers
52+
/*
5253
{
5354
get => _convertArguments ??= ArgumentConverter.GetConverter(this);
5455
set => _convertArguments = value;
5556
}
56-
*/
57+
*/
5758

58-
// TODO: completion;
59-
/*
59+
// TODO: completion;
60+
/*
6061
/// <summary>
6162
/// Gets the list of completion sources for the argument.
6263
/// </summary>
@@ -94,21 +95,16 @@ public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSour
9495
return _completionSources;
9596
}
9697
}
97-
*/
98-
/// <summary>
99-
/// Gets or sets the <see cref="Type" /> that the argument's parsed tokens will be converted to.
100-
/// </summary>
101-
public abstract Type ValueType { get; }
10298
103-
/* TODO: validators
99+
/* TODO: validators
104100
/// <summary>
105101
/// Provides a list of argument validators. Validators can be used
106102
/// to provide custom errors based on user input.
107103
/// </summary>
108104
public List<Action<ArgumentResult>> Validators => _validators ??= new ();
109105
110106
internal bool HasValidators => (_validators?.Count ?? 0) > 0;
111-
*/
107+
*/
112108
/// <summary>
113109
/// Gets the default value for the argument.
114110
/// </summary>
@@ -124,8 +120,8 @@ public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSour
124120
/// Specifies if a default value is defined for the argument.
125121
/// </summary>
126122
public abstract bool HasDefaultValue { get; }
127-
// TODO: completion
128-
/*
123+
// TODO: completion
124+
/*
129125
/// <inheritdoc />
130126
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
131127
{
@@ -134,10 +130,8 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con
134130
.Distinct()
135131
.OrderBy(c => c.SortText, StringComparer.OrdinalIgnoreCase);
136132
}
137-
*/
133+
*/
138134
/// <inheritdoc />
139135
public override string ToString() => $"{nameof(CliArgument)}: {Name}";
140-
141-
internal bool IsBoolean() => ValueType == typeof(bool) || ValueType == typeof(bool?);
142136
}
143137
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
public abstract class CliDataSymbol : CliSymbol
7+
{
8+
protected CliDataSymbol(string name, bool allowWhitespace = false)
9+
: base(name, allowWhitespace)
10+
{ }
11+
12+
/// <summary>
13+
/// Gets or sets the <see cref="Type" /> that the argument's parsed tokens will be converted to.
14+
/// </summary>
15+
public abstract Type ValueType { get; }
16+
17+
internal bool IsBoolean() => ValueType == typeof(bool) || ValueType == typeof(bool?);
18+
19+
}

src/System.CommandLine/CliOption.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ namespace System.CommandLine
1010
/// <summary>
1111
/// A symbol defining a named parameter and a value for that parameter.
1212
/// </summary>
13-
public abstract class CliOption : CliSymbol
13+
public abstract class CliOption : CliDataSymbol
1414
{
15-
// TODO: don't expose field
1615
internal AliasSet? _aliases;
17-
/*
16+
/*
1817
private List<Action<OptionResult>>? _validators;
1918
2019
*/
2120

22-
private protected CliOption(string name, string[] aliases) : base(name)
21+
private protected CliOption(string name, string[] aliases)
22+
: base(name)
2323
{
24-
if (aliases is { Length: > 0 })
24+
if (aliases is { Length: > 0 })
2525
{
2626
_aliases = new(aliases);
2727
}
2828
}
2929

30+
public override Type ValueType => Argument.ValueType;
31+
3032
/// <summary>
3133
/// Gets the <see cref="Argument">argument</see> for the option.
3234
/// </summary>
@@ -37,8 +39,8 @@ private protected CliOption(string name, string[] aliases) : base(name)
3739
/// </summary>
3840
public bool HasDefaultValue => Argument.HasDefaultValue;
3941

40-
// TODO: help
41-
/*
42+
// TODO: help
43+
/*
4244
/// <summary>
4345
/// Gets or sets the name of the Option when displayed in help.
4446
/// </summary>
@@ -51,7 +53,7 @@ public string? HelpName
5153
get => Argument.HelpName;
5254
set => Argument.HelpName = value;
5355
}
54-
*/
56+
*/
5557

5658
/// <summary>
5759
/// Gets or sets the arity of the option.
@@ -62,8 +64,8 @@ public ArgumentArity Arity
6264
set => Argument.Arity = value;
6365
}
6466

65-
// TODO: recursive options, validators, completion
66-
/*
67+
// TODO: recursive options, validators, completion
68+
/*
6769
/// <summary>
6870
/// When set to true, this option will be applied to its immediate parent command or commands and recursively to their subcommands.
6971
/// </summary>
@@ -80,9 +82,9 @@ public ArgumentArity Arity
8082
/// Gets the list of completion sources for the option.
8183
/// </summary>
8284
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSources => Argument.CompletionSources;
83-
*/
85+
*/
8486

85-
// TODO: what does this even mean?
87+
// TODO: what does this even mean?
8688
/// <summary>
8789
/// Gets a value that indicates whether multiple argument tokens are allowed for each option identifier token.
8890
/// </summary>
@@ -98,10 +100,10 @@ public ArgumentArity Arity
98100
/// </example>
99101
public bool AllowMultipleArgumentsPerToken { get; set; }
100102

101-
// TODO: rename to IsGreedy
103+
// TODO: rename to IsGreedy
102104
internal virtual bool Greedy => Argument.Arity.MinimumNumberOfValues > 0 && Argument.ValueType != typeof(bool);
103-
104-
// TODO: rename to IsRequired
105+
106+
// TODO: rename to IsRequired and move to Validation
105107
/// <summary>
106108
/// Indicates whether the option is required when its parent command is invoked.
107109
/// </summary>
@@ -114,8 +116,8 @@ public ArgumentArity Arity
114116
/// <remarks>The collection does not contain the <see cref="CliSymbol.Name"/> of the Option.</remarks>
115117
public ICollection<string> Aliases => _aliases ??= new();
116118

117-
// TODO: invocation, completion
118-
/*
119+
// TODO: invocation, completion
120+
/*
119121
/// <summary>
120122
/// Gets or sets the <see cref="CliAction"/> for the Option. The handler represents the action
121123
/// that will be performed when the Option is invoked.

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="AliasSet.cs" />
2828
<Compile Include="ArgumentArity.cs" />
2929
<Compile Include="Binding\ArgumentConversionResult.cs" />
30+
<Compile Include="CliDataSymbol.cs" />
3031
<Compile Include="Parsing\CommandValueResult.cs" />
3132
<Compile Include="Parsing\SymbolLookupByName.cs" />
3233
<Compile Include="Parsing\ValueResultOutcome.cs" />

0 commit comments

Comments
 (0)