From b43093adc2437fb5fee92cc469ba0f62f19ddbd5 Mon Sep 17 00:00:00 2001 From: Patryk Golebiowski Date: Sat, 22 Jul 2023 13:50:53 +0200 Subject: [PATCH] Add `ExpectedAsSinglePathToExistingDirectoryOrFile` --- .../WithArguments/IUserInputToCommandOption.cs | 7 +++++++ .../Leaf/WithArguments/UserInputToCommandOption.cs | 14 ++++++++++++++ src/TreeBasedCli/TreeBasedCli.nuspec | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/TreeBasedCli/Commands/Leaf/WithArguments/IUserInputToCommandOption.cs b/src/TreeBasedCli/Commands/Leaf/WithArguments/IUserInputToCommandOption.cs index f9f0f10..1078b46 100644 --- a/src/TreeBasedCli/Commands/Leaf/WithArguments/IUserInputToCommandOption.cs +++ b/src/TreeBasedCli/Commands/Leaf/WithArguments/IUserInputToCommandOption.cs @@ -48,6 +48,13 @@ public interface IUserInputToCommandOption /// string ExpectedAsSinglePathToExistingDirectory(); + /// + /// Gets the single value of this argument, interpreted as a path to an existing file or directory. + /// This method throws a if the number of values is not equal to 1. + /// It throws a if no file or directory exists at the specified path. + /// + string ExpectedAsSinglePathToExistingDirectoryOrFile(); + /// /// Gets the single value of this argument, interpreted as an integer. /// This method throws a if the number of values is not equal to 1. diff --git a/src/TreeBasedCli/Commands/Leaf/WithArguments/UserInputToCommandOption.cs b/src/TreeBasedCli/Commands/Leaf/WithArguments/UserInputToCommandOption.cs index 061aaf6..2c622db 100644 --- a/src/TreeBasedCli/Commands/Leaf/WithArguments/UserInputToCommandOption.cs +++ b/src/TreeBasedCli/Commands/Leaf/WithArguments/UserInputToCommandOption.cs @@ -77,6 +77,20 @@ public string ExpectedAsSinglePathToExistingDirectory() return path; } + /// + public string ExpectedAsSinglePathToExistingDirectoryOrFile() + { + string path = this.ExpectedAsSingleValue(); + + if (!File.Exists(path) && !Directory.Exists(path)) + { + ThrowHelper.MessageOnlyException( + $"The file or directory '{path}' does not exist."); + } + + return path; + } + /// public int ExpectedAsSingleInteger() { diff --git a/src/TreeBasedCli/TreeBasedCli.nuspec b/src/TreeBasedCli/TreeBasedCli.nuspec index a248ec2..c7e40b1 100644 --- a/src/TreeBasedCli/TreeBasedCli.nuspec +++ b/src/TreeBasedCli/TreeBasedCli.nuspec @@ -2,7 +2,7 @@ TreeBasedCli - 3.2.5 + 3.2.6 Patryk Golebiowski Patryk Golebiowski