diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 8b64bbd00e..afdbc3f415 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -8,6 +8,7 @@ using FluentAssertions; using System.Linq; using Xunit; +using System.Net; namespace System.CommandLine.Tests.Binding { @@ -708,7 +709,29 @@ public void Values_can_be_correctly_converted_to_nullable_sbyte_without_the_pars value.Should().Be(123); } - + + [Fact] + public void Values_can_be_correctly_converted_to_ipaddress_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-us"); + + var value = option.Parse("-us 1.2.3.4").GetValueForOption(option); + + value.Should().Be(IPAddress.Parse("1.2.3.4")); + } + +#if NETCOREAPP3_0_OR_GREATER + [Fact] + public void Values_can_be_correctly_converted_to_ipendpoint_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-us"); + + var value = option.Parse("-us 1.2.3.4:56").GetValueForOption(option); + + value.Should().Be(IPEndPoint.Parse("1.2.3.4:56")); + } +#endif + [Fact] public void Values_can_be_correctly_converted_to_byte_without_the_parser_specifying_a_custom_converter() { diff --git a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs index c4e56bad5e..82cb8d90eb 100644 --- a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs +++ b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using System.Net; namespace System.CommandLine.Binding; @@ -154,6 +155,32 @@ internal static partial class ArgumentConverter return false; }, + [typeof(IPAddress)] = (string token, out object? value) => + { + if (IPAddress.TryParse(token, out var ip)) + { + value = ip; + return true; + } + + value = default; + return false; + }, + +#if NETCOREAPP3_0_OR_GREATER + [typeof(IPEndPoint)] = (string token, out object? value) => + { + if (IPEndPoint.TryParse(token, out var ipendpoint)) + { + value = ipendpoint; + return true; + } + + value = default; + return false; + }, +#endif + [typeof(long)] = (string token, out object? value) => { if (long.TryParse(token, out var longValue))