File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed
System.CommandLine/Parsing Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change 3
3
4
4
using System . CommandLine . Invocation ;
5
5
using System . CommandLine . Parsing ;
6
+ using System . Linq ;
6
7
using FluentAssertions ;
7
8
using Xunit ;
8
9
using Xunit . Abstractions ;
@@ -311,6 +312,38 @@ public void Option_T_default_value_can_be_set()
311
312
. Be ( 123 ) ;
312
313
}
313
314
315
+ [ Fact ]
316
+ public void Option_T_default_value_is_validated ( )
317
+ {
318
+ var arg = new Argument < int > ( ( ) => 123 ) ;
319
+ arg . AddValidator ( symbol =>
320
+ symbol . Tokens
321
+ . Select ( t => t . Value )
322
+ . Where ( v => v == "123" )
323
+ . Select ( x => "ERR" )
324
+ . FirstOrDefault ( ) ) ;
325
+
326
+ var option = new Option ( "-x" )
327
+ {
328
+ Argument = arg
329
+ } ;
330
+
331
+ option
332
+ . Parse ( "-x 123" )
333
+ . Errors
334
+ . Select ( e => e . Message )
335
+ . Should ( )
336
+ . BeEquivalentTo ( new [ ] { "ERR" } ) ;
337
+
338
+ option
339
+ . Parse ( "" )
340
+ . Errors
341
+ . Select ( e => e . Message )
342
+ . Should ( )
343
+ . BeEquivalentTo ( new [ ] { "ERR" } ) ;
344
+
345
+ }
346
+
314
347
protected override Symbol CreateSymbol ( string name ) => new Option ( name ) ;
315
348
}
316
349
}
Original file line number Diff line number Diff line change @@ -327,13 +327,14 @@ private void PopulateDefaultValues()
327
327
optionResult . GetDefaultValueFor ( option . Argument ) ,
328
328
TokenType . Argument ) ;
329
329
330
- optionResult . Children . Add (
331
- new ArgumentResult (
330
+ var childArgumentResult = new ArgumentResult (
332
331
option . Argument ,
333
- optionResult ) ) ;
332
+ optionResult ) ;
334
333
334
+ optionResult . Children . Add ( childArgumentResult ) ;
335
335
commandResult . Children . Add ( optionResult ) ;
336
336
optionResult . AddToken ( token ) ;
337
+ childArgumentResult . AddToken ( token ) ;
337
338
_rootCommandResult . AddToSymbolMap ( optionResult ) ;
338
339
339
340
break ;
You can’t perform that action at this time.
0 commit comments