Skip to content

Commit 6477666

Browse files
Merge pull request #2471 from KathleenDollard/m-clidatasymbol
Added CliDataSymbol
2 parents 0cc5887 + 726655e commit 6477666

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

src/System.CommandLine/CliArgument.cs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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-
using System.Collections.Generic;
54
using System.CommandLine.Binding;
65
using System.CommandLine.Parsing;
76

@@ -10,16 +9,17 @@ namespace System.CommandLine
109
/// <summary>
1110
/// 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>.
1211
/// </summary>
13-
public abstract class CliArgument : CliSymbol
12+
public abstract class CliArgument : CliValueSymbol
1413
{
1514
private ArgumentArity _arity;
16-
// TODO: custom parser, completion, validators
17-
/*
15+
// TODO: custom parser, completion, validators
16+
/*
1817
private TryConvertArgument? _convertArguments;
1918
private List<Func<CompletionContext, IEnumerable<CompletionItem>>>? _completionSources = null;
2019
private List<Action<ArgumentResult>>? _validators = null;
21-
*/
22-
private protected CliArgument(string name) : base(name, allowWhitespace: true)
20+
*/
21+
private protected CliArgument(string name)
22+
: base(name, allowWhitespace: true)
2323
{
2424
}
2525

@@ -39,24 +39,24 @@ public ArgumentArity Arity
3939
}
4040
set => _arity = value;
4141
}
42-
// TODO: help, completion
43-
/*
42+
// TODO: help, completion
43+
/*
4444
/// <summary>
4545
/// The name used in help output to describe the argument.
4646
/// </summary>
4747
public string? HelpName { get; set; }
48-
*/
48+
*/
4949
internal TryConvertArgument? ConvertArguments => ArgumentConverter.GetConverter(this);
50-
// TODO: custom parsers
51-
/*
50+
// TODO: custom parsers
51+
/*
5252
{
5353
get => _convertArguments ??= ArgumentConverter.GetConverter(this);
5454
set => _convertArguments = value;
5555
}
56-
*/
56+
*/
5757

58-
// TODO: completion;
59-
/*
58+
// TODO: completion;
59+
/*
6060
/// <summary>
6161
/// Gets the list of completion sources for the argument.
6262
/// </summary>
@@ -94,21 +94,16 @@ public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSour
9494
return _completionSources;
9595
}
9696
}
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; }
10297
103-
/* TODO: validators
98+
/* TODO: validators
10499
/// <summary>
105100
/// Provides a list of argument validators. Validators can be used
106101
/// to provide custom errors based on user input.
107102
/// </summary>
108103
public List<Action<ArgumentResult>> Validators => _validators ??= new ();
109104
110105
internal bool HasValidators => (_validators?.Count ?? 0) > 0;
111-
*/
106+
*/
112107
/// <summary>
113108
/// Gets the default value for the argument.
114109
/// </summary>
@@ -124,8 +119,8 @@ public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSour
124119
/// Specifies if a default value is defined for the argument.
125120
/// </summary>
126121
public abstract bool HasDefaultValue { get; }
127-
// TODO: completion
128-
/*
122+
// TODO: completion
123+
/*
129124
/// <inheritdoc />
130125
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
131126
{
@@ -134,10 +129,8 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con
134129
.Distinct()
135130
.OrderBy(c => c.SortText, StringComparer.OrdinalIgnoreCase);
136131
}
137-
*/
132+
*/
138133
/// <inheritdoc />
139134
public override string ToString() => $"{nameof(CliArgument)}: {Name}";
140-
141-
internal bool IsBoolean() => ValueType == typeof(bool) || ValueType == typeof(bool?);
142135
}
143136
}

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 : CliValueSymbol
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.
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 CliValueSymbol : CliSymbol
7+
{
8+
protected CliValueSymbol(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/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="CliValueSymbol.cs" />
3031
<Compile Include="Parsing\CommandValueResult.cs" />
3132
<Compile Include="Parsing\SymbolLookupByName.cs" />
3233
<Compile Include="Parsing\ValueResultOutcome.cs" />

0 commit comments

Comments
 (0)