diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 93edfe7959..0e15ae1af4 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -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);