From 2af370ef21d25d57b68bf8ebf826f5dd1cdeabee Mon Sep 17 00:00:00 2001 From: Christian Jones Date: Sat, 9 Apr 2022 17:57:04 -0400 Subject: [PATCH 1/2] Error instead of exception for FileSystemInfo null paths --- .../Binding/TypeConversionTests.cs | 17 ++++++++++++++++- .../ArgumentConverter.StringConverters.cs | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 93edfe7959..85dfdf50be 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -5,8 +5,8 @@ using System.Collections.Generic; using System.CommandLine.Utility; using System.IO; -using FluentAssertions; using System.Linq; +using FluentAssertions; using Xunit; namespace System.CommandLine.Tests.Binding @@ -65,6 +65,21 @@ public void Command_argument_of_FileInfo_returns_null_when_argument_is_not_provi .BeNull(); } + [Fact] + public void Argument_of_FileInfo_that_is_empty_results_in_an_informative_error() + { + var option = new Option("--file"); + var result = option.Parse(new string[] { "--file", "" }); + + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Contain("Cannot parse argument '' for option '--file'"); + } + [Fact] public void Argument_of_array_of_FileInfo_can_be_called_without_custom_conversion_logic() { diff --git a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs index 3314be2c01..24b2b8fb7b 100644 --- a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs +++ b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs @@ -62,6 +62,11 @@ internal static partial class ArgumentConverter [typeof(DirectoryInfo)] = (string path, out object? value) => { + if (String.IsNullOrEmpty(path)) + { + value = default; + return false; + } value = new DirectoryInfo(path); return true; }, @@ -80,12 +85,22 @@ internal static partial class ArgumentConverter [typeof(FileInfo)] = (string path, out object? value) => { + if (String.IsNullOrEmpty(path)) + { + value = default; + return false; + } value = new FileInfo(path); return true; }, [typeof(FileSystemInfo)] = (string path, out object? value) => { + if (String.IsNullOrEmpty(path)) + { + value = default; + return false; + } if (Directory.Exists(path)) { value = new DirectoryInfo(path); From b524516dc42a4dc7bfb9f7909497f2c9081e42d6 Mon Sep 17 00:00:00 2001 From: Christian Jones Date: Sat, 9 Apr 2022 17:59:28 -0400 Subject: [PATCH 2/2] Revert autoformat --- src/System.CommandLine.Tests/Binding/TypeConversionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 85dfdf50be..0e15ae1af4 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -5,8 +5,8 @@ using System.Collections.Generic; using System.CommandLine.Utility; using System.IO; -using System.Linq; using FluentAssertions; +using System.Linq; using Xunit; namespace System.CommandLine.Tests.Binding