@@ -38,42 +38,42 @@ public CliArgument(string name) : base(name)
38
38
39
39
// TODO: custom parsers
40
40
/*
41
- /// <summary>
42
- /// A custom argument parser.
43
- /// </summary>
44
- /// <remarks>
45
- /// It's invoked when there was parse input provided for given Argument.
46
- /// The same instance can be set as <see cref="DefaultValueFactory"/>, in such case
47
- /// the delegate is also invoked when no input was provided.
48
- /// </remarks>
49
- public Func<ArgumentResult, T?>? CustomParser
41
+ /// <summary>
42
+ /// A custom argument parser.
43
+ /// </summary>
44
+ /// <remarks>
45
+ /// It's invoked when there was parse input provided for given Argument.
46
+ /// The same instance can be set as <see cref="DefaultValueFactory"/>, in such case
47
+ /// the delegate is also invoked when no input was provided.
48
+ /// </remarks>
49
+ public Func<ArgumentResult, T?>? CustomParser
50
+ {
51
+ get => _customParser;
52
+ set
53
+ {
54
+ _customParser = value;
55
+
56
+ if (value is not null)
50
57
{
51
- get => _customParser;
52
- set
58
+ ConvertArguments = (ArgumentResult argumentResult, out object? parsedValue) =>
53
59
{
54
- _customParser = value;
60
+ int errorsBefore = argumentResult.SymbolResultTree.ErrorCount;
61
+ var result = value(argumentResult);
55
62
56
- if (value is not null )
63
+ if (errorsBefore == argumentResult.SymbolResultTree.ErrorCount )
57
64
{
58
- ConvertArguments = (ArgumentResult argumentResult, out object? parsedValue) =>
59
- {
60
- int errorsBefore = argumentResult.SymbolResultTree.ErrorCount;
61
- var result = value(argumentResult);
62
-
63
- if (errorsBefore == argumentResult.SymbolResultTree.ErrorCount)
64
- {
65
- parsedValue = result;
66
- return true;
67
- }
68
- else
69
- {
70
- parsedValue = default(T)!;
71
- return false;
72
- }
73
- };
65
+ parsedValue = result;
66
+ return true;
74
67
}
75
- }
68
+ else
69
+ {
70
+ parsedValue = default(T)!;
71
+ return false;
72
+ }
73
+ };
76
74
}
75
+ }
76
+ }
77
77
*/
78
78
/// <inheritdoc />
79
79
public override Type ValueType => typeof ( T ) ;
@@ -92,84 +92,84 @@ public CliArgument(string name) : base(name)
92
92
}
93
93
// TODO: completion, validators
94
94
/*
95
- /// <summary>
96
- /// Configures the argument to accept only the specified values, and to suggest them as command line completions.
97
- /// </summary>
98
- /// <param name="values">The values that are allowed for the argument.</param>
99
- public void AcceptOnlyFromAmong(params string[] values)
95
+ /// <summary>
96
+ /// Configures the argument to accept only the specified values, and to suggest them as command line completions.
97
+ /// </summary>
98
+ /// <param name="values">The values that are allowed for the argument.</param>
99
+ public void AcceptOnlyFromAmong(params string[] values)
100
+ {
101
+ if (values is not null && values.Length > 0)
102
+ {
103
+ Validators.Clear();
104
+ Validators.Add(UnrecognizedArgumentError);
105
+ CompletionSources.Clear();
106
+ CompletionSources.Add(values);
107
+ }
108
+
109
+ void UnrecognizedArgumentError(ArgumentResult argumentResult)
110
+ {
111
+ for (var i = 0; i < argumentResult.Tokens.Count; i++)
100
112
{
101
- if (values is not null && values.Length > 0)
102
- {
103
- Validators.Clear();
104
- Validators.Add(UnrecognizedArgumentError);
105
- CompletionSources.Clear();
106
- CompletionSources.Add(values);
107
- }
113
+ var token = argumentResult.Tokens[i];
108
114
109
- void UnrecognizedArgumentError(ArgumentResult argumentResult )
115
+ if (token.Symbol is null || token.Symbol == this )
110
116
{
111
- for (var i = 0; i < argumentResult.Tokens.Count; i++ )
117
+ if (Array.IndexOf(values, token.Value) < 0 )
112
118
{
113
- var token = argumentResult.Tokens[i];
114
-
115
- if (token.Symbol is null || token.Symbol == this)
116
- {
117
- if (Array.IndexOf(values, token.Value) < 0)
118
- {
119
- argumentResult.AddError(LocalizationResources.UnrecognizedArgument(token.Value, values));
120
- }
121
- }
119
+ argumentResult.AddError(LocalizationResources.UnrecognizedArgument(token.Value, values));
122
120
}
123
121
}
124
122
}
123
+ }
124
+ }
125
125
126
- /// <summary>
127
- /// Configures the argument to accept only values representing legal file paths.
128
- /// </summary>
129
- public void AcceptLegalFilePathsOnly()
130
- {
131
- Validators.Add(static result =>
132
- {
133
- var invalidPathChars = Path.GetInvalidPathChars();
126
+ /// <summary>
127
+ /// Configures the argument to accept only values representing legal file paths.
128
+ /// </summary>
129
+ public void AcceptLegalFilePathsOnly()
130
+ {
131
+ Validators.Add(static result =>
132
+ {
133
+ var invalidPathChars = Path.GetInvalidPathChars();
134
134
135
- for (var i = 0; i < result.Tokens.Count; i++)
136
- {
137
- var token = result.Tokens[i];
135
+ for (var i = 0; i < result.Tokens.Count; i++)
136
+ {
137
+ var token = result.Tokens[i];
138
138
139
- // File class no longer check invalid character
140
- // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
141
- var invalidCharactersIndex = token.Value.IndexOfAny(invalidPathChars);
139
+ // File class no longer check invalid character
140
+ // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
141
+ var invalidCharactersIndex = token.Value.IndexOfAny(invalidPathChars);
142
142
143
- if (invalidCharactersIndex >= 0)
144
- {
145
- result.AddError(LocalizationResources.InvalidCharactersInPath(token.Value[invalidCharactersIndex]));
146
- }
147
- }
148
- });
143
+ if (invalidCharactersIndex >= 0)
144
+ {
145
+ result.AddError(LocalizationResources.InvalidCharactersInPath(token.Value[invalidCharactersIndex]));
146
+ }
149
147
}
148
+ });
149
+ }
150
150
151
- /// <summary>
152
- /// Configures the argument to accept only values representing legal file names.
153
- /// </summary>
154
- /// <remarks>A parse error will result, for example, if file path separators are found in the parsed value.</remarks>
155
- public void AcceptLegalFileNamesOnly()
156
- {
157
- Validators.Add(static result =>
158
- {
159
- var invalidFileNameChars = Path.GetInvalidFileNameChars();
151
+ /// <summary>
152
+ /// Configures the argument to accept only values representing legal file names.
153
+ /// </summary>
154
+ /// <remarks>A parse error will result, for example, if file path separators are found in the parsed value.</remarks>
155
+ public void AcceptLegalFileNamesOnly()
156
+ {
157
+ Validators.Add(static result =>
158
+ {
159
+ var invalidFileNameChars = Path.GetInvalidFileNameChars();
160
160
161
- for (var i = 0; i < result.Tokens.Count; i++)
162
- {
163
- var token = result.Tokens[i];
164
- var invalidCharactersIndex = token.Value.IndexOfAny(invalidFileNameChars);
161
+ for (var i = 0; i < result.Tokens.Count; i++)
162
+ {
163
+ var token = result.Tokens[i];
164
+ var invalidCharactersIndex = token.Value.IndexOfAny(invalidFileNameChars);
165
165
166
- if (invalidCharactersIndex >= 0)
167
- {
168
- result.AddError(LocalizationResources.InvalidCharactersInFileName(token.Value[invalidCharactersIndex]));
169
- }
170
- }
171
- });
166
+ if (invalidCharactersIndex >= 0)
167
+ {
168
+ result.AddError(LocalizationResources.InvalidCharactersInFileName(token.Value[invalidCharactersIndex]));
169
+ }
172
170
}
171
+ });
172
+ }
173
173
*/
174
174
175
175
[ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL3050" , Justification = "https://github.com/dotnet/command-line-api/issues/1638" ) ]
0 commit comments