File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 9
9
using System . Linq ;
10
10
using System . Threading . Tasks ;
11
11
using Xunit ;
12
+ using System . Net ;
12
13
13
14
namespace System . CommandLine . Tests
14
15
{
@@ -620,6 +621,29 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
620
621
options => options . WithStrictOrdering ( ) ) ;
621
622
}
622
623
624
+ [ Fact ]
625
+ public void Custom_parser_can_return_null ( )
626
+ {
627
+ CliOption < IPAddress > option = new ( "-ip" )
628
+ {
629
+ CustomParser = ( argumentResult ) =>
630
+ {
631
+ string value = argumentResult . Tokens . Last ( ) . Value ;
632
+ if ( IPAddress . TryParse ( value , out var address ) )
633
+ {
634
+ return address ;
635
+ }
636
+
637
+ argumentResult . AddError ( $ "'{ value } ' is not a valid value") ;
638
+ return null ;
639
+ }
640
+ } ;
641
+
642
+ ParseResult parseResult = new CliRootCommand ( ) { option } . Parse ( "-ip a.b.c.d" ) ;
643
+
644
+ parseResult . Errors . Should ( ) . Contain ( error => error . Message == "'a.b.c.d' is not a valid value" ) ;
645
+ }
646
+
623
647
[ Fact ]
624
648
public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_they_become_unmatched_tokens ( )
625
649
{
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ namespace System.CommandLine
11
11
/// <inheritdoc cref="CliArgument" />
12
12
public class CliArgument < T > : CliArgument
13
13
{
14
- private Func < ArgumentResult , T > ? _customParser ;
14
+ private Func < ArgumentResult , T ? > ? _customParser ;
15
15
16
16
/// <summary>
17
17
/// Initializes a new instance of the Argument class.
@@ -39,7 +39,7 @@ public CliArgument(string name) : base(name)
39
39
/// The same instance can be set as <see cref="DefaultValueFactory"/>, in such case
40
40
/// the delegate is also invoked when no input was provided.
41
41
/// </remarks>
42
- public Func < ArgumentResult , T > ? CustomParser
42
+ public Func < ArgumentResult , T ? > ? CustomParser
43
43
{
44
44
get => _customParser ;
45
45
set
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ public Func<ArgumentResult, T>? DefaultValueFactory
42
42
}
43
43
44
44
/// <inheritdoc cref="CliArgument{T}.CustomParser" />
45
- public Func < ArgumentResult , T > ? CustomParser
45
+ public Func < ArgumentResult , T ? > ? CustomParser
46
46
{
47
47
get => _argument . CustomParser ;
48
48
set => _argument . CustomParser = value ;
You can’t perform that action at this time.
0 commit comments