@@ -38,42 +38,42 @@ public CliArgument(string name) : base(name)
3838
3939 // TODO: custom parsers
4040 /*
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)
5057 {
51- get => _customParser;
52- set
58+ ConvertArguments = (ArgumentResult argumentResult, out object? parsedValue) =>
5359 {
54- _customParser = value;
60+ int errorsBefore = argumentResult.SymbolResultTree.ErrorCount;
61+ var result = value(argumentResult);
5562
56- if (value is not null )
63+ if (errorsBefore == argumentResult.SymbolResultTree.ErrorCount )
5764 {
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;
7467 }
75- }
68+ else
69+ {
70+ parsedValue = default(T)!;
71+ return false;
72+ }
73+ };
7674 }
75+ }
76+ }
7777 */
7878 /// <inheritdoc />
7979 public override Type ValueType => typeof ( T ) ;
@@ -92,84 +92,84 @@ public CliArgument(string name) : base(name)
9292 }
9393 // TODO: completion, validators
9494 /*
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++)
100112 {
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];
108114
109- void UnrecognizedArgumentError(ArgumentResult argumentResult )
115+ if (token.Symbol is null || token.Symbol == this )
110116 {
111- for (var i = 0; i < argumentResult.Tokens.Count; i++ )
117+ if (Array.IndexOf(values, token.Value) < 0 )
112118 {
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));
122120 }
123121 }
124122 }
123+ }
124+ }
125125
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();
134134
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];
138138
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);
142142
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+ }
149147 }
148+ });
149+ }
150150
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();
160160
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);
165165
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+ }
172170 }
171+ });
172+ }
173173 */
174174
175175 [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL3050" , Justification = "https://github.com/dotnet/command-line-api/issues/1638" ) ]
0 commit comments